Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 17 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,15 @@ class Serializer {
if (typeof i === 'bigint') {
return i.toString()
} else if (Number.isInteger(i)) {
return this.asNumber(i)
return '' + i
} else {
/* eslint no-undef: "off" */
return this.asNumber(this.parseInteger(i))
const integer = this.parseInteger(i)
if (Number.isNaN(integer)) {
throw new Error(`The value "${i}" cannot be converted to an integer.`)
} else {
return '' + integer
}
}
}

Expand All @@ -99,8 +104,8 @@ class Serializer {

asNumber (i) {
const num = Number(i)
if (isNaN(num)) {
return 'null'
if (Number.isNaN(num)) {
throw new Error(`The value "${i}" cannot be converted to a number.`)
} else {
return '' + num
}
Expand Down Expand Up @@ -725,7 +730,7 @@ function buildCode (location, code, laterCode, name) {
const schema = location.schema
let required = schema.required

Object.keys(schema.properties || {}).forEach((key, i, a) => {
Object.keys(schema.properties || {}).forEach((key) => {
let propertyLocation = mergeLocation(location, { schema: schema.properties[key] })
if (schema.properties[key].$ref) {
propertyLocation = refFinder(schema.properties[key].$ref, location)
Expand All @@ -735,45 +740,18 @@ function buildCode (location, code, laterCode, name) {
// Using obj['key'] !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.

const type = schema.properties[key].type
const nullable = schema.properties[key].nullable
const sanitized = JSON.stringify(key)
const asString = JSON.stringify(sanitized)

if (nullable) {
code += `
if (obj[${sanitized}] === null) {
${addComma}
json += ${asString} + ':null'
} else {
`
}

if (type === 'number') {
code += `
var t = Number(obj[${sanitized}])
if (!isNaN(t)) {
${addComma}
json += ${asString} + ':' + t
`
} else if (type === 'integer') {
code += `
var t = serializer.asInteger(obj[${sanitized}])
if (!isNaN(t)) {
${addComma}
json += ${asString} + ':' + t
code += `
if (obj[${sanitized}] !== undefined) {
${addComma}
json += ${asString} + ':'
`
} else {
code += `
if (obj[${sanitized}] !== undefined) {
${addComma}
json += ${asString} + ':'
`

const result = nested(laterCode, name, key, mergeLocation(propertyLocation, { schema: schema.properties[key] }), undefined, false)
code += result.code
laterCode = result.laterCode
}
const result = nested(laterCode, name, key, mergeLocation(propertyLocation, { schema: schema.properties[key] }), undefined, false)
code += result.code
laterCode = result.laterCode

const defaultValue = schema.properties[key].default
if (defaultValue !== undefined) {
Expand All @@ -794,12 +772,6 @@ function buildCode (location, code, laterCode, name) {
code += `
}
`

if (nullable) {
code += `
}
`
}
})

if (required && required.length > 0) {
Expand Down
16 changes: 8 additions & 8 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ buildTest({
readonly: true
})

test('skip or coerce numbers and integers that are not numbers', (t) => {
test('throw an error or coerce numbers and integers that are not numbers', (t) => {
const stringify = build({
title: 'basic',
type: 'object',
Expand All @@ -275,14 +275,14 @@ test('skip or coerce numbers and integers that are not numbers', (t) => {
}
})

let result = stringify({
age: 'hello ',
distance: 'long'
})

t.same(JSON.parse(result), {})
try {
stringify({ age: 'hello ', distance: 'long' })
t.fail('should throw an error')
} catch (err) {
t.ok(err)
}

result = stringify({
const result = stringify({
age: '42',
distance: true
})
Expand Down
14 changes: 11 additions & 3 deletions test/patternProperties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test('patternProperties - string coerce', (t) => {
})

test('patternProperties - number coerce', (t) => {
t.plan(1)
t.plan(2)
const stringify = build({
title: 'check number coerce',
type: 'object',
Expand All @@ -75,8 +75,16 @@ test('patternProperties - number coerce', (t) => {
}
})

const obj = { foo: true, ofoo: '42', xfoo: 'string', arrfoo: [1, 2], objfoo: { num: 42 } }
t.equal(stringify(obj), '{"foo":1,"ofoo":42,"xfoo":null,"arrfoo":null,"objfoo":null}')
const coercibleValues = { foo: true, ofoo: '42' }
t.equal(stringify(coercibleValues), '{"foo":1,"ofoo":42}')

const incoercibleValues = { xfoo: 'string', arrfoo: [1, 2], objfoo: { num: 42 } }
try {
stringify(incoercibleValues)
t.fail('should throw an error')
} catch (err) {
t.ok(err)
}
})

test('patternProperties - boolean coerce', (t) => {
Expand Down
2 changes: 1 addition & 1 deletion test/required.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ test('required numbers', (t) => {
})
t.fail()
} catch (e) {
t.equal(e.message, '"num" is required!')
t.equal(e.message, 'The value "aaa" cannot be converted to an integer.')
t.pass()
}
})