Skip to content

Commit 75176eb

Browse files
committed
fix(build): Prevent invalid array items
1 parent d242fb3 commit 75176eb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ function buildArray (schema, code, name, externalSchema, fullSchema) {
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,27 @@ buildTest({
140140
}
141141
}
142142
}, {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)