Skip to content

Commit

Permalink
fix: remove array items type checking (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko authored Sep 6, 2022
1 parent 48d76f7 commit 07f07c8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 65 deletions.
52 changes: 5 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,19 +626,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 += ','
}
}
`
Expand Down Expand Up @@ -675,43 +670,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++
Expand Down
65 changes: 47 additions & 18 deletions test/array.test.js
Original file line number Diff line number Diff line change
@@ -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('..')

Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 07f07c8

Please sign in to comment.