Skip to content

Commit 411adb7

Browse files
committed
fix(build): Correctly handle repeated array types
1 parent 1585900 commit 411adb7

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,35 +407,35 @@ function buildArray (schema, code, name, externalSchema, fullSchema) {
407407
result = schema.items.reduce((res, item, i) => {
408408
var accessor = '[i]'
409409
const tmpRes = nested(laterCode, name, accessor, item, externalSchema, fullSchema, i)
410-
var condition
410+
var condition = `i === ${i} && `
411411
switch (item.type) {
412412
case 'null':
413-
condition = `obj${accessor} === null`
413+
condition += `obj${accessor} === null`
414414
break
415415
case 'string':
416-
condition = `typeof obj${accessor} === 'string'`
416+
condition += `typeof obj${accessor} === 'string'`
417417
break
418418
case 'integer':
419-
condition = `Number.isInteger(obj${accessor})`
419+
condition += `Number.isInteger(obj${accessor})`
420420
break
421421
case 'number':
422-
condition = `!Number.isInteger(obj${accessor}) && Number.isFinite(obj${accessor})`
422+
condition += `Number.isFinite(obj${accessor})`
423423
break
424424
case 'boolean':
425-
condition = `typeof obj${accessor} === 'boolean'`
425+
condition += `typeof obj${accessor} === 'boolean'`
426426
break
427427
case 'object':
428-
condition = `obj${accessor} && typeof obj${accessor} === 'object' && obj${accessor}.constructor === Object`
428+
condition += `obj${accessor} && typeof obj${accessor} === 'object' && obj${accessor}.constructor === Object`
429429
break
430430
case 'array':
431-
condition = `Array.isArray(obj${accessor})`
431+
condition += `Array.isArray(obj${accessor})`
432432
break
433433
default:
434434
throw new Error(`${item.type} unsupported`)
435435
}
436436
return {
437437
code: `${res.code}
438-
if (${condition}) {
438+
${i > 0 ? 'else' : ''} if (${condition}) {
439439
${tmpRes.code}
440440
}`,
441441
laterCode: `${res.laterCode}

test/array.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,21 @@ buildTest({
9494
}, {
9595
ids: [null, 'test', 1, 1.1, true, {a: 'test'}, ['test']]
9696
})
97+
98+
buildTest({
99+
'title': 'repeated types',
100+
'type': 'object',
101+
'properties': {
102+
'ids': {
103+
'type': 'array',
104+
'items': [
105+
{
106+
type: 'number'
107+
},
108+
{
109+
type: 'number'
110+
}
111+
]
112+
}
113+
}
114+
}, {ids: [1, 2]})

0 commit comments

Comments
 (0)