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
6 changes: 4 additions & 2 deletions serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = class Serializer {
} else {
/* eslint no-undef: "off" */
const integer = this.parseInteger(i)
if (Number.isNaN(integer)) {
if (Number.isNaN(integer) || !Number.isFinite(integer)) {
throw new Error(`The value "${i}" cannot be converted to an integer.`)
} else {
return '' + integer
Expand All @@ -48,8 +48,10 @@ module.exports = class Serializer {

asNumber (i) {
const num = Number(i)
if (Number.isNaN(num) || !Number.isFinite(num)) {
if (Number.isNaN(num)) {
throw new Error(`The value "${i}" cannot be converted to a number.`)
} else if (!Number.isFinite(num)) {
return null
} else {
return '' + num
}
Expand Down
24 changes: 20 additions & 4 deletions test/Infinity.test.js → test/infinity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const test = require('tap').test
const build = require('..')

test('Finite numbers', t => {
const values = [-5, 0, -0, 1.33, Math.E, Number.EPSILON, Number.MAX_SAFE_INTEGER, Number.MAX_VALUE,
const values = [-5, 0, -0, 1.33, 99, 100.0,
Math.E, Number.EPSILON,
Number.MAX_SAFE_INTEGER, Number.MAX_VALUE,
Number.MIN_SAFE_INTEGER, Number.MIN_VALUE]

t.plan(values.length)
Expand All @@ -18,13 +20,13 @@ test('Finite numbers', t => {
values.forEach(v => t.equal(stringify(v), JSON.stringify(v)))
})

test('Infinite numbers', t => {
test('Infinite integers', t => {
const values = [Infinity, -Infinity]

t.plan(values.length)

const schema = {
type: 'number'
type: 'integer'
}

const stringify = build(schema)
Expand All @@ -33,7 +35,21 @@ test('Infinite numbers', t => {
try {
stringify(v)
} catch (err) {
t.equal(err.message, `The value "${v}" cannot be converted to a number.`)
t.equal(err.message, `The value "${v}" cannot be converted to an integer.`)
}
})
})

test('Infinite numbers', t => {
const values = [Infinity, -Infinity]

t.plan(values.length)

const schema = {
type: 'number'
}

const stringify = build(schema)

values.forEach(v => t.equal(stringify(v), JSON.stringify(v)))
})