Skip to content

Commit

Permalink
Attempt to fix 333 (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarfd authored May 28, 2021
1 parent 5d1c7b9 commit eef2c2f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,8 @@ function $asInteger (i) {
} else if (Number.isInteger(i)) {
return $asNumber(i)
} else {
// if the output is NaN the type is coerced to int 0
/* eslint no-undef: "off" */
return $asNumber(parseInteger(i) || 0)
return $asNumber(parseInteger(i))
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test('verify padding for rendered date in a string when format is date', (t) =>
t.ok(validate(JSON.parse(output)), 'valid schema')
})

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

const schema = {
Expand Down
33 changes: 32 additions & 1 deletion test/integer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const semver = require('semver')
const validator = require('is-my-json-valid')
const proxyquire = require('proxyquire')
const build = proxyquire('..', { long: null })
const ROUNDING_TYPES = ['ceil', 'floor', 'round']

test('render an integer as JSON', (t) => {
t.plan(2)
Expand Down Expand Up @@ -40,6 +41,10 @@ test('render a float as an integer', (t) => {
const cases = [
{ input: Math.PI, output: '3' },
{ input: 5.0, output: '5' },
{ input: null, output: '0' },
{ input: 0, output: '0' },
{ input: 0.0, output: '0' },
{ input: 42, output: '42' },
{ input: 1.99999, output: '1' },
{ input: -45.05, output: '-45' },
{ input: 0.95, output: '1', rounding: 'ceil' },
Expand Down Expand Up @@ -138,7 +143,7 @@ if (semver.gt(process.versions.node, '10.3.0')) {
t.end()
}

test('should round interger object parameter ', t => {
test('should round integer object parameter', t => {
t.plan(2)

const schema = { type: 'object', properties: { magic: { type: 'integer' } } }
Expand All @@ -149,3 +154,29 @@ test('should round interger object parameter ', t => {
t.equal(output, '{"magic":5}')
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test('should not stringify a property if it does not exist', t => {
t.plan(2)

const schema = { title: 'Example Schema', type: 'object', properties: { age: { type: 'integer' } } }
const validate = validator(schema)
const stringify = build(schema)
const output = stringify({})

t.equal(output, '{}')
t.ok(validate(JSON.parse(output)), 'valid schema')
})

ROUNDING_TYPES.forEach((rounding) => {
test(`should not stringify a property if it does not exist (rounding: ${rounding})`, t => {
t.plan(2)

const schema = { type: 'object', properties: { magic: { type: 'integer' } } }
const validate = validator(schema)
const stringify = build(schema, { rounding })
const output = stringify({})

t.equal(output, '{}')
t.ok(validate(JSON.parse(output)), 'valid schema')
})
})

0 comments on commit eef2c2f

Please sign in to comment.