diff --git a/index.js b/index.js index a4c43b56..453427e4 100644 --- a/index.js +++ b/index.js @@ -641,19 +641,14 @@ function buildArray (location) { if (Array.isArray(itemsSchema)) { for (let i = 0; i < itemsSchema.length; i++) { - const item = itemsSchema[i] const tmpRes = buildValue(mergeLocation(itemsLocation, i), `obj[${i}]`) functionCode += ` if (${i} < arrayLength) { - if (${buildArrayTypeCondition(item.type, `[${i}]`)}) { - let json = '' - ${tmpRes} - jsonOutput += json - if (${i} < arrayLength - 1) { - jsonOutput += ',' - } - } else { - throw new Error(\`Item at ${i} does not match schema definition.\`) + let json = '' + ${tmpRes} + jsonOutput += json + if (${i} < arrayLength - 1) { + jsonOutput += ',' } } ` @@ -690,43 +685,6 @@ function buildArray (location) { return functionName } -function buildArrayTypeCondition (type, accessor) { - let condition - switch (type) { - case 'null': - condition = `obj${accessor} === null` - break - case 'string': - condition = `typeof obj${accessor} === 'string'` - break - case 'integer': - condition = `Number.isInteger(obj${accessor})` - break - case 'number': - condition = `Number.isFinite(obj${accessor})` - break - case 'boolean': - condition = `typeof obj${accessor} === 'boolean'` - break - case 'object': - condition = `obj${accessor} && typeof obj${accessor} === 'object' && obj${accessor}.constructor === Object` - break - case 'array': - condition = `Array.isArray(obj${accessor})` - break - default: - if (Array.isArray(type)) { - const conditions = type.map((subType) => { - return buildArrayTypeCondition(subType, accessor) - }) - condition = `(${conditions.join(' || ')})` - } else { - throw new Error(`${type} unsupported`) - } - } - return condition -} - let genFuncNameCounter = 0 function generateFuncName () { return 'anonymous' + genFuncNameCounter++ diff --git a/test/array.test.js b/test/array.test.js index b1fe96ff..d844be8f 100644 --- a/test/array.test.js +++ b/test/array.test.js @@ -1,6 +1,7 @@ 'use strict' const test = require('tap').test +const { DateTime } = require('luxon') const validator = require('is-my-json-valid') const build = require('..') @@ -152,28 +153,56 @@ buildTest({ '@data': ['test'] }) -test('invalid items throw', (t) => { +test('coerce number to string type item', (t) => { t.plan(1) const schema = { - type: 'object', - properties: { - args: { - type: 'array', - items: [ - { - type: 'object', - patternProperties: { - '.*': { - type: 'string' - } - } - } - ] - } - } + type: 'array', + items: [{ type: 'string' }] + } + const stringify = build(schema) + t.equal(stringify([1]), '["1"]') +}) + +test('coerce string to number type item', (t) => { + t.plan(1) + const schema = { + type: 'array', + items: [{ type: 'number' }] + } + const stringify = build(schema) + t.equal(stringify(['1']), '[1]') +}) + +test('coerce string to integer type item', (t) => { + t.plan(1) + const schema = { + type: 'array', + items: [{ type: 'integer' }] + } + const stringify = build(schema) + t.equal(stringify(['1']), '[1]') +}) + +test('coerce date to string (date) type item', (t) => { + t.plan(1) + const schema = { + type: 'array', + items: [{ type: 'string', format: 'date' }] + } + const stringify = build(schema) + const date = new Date() + t.equal(stringify([date]), `["${DateTime.fromJSDate(date).toISODate()}"]`) +}) + +test('coerce date to string (time) type item', (t) => { + t.plan(1) + const schema = { + type: 'array', + items: [{ type: 'string', format: 'time' }] } const stringify = build(schema) - t.throws(() => stringify({ args: ['invalid'] })) + const date = new Date() + t.equal(stringify([date]), `["${DateTime.fromJSDate(date).toFormat('HH:mm:ss')}"]`) }) buildTest({