Skip to content

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

Merged
merged 3 commits into from
Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}:'`
Copy link
Member

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


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 += `
}
`
}
})

Expand Down Expand Up @@ -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})`
Copy link
Member

@mcollina mcollina Aug 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same in all the above. It reads better here, IMHO.

Copy link
Member Author

@delvedor delvedor Aug 22, 2016

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, I cannot fight with "I'm selfish and lazy :D."
I will update it :)

break
default:
throw new Error(`${type} unsupported`)
Expand Down
26 changes: 25 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,31 @@ test('object with required field', (t) => {
})
t.fail()
} catch (e) {
t.is(e.message, '.str is required!')
t.is(e.message, 'str is required!')
t.pass()
}
})

test('missing values', (t) => {
t.plan(3)

const stringify = build({
title: 'object with missing values',
type: 'object',
properties: {
str: {
type: 'string'
},
num: {
type: 'number'
},
val: {
type: 'string'
}
}
})

t.equal('{"val":"value"}', stringify({ val: 'value' }))
t.equal('{"str":"string","val":"value"}', stringify({ str: 'string', val: 'value' }))
t.equal('{"str":"string","num":42,"val":"value"}', stringify({ str: 'string', num: 42, val: 'value' }))
})