Skip to content

Commit 8ebc931

Browse files
cemremengumcollina
authored andcommitted
Make integer/number handling consistent for type arrays (#146)
* Make integer handling consistent, add tests * Add note about null and coercion
1 parent 207bf9b commit 8ebc931

File tree

4 files changed

+162
-5
lines changed

4 files changed

+162
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,13 @@ console.log(stringify({product: null})) // "{"product":null}"
481481
console.log(stringify(null)) // null
482482
```
483483
484+
Otherwise, instead of raising an error, null values will be coerced as follows:
485+
486+
- `integer` -> `0`
487+
- `number` -> `0`
488+
- `string` -> `""`
489+
- `boolean` -> `false`
490+
484491
<a name="acknowledgements"></a>
485492
## Acknowledgements
486493

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,12 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
879879
`
880880
} else if (type === 'integer') {
881881
code += `
882-
${index === 0 ? 'if' : 'else if'}(Number.isInteger(obj${accessor}))
882+
${index === 0 ? 'if' : 'else if'}(Number.isInteger(obj${accessor}) || obj${accessor} === null)
883+
${nestedResult.code}
884+
`
885+
} else if (type === 'number') {
886+
code += `
887+
${index === 0 ? 'if' : 'else if'}(isNaN(obj${accessor}) === false)
883888
${nestedResult.code}
884889
`
885890
} else {

test/missing-values.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,48 @@ test('handle null when value should be string', (t) => {
4141

4242
t.equal('{"str":""}', stringify({ str: null }))
4343
})
44+
45+
test('handle null when value should be integer', (t) => {
46+
t.plan(1)
47+
48+
const stringify = build({
49+
type: 'object',
50+
properties: {
51+
int: {
52+
type: 'integer'
53+
}
54+
}
55+
})
56+
57+
t.equal('{"int":0}', stringify({ int: null }))
58+
})
59+
60+
test('handle null when value should be number', (t) => {
61+
t.plan(1)
62+
63+
const stringify = build({
64+
type: 'object',
65+
properties: {
66+
num: {
67+
type: 'number'
68+
}
69+
}
70+
})
71+
72+
t.equal('{"num":0}', stringify({ num: null }))
73+
})
74+
75+
test('handle null when value should be boolean', (t) => {
76+
t.plan(1)
77+
78+
const stringify = build({
79+
type: 'object',
80+
properties: {
81+
bool: {
82+
type: 'boolean'
83+
}
84+
}
85+
})
86+
87+
t.equal('{"bool":false}', stringify({ bool: null }))
88+
})

test/typesArray.test.js

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

6-
test('possibly nullable primitive alternative', (t) => {
6+
test('possibly nullable integer primitive alternative', (t) => {
77
t.plan(1)
88

99
const schema = {
@@ -28,7 +28,32 @@ test('possibly nullable primitive alternative', (t) => {
2828
}
2929
})
3030

31-
test('possibly nullable primitive alternative with null value', (t) => {
31+
test('possibly nullable number primitive alternative', (t) => {
32+
t.plan(1)
33+
34+
const schema = {
35+
title: 'simple object with multi-type nullable primitive',
36+
type: 'object',
37+
properties: {
38+
data: {
39+
type: ['number']
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+
56+
test('possibly nullable integer primitive alternative with null value', (t) => {
3257
t.plan(1)
3358

3459
const schema = {
@@ -47,13 +72,38 @@ test('possibly nullable primitive alternative with null value', (t) => {
4772
const value = stringify({
4873
data: null
4974
})
50-
t.is(value, '{"data":null}')
75+
t.is(value, '{"data":0}')
76+
} catch (e) {
77+
t.fail()
78+
}
79+
})
80+
81+
test('possibly nullable number primitive alternative with null value', (t) => {
82+
t.plan(1)
83+
84+
const schema = {
85+
title: 'simple object with multi-type nullable primitive',
86+
type: 'object',
87+
properties: {
88+
data: {
89+
type: ['number']
90+
}
91+
}
92+
}
93+
94+
const stringify = build(schema)
95+
96+
try {
97+
const value = stringify({
98+
data: null
99+
})
100+
t.is(value, '{"data":0}')
51101
} catch (e) {
52102
t.fail()
53103
}
54104
})
55105

56-
test('nullable primitive', (t) => {
106+
test('nullable integer primitive', (t) => {
57107
t.plan(1)
58108

59109
const schema = {
@@ -78,6 +128,31 @@ test('nullable primitive', (t) => {
78128
}
79129
})
80130

131+
test('nullable number primitive', (t) => {
132+
t.plan(1)
133+
134+
const schema = {
135+
title: 'simple object with nullable primitive',
136+
type: 'object',
137+
properties: {
138+
data: {
139+
type: ['number', 'null']
140+
}
141+
}
142+
}
143+
144+
const stringify = build(schema)
145+
146+
try {
147+
const value = stringify({
148+
data: 4
149+
})
150+
t.is(value, '{"data":4}')
151+
} catch (e) {
152+
t.fail()
153+
}
154+
})
155+
81156
test('nullable primitive with null value', (t) => {
82157
t.plan(1)
83158

@@ -103,6 +178,31 @@ test('nullable primitive with null value', (t) => {
103178
}
104179
})
105180

181+
test('nullable number primitive with null value', (t) => {
182+
t.plan(1)
183+
184+
const schema = {
185+
title: 'simple object with nullable primitive',
186+
type: 'object',
187+
properties: {
188+
data: {
189+
type: ['number', 'null']
190+
}
191+
}
192+
}
193+
194+
const stringify = build(schema)
195+
196+
try {
197+
const value = stringify({
198+
data: null
199+
})
200+
t.is(value, '{"data":null}')
201+
} catch (e) {
202+
t.fail()
203+
}
204+
})
205+
106206
test('possibly null object with multi-type property', (t) => {
107207
t.plan(3)
108208

0 commit comments

Comments
 (0)