Skip to content

Commit

Permalink
Corrects integer handling (#134)
Browse files Browse the repository at this point in the history
* Closes #133
  • Loading branch information
bhamon authored and cemremengu committed Mar 1, 2019
1 parent 9279277 commit 3683784
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,11 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
${index === 0 ? 'if' : 'else if'}(Array.isArray(obj${accessor}))
${nestedResult.code}
`
} else if (type === 'integer') {
code += `
${index === 0 ? 'if' : 'else if'}(Number.isInteger(obj${accessor}))
${nestedResult.code}
`
} else {
code += `
${index === 0 ? 'if' : 'else if'}(typeof obj${accessor} === "${type}")
Expand Down
50 changes: 50 additions & 0 deletions test/typesArray.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,56 @@
const test = require('tap').test
const build = require('..')

test('possibly nullable primitive alternative', (t) => {
t.plan(1)

const schema = {
title: 'simple object with multi-type nullable primitive',
type: 'object',
properties: {
data: {
type: ['integer']
}
}
}

const stringify = build(schema)

try {
const value = stringify({
data: 4
})
t.is(value, '{"data":4}')
} catch (e) {
t.fail()
}
})

test('nullable primitive', (t) => {
t.plan(1)

const schema = {
title: 'simple object with nullable primitive',
type: 'object',
properties: {
data: {
type: ['integer', 'null']
}
}
}

const stringify = build(schema)

try {
const value = stringify({
data: 4
})
t.is(value, '{"data":4}')
} catch (e) {
t.fail()
}
})

test('possibly null object with multi-type property', (t) => {
t.plan(3)

Expand Down

0 comments on commit 3683784

Please sign in to comment.