-
-
Notifications
You must be signed in to change notification settings - Fork 209
Support for missing parameters #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,17 +140,33 @@ function buildObject (schema, code, name) { | |
var laterCode = '' | ||
|
||
Object.keys(schema.properties).forEach((key, i, a) => { | ||
/* code += ` | ||
if (obj.hasOwnProperty('${key}')) { | ||
json += '${$asString(key)}:'`*/ | ||
code += ` | ||
json += '${$asString(key)}:' | ||
` | ||
if (obj.${key} !== undefined) { | ||
json += '${$asString(key)}:'` | ||
|
||
const result = nested(laterCode, name, '.' + key, schema.properties[key]) | ||
|
||
code += result.code | ||
laterCode = result.laterCode | ||
|
||
if (i < a.length - 1) { | ||
code += 'json += \',\'' | ||
code += ` | ||
json += \',\'` | ||
} | ||
|
||
if (schema.properties[key].required) { | ||
code += ` | ||
} else { | ||
throw new Error('${key} is required!') | ||
} | ||
` | ||
} else { | ||
code += ` | ||
} | ||
` | ||
} | ||
}) | ||
|
||
|
@@ -202,48 +218,36 @@ function buildArray (schema, code, name) { | |
function nested (laterCode, name, key, schema) { | ||
var code = '' | ||
var funcName | ||
if (schema.required) { | ||
code += ` | ||
if (!obj.hasOwnProperty('${key.slice(1)}')) { | ||
throw new Error('${key} is required!') | ||
}` | ||
} | ||
const type = schema.type | ||
switch (type) { | ||
case 'null': | ||
code += ` | ||
json += $asNull() | ||
` | ||
json += $asNull()` | ||
break | ||
case 'string': | ||
code += ` | ||
json += $asString(obj${key}) | ||
` | ||
json += $asString(obj${key})` | ||
break | ||
case 'number': | ||
case 'integer': | ||
code += ` | ||
json += $asNumber(obj${key}) | ||
` | ||
json += $asNumber(obj${key})` | ||
break | ||
case 'boolean': | ||
code += ` | ||
json += $asBoolean(obj${key}) | ||
` | ||
json += $asBoolean(obj${key})` | ||
break | ||
case 'object': | ||
funcName = (name + key).replace(/[-.\[\]]/g, '') | ||
laterCode = buildObject(schema, laterCode, funcName) | ||
code += ` | ||
json += ${funcName}(obj${key}) | ||
` | ||
json += ${funcName}(obj${key})` | ||
break | ||
case 'array': | ||
funcName = (name + key).replace(/[-.\[\]]/g, '') | ||
laterCode = buildArray(schema, laterCode, funcName) | ||
code += ` | ||
json += ${funcName}(obj${key}) | ||
` | ||
json += ${funcName}(obj${key})` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same in all the above. It reads better here, IMHO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did that change to improve generated code readability, if I close the '`' in the newline the generated code will be: if (obj.lastName !== undefined) {
json += '"lastName":'
json += $asString(obj.lastName)
json += ','
} instead of if (obj.lastName !== undefined) {
json += '"lastName":'
json += $asString(obj.lastName)
json += ','
} Is it ok to you? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. every generated code was put it in this way so that he could easily copied and pasted around, without worrying about backticks :). It's optimized to be read in the "template" form, rather than the generated one: I'm selfish and lazy :D. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol, I cannot fight with "I'm selfish and lazy :D." |
||
break | ||
default: | ||
throw new Error(`${type} unsupported`) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please close the '`' in the new line