Skip to content

Commit 07f07c8

Browse files
fix: remove array items type checking (#524)
1 parent 48d76f7 commit 07f07c8

File tree

2 files changed

+52
-65
lines changed

2 files changed

+52
-65
lines changed

index.js

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -626,19 +626,14 @@ function buildArray (location) {
626626

627627
if (Array.isArray(itemsSchema)) {
628628
for (let i = 0; i < itemsSchema.length; i++) {
629-
const item = itemsSchema[i]
630629
const tmpRes = buildValue(mergeLocation(itemsLocation, i), `obj[${i}]`)
631630
functionCode += `
632631
if (${i} < arrayLength) {
633-
if (${buildArrayTypeCondition(item.type, `[${i}]`)}) {
634-
let json = ''
635-
${tmpRes}
636-
jsonOutput += json
637-
if (${i} < arrayLength - 1) {
638-
jsonOutput += ','
639-
}
640-
} else {
641-
throw new Error(\`Item at ${i} does not match schema definition.\`)
632+
let json = ''
633+
${tmpRes}
634+
jsonOutput += json
635+
if (${i} < arrayLength - 1) {
636+
jsonOutput += ','
642637
}
643638
}
644639
`
@@ -675,43 +670,6 @@ function buildArray (location) {
675670
return functionName
676671
}
677672

678-
function buildArrayTypeCondition (type, accessor) {
679-
let condition
680-
switch (type) {
681-
case 'null':
682-
condition = `obj${accessor} === null`
683-
break
684-
case 'string':
685-
condition = `typeof obj${accessor} === 'string'`
686-
break
687-
case 'integer':
688-
condition = `Number.isInteger(obj${accessor})`
689-
break
690-
case 'number':
691-
condition = `Number.isFinite(obj${accessor})`
692-
break
693-
case 'boolean':
694-
condition = `typeof obj${accessor} === 'boolean'`
695-
break
696-
case 'object':
697-
condition = `obj${accessor} && typeof obj${accessor} === 'object' && obj${accessor}.constructor === Object`
698-
break
699-
case 'array':
700-
condition = `Array.isArray(obj${accessor})`
701-
break
702-
default:
703-
if (Array.isArray(type)) {
704-
const conditions = type.map((subType) => {
705-
return buildArrayTypeCondition(subType, accessor)
706-
})
707-
condition = `(${conditions.join(' || ')})`
708-
} else {
709-
throw new Error(`${type} unsupported`)
710-
}
711-
}
712-
return condition
713-
}
714-
715673
let genFuncNameCounter = 0
716674
function generateFuncName () {
717675
return 'anonymous' + genFuncNameCounter++

test/array.test.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const test = require('tap').test
4+
const { DateTime } = require('luxon')
45
const validator = require('is-my-json-valid')
56
const build = require('..')
67

@@ -152,28 +153,56 @@ buildTest({
152153
'@data': ['test']
153154
})
154155

155-
test('invalid items throw', (t) => {
156+
test('coerce number to string type item', (t) => {
156157
t.plan(1)
157158
const schema = {
158-
type: 'object',
159-
properties: {
160-
args: {
161-
type: 'array',
162-
items: [
163-
{
164-
type: 'object',
165-
patternProperties: {
166-
'.*': {
167-
type: 'string'
168-
}
169-
}
170-
}
171-
]
172-
}
173-
}
159+
type: 'array',
160+
items: [{ type: 'string' }]
161+
}
162+
const stringify = build(schema)
163+
t.equal(stringify([1]), '["1"]')
164+
})
165+
166+
test('coerce string to number type item', (t) => {
167+
t.plan(1)
168+
const schema = {
169+
type: 'array',
170+
items: [{ type: 'number' }]
171+
}
172+
const stringify = build(schema)
173+
t.equal(stringify(['1']), '[1]')
174+
})
175+
176+
test('coerce string to integer type item', (t) => {
177+
t.plan(1)
178+
const schema = {
179+
type: 'array',
180+
items: [{ type: 'integer' }]
181+
}
182+
const stringify = build(schema)
183+
t.equal(stringify(['1']), '[1]')
184+
})
185+
186+
test('coerce date to string (date) type item', (t) => {
187+
t.plan(1)
188+
const schema = {
189+
type: 'array',
190+
items: [{ type: 'string', format: 'date' }]
191+
}
192+
const stringify = build(schema)
193+
const date = new Date()
194+
t.equal(stringify([date]), `["${DateTime.fromJSDate(date).toISODate()}"]`)
195+
})
196+
197+
test('coerce date to string (time) type item', (t) => {
198+
t.plan(1)
199+
const schema = {
200+
type: 'array',
201+
items: [{ type: 'string', format: 'time' }]
174202
}
175203
const stringify = build(schema)
176-
t.throws(() => stringify({ args: ['invalid'] }))
204+
const date = new Date()
205+
t.equal(stringify([date]), `["${DateTime.fromJSDate(date).toFormat('HH:mm:ss')}"]`)
177206
})
178207

179208
buildTest({

0 commit comments

Comments
 (0)