Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: drop array items type checking #706

Merged
merged 1 commit into from
Apr 11, 2024
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
58 changes: 5 additions & 53 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 += ','
}
}
`
Expand Down Expand Up @@ -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++
}
Expand Down
28 changes: 2 additions & 26 deletions test/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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.'))
})

Expand Down
Loading