Skip to content

Commit 3683784

Browse files
bhamoncemremengu
authored andcommitted
Corrects integer handling (#134)
* Closes #133
1 parent 9279277 commit 3683784

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,11 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
877877
${index === 0 ? 'if' : 'else if'}(Array.isArray(obj${accessor}))
878878
${nestedResult.code}
879879
`
880+
} else if (type === 'integer') {
881+
code += `
882+
${index === 0 ? 'if' : 'else if'}(Number.isInteger(obj${accessor}))
883+
${nestedResult.code}
884+
`
880885
} else {
881886
code += `
882887
${index === 0 ? 'if' : 'else if'}(typeof obj${accessor} === "${type}")

test/typesArray.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,56 @@
33
const test = require('tap').test
44
const build = require('..')
55

6+
test('possibly nullable primitive alternative', (t) => {
7+
t.plan(1)
8+
9+
const schema = {
10+
title: 'simple object with multi-type nullable primitive',
11+
type: 'object',
12+
properties: {
13+
data: {
14+
type: ['integer']
15+
}
16+
}
17+
}
18+
19+
const stringify = build(schema)
20+
21+
try {
22+
const value = stringify({
23+
data: 4
24+
})
25+
t.is(value, '{"data":4}')
26+
} catch (e) {
27+
t.fail()
28+
}
29+
})
30+
31+
test('nullable primitive', (t) => {
32+
t.plan(1)
33+
34+
const schema = {
35+
title: 'simple object with nullable primitive',
36+
type: 'object',
37+
properties: {
38+
data: {
39+
type: ['integer', 'null']
40+
}
41+
}
42+
}
43+
44+
const stringify = build(schema)
45+
46+
try {
47+
const value = stringify({
48+
data: 4
49+
})
50+
t.is(value, '{"data":4}')
51+
} catch (e) {
52+
t.fail()
53+
}
54+
})
55+
656
test('possibly null object with multi-type property', (t) => {
757
t.plan(3)
858

0 commit comments

Comments
 (0)