Skip to content

Commit 377288f

Browse files
feat: drop array items type checking (#706)
1 parent ce76e59 commit 377288f

File tree

2 files changed

+7
-79
lines changed

2 files changed

+7
-79
lines changed

index.js

+5-53
Original file line numberDiff line numberDiff line change
@@ -548,19 +548,14 @@ function buildArray (context, location) {
548548

549549
if (Array.isArray(itemsSchema)) {
550550
for (let i = 0; i < itemsSchema.length; i++) {
551-
const item = itemsSchema[i]
552551
const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), `obj[${i}]`)
553552
functionCode += `
554553
if (${i} < arrayLength) {
555-
if (${buildArrayTypeCondition(item.type, `[${i}]`)}) {
556-
let json = ''
557-
${tmpRes}
558-
jsonOutput += json
559-
if (${i} < arrayLength - 1) {
560-
jsonOutput += ','
561-
}
562-
} else {
563-
throw new Error(\`Item at ${i} does not match schema definition.\`)
554+
let json = ''
555+
${tmpRes}
556+
jsonOutput += json
557+
if (${i} < arrayLength - 1) {
558+
jsonOutput += ','
564559
}
565560
}
566561
`
@@ -596,49 +591,6 @@ function buildArray (context, location) {
596591
return functionName
597592
}
598593

599-
function buildArrayTypeCondition (type, accessor) {
600-
let condition
601-
switch (type) {
602-
case 'null':
603-
condition = `obj${accessor} === null`
604-
break
605-
case 'string':
606-
condition = `typeof obj${accessor} === 'string' ||
607-
obj${accessor} === null ||
608-
obj${accessor} instanceof Date ||
609-
obj${accessor} instanceof RegExp ||
610-
(
611-
typeof obj${accessor} === "object" &&
612-
typeof obj${accessor}.toString === "function" &&
613-
obj${accessor}.toString !== Object.prototype.toString
614-
)`
615-
break
616-
case 'integer':
617-
condition = `Number.isInteger(obj${accessor})`
618-
break
619-
case 'number':
620-
condition = `Number.isFinite(obj${accessor})`
621-
break
622-
case 'boolean':
623-
condition = `typeof obj${accessor} === 'boolean'`
624-
break
625-
case 'object':
626-
condition = `obj${accessor} && typeof obj${accessor} === 'object' && obj${accessor}.constructor === Object`
627-
break
628-
case 'array':
629-
condition = `Array.isArray(obj${accessor})`
630-
break
631-
default:
632-
if (Array.isArray(type)) {
633-
const conditions = type.map((subType) => {
634-
return buildArrayTypeCondition(subType, accessor)
635-
})
636-
condition = `(${conditions.join(' || ')})`
637-
}
638-
}
639-
return condition
640-
}
641-
642594
function generateFuncName (context) {
643595
return 'anonymous' + context.functionsCounter++
644596
}

test/array.test.js

+2-26
Original file line numberDiff line numberDiff line change
@@ -195,30 +195,6 @@ buildTest({
195195
'@data': ['test']
196196
})
197197

198-
test('invalid items throw', (t) => {
199-
t.plan(1)
200-
const schema = {
201-
type: 'object',
202-
properties: {
203-
args: {
204-
type: 'array',
205-
items: [
206-
{
207-
type: 'object',
208-
patternProperties: {
209-
'.*': {
210-
type: 'string'
211-
}
212-
}
213-
}
214-
]
215-
}
216-
}
217-
}
218-
const stringify = build(schema)
219-
t.throws(() => stringify({ args: ['invalid'] }))
220-
})
221-
222198
buildTest({
223199
title: 'item types in array default to any',
224200
type: 'object',
@@ -329,8 +305,8 @@ test('array items is a list of schema and additionalItems is false /2', (t) => {
329305

330306
const stringify = build(schema)
331307

332-
t.throws(() => stringify({ foo: [1, 'bar'] }), new Error('Item at 0 does not match schema definition.'))
333-
t.throws(() => stringify({ foo: ['foo', 1] }), new Error('Item at 1 does not match schema definition.'))
308+
t.strictSame(stringify({ foo: [1, 'bar'] }), '{"foo":["1","bar"]}')
309+
t.strictSame(stringify({ foo: ['foo', 1] }), '{"foo":["foo","1"]}')
334310
t.throws(() => stringify({ foo: ['foo', 'bar', 'baz'] }), new Error('Item at 2 does not match schema definition.'))
335311
})
336312

0 commit comments

Comments
 (0)