From 36837843389803127112db1d22bfdc928afa481e Mon Sep 17 00:00:00 2001 From: Benjamin Hamon Date: Fri, 1 Mar 2019 18:30:06 +0100 Subject: [PATCH] Corrects integer handling (#134) * Closes #133 --- index.js | 5 +++++ test/typesArray.test.js | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/index.js b/index.js index cf5faf3a..c6b1c121 100644 --- a/index.js +++ b/index.js @@ -877,6 +877,11 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe ${index === 0 ? 'if' : 'else if'}(Array.isArray(obj${accessor})) ${nestedResult.code} ` + } else if (type === 'integer') { + code += ` + ${index === 0 ? 'if' : 'else if'}(Number.isInteger(obj${accessor})) + ${nestedResult.code} + ` } else { code += ` ${index === 0 ? 'if' : 'else if'}(typeof obj${accessor} === "${type}") diff --git a/test/typesArray.test.js b/test/typesArray.test.js index 8defd528..b7c7ba63 100644 --- a/test/typesArray.test.js +++ b/test/typesArray.test.js @@ -3,6 +3,56 @@ const test = require('tap').test const build = require('..') +test('possibly nullable primitive alternative', (t) => { + t.plan(1) + + const schema = { + title: 'simple object with multi-type nullable primitive', + type: 'object', + properties: { + data: { + type: ['integer'] + } + } + } + + const stringify = build(schema) + + try { + const value = stringify({ + data: 4 + }) + t.is(value, '{"data":4}') + } catch (e) { + t.fail() + } +}) + +test('nullable primitive', (t) => { + t.plan(1) + + const schema = { + title: 'simple object with nullable primitive', + type: 'object', + properties: { + data: { + type: ['integer', 'null'] + } + } + } + + const stringify = build(schema) + + try { + const value = stringify({ + data: 4 + }) + t.is(value, '{"data":4}') + } catch (e) { + t.fail() + } +}) + test('possibly null object with multi-type property', (t) => { t.plan(3)