Skip to content

Commit eef2c2f

Browse files
authored
Attempt to fix 333 (#334)
1 parent 5d1c7b9 commit eef2c2f

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,8 @@ function $asInteger (i) {
254254
} else if (Number.isInteger(i)) {
255255
return $asNumber(i)
256256
} else {
257-
// if the output is NaN the type is coerced to int 0
258257
/* eslint no-undef: "off" */
259-
return $asNumber(parseInteger(i) || 0)
258+
return $asNumber(parseInteger(i))
260259
}
261260
}
262261

test/date.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ test('verify padding for rendered date in a string when format is date', (t) =>
7676
t.ok(validate(JSON.parse(output)), 'valid schema')
7777
})
7878

79-
test('render a date in a string when format is time as HH:mm:ss', (t) => {
79+
test('render a date in a string when format is time as kk:mm:ss', (t) => {
8080
t.plan(3)
8181

8282
const schema = {

test/integer.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const semver = require('semver')
66
const validator = require('is-my-json-valid')
77
const proxyquire = require('proxyquire')
88
const build = proxyquire('..', { long: null })
9+
const ROUNDING_TYPES = ['ceil', 'floor', 'round']
910

1011
test('render an integer as JSON', (t) => {
1112
t.plan(2)
@@ -40,6 +41,10 @@ test('render a float as an integer', (t) => {
4041
const cases = [
4142
{ input: Math.PI, output: '3' },
4243
{ input: 5.0, output: '5' },
44+
{ input: null, output: '0' },
45+
{ input: 0, output: '0' },
46+
{ input: 0.0, output: '0' },
47+
{ input: 42, output: '42' },
4348
{ input: 1.99999, output: '1' },
4449
{ input: -45.05, output: '-45' },
4550
{ input: 0.95, output: '1', rounding: 'ceil' },
@@ -138,7 +143,7 @@ if (semver.gt(process.versions.node, '10.3.0')) {
138143
t.end()
139144
}
140145

141-
test('should round interger object parameter ', t => {
146+
test('should round integer object parameter', t => {
142147
t.plan(2)
143148

144149
const schema = { type: 'object', properties: { magic: { type: 'integer' } } }
@@ -149,3 +154,29 @@ test('should round interger object parameter ', t => {
149154
t.equal(output, '{"magic":5}')
150155
t.ok(validate(JSON.parse(output)), 'valid schema')
151156
})
157+
158+
test('should not stringify a property if it does not exist', t => {
159+
t.plan(2)
160+
161+
const schema = { title: 'Example Schema', type: 'object', properties: { age: { type: 'integer' } } }
162+
const validate = validator(schema)
163+
const stringify = build(schema)
164+
const output = stringify({})
165+
166+
t.equal(output, '{}')
167+
t.ok(validate(JSON.parse(output)), 'valid schema')
168+
})
169+
170+
ROUNDING_TYPES.forEach((rounding) => {
171+
test(`should not stringify a property if it does not exist (rounding: ${rounding})`, t => {
172+
t.plan(2)
173+
174+
const schema = { type: 'object', properties: { magic: { type: 'integer' } } }
175+
const validate = validator(schema)
176+
const stringify = build(schema, { rounding })
177+
const output = stringify({})
178+
179+
t.equal(output, '{}')
180+
t.ok(validate(JSON.parse(output)), 'valid schema')
181+
})
182+
})

0 commit comments

Comments
 (0)