diff --git a/index.js b/index.js index 2507f10a..3d6154be 100644 --- a/index.js +++ b/index.js @@ -548,19 +548,14 @@ function buildArray (context, location) { if (Array.isArray(itemsSchema)) { for (let i = 0; i < itemsSchema.length; i++) { - const item = itemsSchema[i] const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(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 += ',' } } ` @@ -596,49 +591,6 @@ function buildArray (context, 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' || - obj${accessor} === null || - obj${accessor} instanceof Date || - obj${accessor} instanceof RegExp || - ( - typeof obj${accessor} === "object" && - typeof obj${accessor}.toString === "function" && - obj${accessor}.toString !== Object.prototype.toString - )` - 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(' || ')})` - } - } - return condition -} - function generateFuncName (context) { return 'anonymous' + context.functionsCounter++ } diff --git a/test/array.test.js b/test/array.test.js index fbffe02f..d10478f9 100644 --- a/test/array.test.js +++ b/test/array.test.js @@ -195,30 +195,6 @@ buildTest({ '@data': ['test'] }) -test('invalid items throw', (t) => { - t.plan(1) - const schema = { - type: 'object', - properties: { - args: { - type: 'array', - items: [ - { - type: 'object', - patternProperties: { - '.*': { - type: 'string' - } - } - } - ] - } - } - } - const stringify = build(schema) - t.throws(() => stringify({ args: ['invalid'] })) -}) - buildTest({ title: 'item types in array default to any', type: 'object', @@ -329,8 +305,8 @@ test('array items is a list of schema and additionalItems is false /2', (t) => { const stringify = build(schema) - t.throws(() => stringify({ foo: [1, 'bar'] }), new Error('Item at 0 does not match schema definition.')) - t.throws(() => stringify({ foo: ['foo', 1] }), new Error('Item at 1 does not match schema definition.')) + t.strictSame(stringify({ foo: [1, 'bar'] }), '{"foo":["1","bar"]}') + t.strictSame(stringify({ foo: ['foo', 1] }), '{"foo":["foo","1"]}') t.throws(() => stringify({ foo: ['foo', 'bar', 'baz'] }), new Error('Item at 2 does not match schema definition.')) })