Skip to content

Commit

Permalink
feat: drop array items type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko committed Apr 10, 2024
1 parent e87268d commit 5f36654
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 79 deletions.
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

0 comments on commit 5f36654

Please sign in to comment.