Skip to content

Commit 62b52a2

Browse files
authored
Merge pull request #47 from elliotttf/44-fix-pattern-properties-array
fix(build): Correctly handle repeated array types
2 parents 1585900 + 75176eb commit 62b52a2

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

index.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,41 +407,46 @@ 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}
442442
${tmpRes.laterCode}`
443443
}
444444
}, result)
445+
result.code += `
446+
else {
447+
throw new Error(\`Item at $\{i} does not match schema definition.\`)
448+
}
449+
`
445450
} else {
446451
result = nested(laterCode, name, '[i]', schema.items, externalSchema, fullSchema)
447452
}

test/array.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,73 @@ 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]})
115+
116+
buildTest({
117+
'title': 'pattern properties array',
118+
'type': 'object',
119+
'properties': {
120+
'args': {
121+
'type': 'array',
122+
'items': [
123+
{
124+
'type': 'object',
125+
'patternProperties': {
126+
'.*': {
127+
'type': 'string'
128+
}
129+
}
130+
},
131+
{
132+
'type': 'object',
133+
'patternProperties': {
134+
'.*': {
135+
'type': 'number'
136+
}
137+
}
138+
}
139+
]
140+
}
141+
}
142+
}, {args: [{a: 'test'}, {b: 1}]})
143+
144+
test('invalid items throw', (t) => {
145+
t.plan(1)
146+
const schema = {
147+
'type': 'object',
148+
'properties': {
149+
'args': {
150+
'type': 'array',
151+
'items': [
152+
{
153+
'type': 'object',
154+
'patternProperties': {
155+
'.*': {
156+
'type': 'string'
157+
}
158+
}
159+
}
160+
]
161+
}
162+
}
163+
}
164+
const stringify = build(schema)
165+
t.throws(() => stringify({args: ['invalid']}))
166+
})

0 commit comments

Comments
 (0)