Skip to content

Commit 4ddffe5

Browse files
authored
added support to additionalItems property in array object (#322)
1 parent 640cec2 commit 4ddffe5

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,9 @@ function buildArray (location, code, name, key = null) {
980980
}
981981

982982
let result = { code: '', laterCode: '' }
983+
const accessor = '[i]'
983984
if (Array.isArray(schema.items)) {
984985
result = schema.items.reduce((res, item, i) => {
985-
const accessor = '[i]'
986986
const tmpRes = nested(laterCode, name, accessor, mergeLocation(location, { schema: item }), i, true)
987987
const condition = `i === ${i} && ${buildArrayTypeCondition(item.type, accessor)}`
988988
return {
@@ -994,13 +994,23 @@ function buildArray (location, code, name, key = null) {
994994
${tmpRes.laterCode}`
995995
}
996996
}, result)
997+
998+
if (schema.additionalItems) {
999+
const tmpRes = nested(laterCode, name, accessor, mergeLocation(location, { schema: schema.items }), undefined, true)
1000+
result.code += `
1001+
else if (i >= ${schema.items.length}) {
1002+
${tmpRes.code}
1003+
}
1004+
`
1005+
}
1006+
9971007
result.code += `
9981008
else {
9991009
throw new Error(\`Item at $\{i} does not match schema definition.\`)
10001010
}
10011011
`
10021012
} else {
1003-
result = nested(laterCode, name, '[i]', mergeLocation(location, { schema: schema.items }), undefined, true)
1013+
result = nested(laterCode, name, accessor, mergeLocation(location, { schema: schema.items }), undefined, true)
10041014
}
10051015

10061016
if (key) {

test/array.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,69 @@ buildTest({
210210
foo: [1, 'string', {}, null]
211211
})
212212

213+
test('array items is a list of schema and additionalItems is true, just the described item is validated', (t) => {
214+
t.plan(1)
215+
216+
const schema = {
217+
type: 'object',
218+
properties: {
219+
foo: {
220+
type: 'array',
221+
items: [
222+
{
223+
type: 'string'
224+
}
225+
],
226+
additionalItems: true
227+
}
228+
}
229+
}
230+
231+
const stringify = build(schema)
232+
const result = stringify({
233+
foo: [
234+
'foo',
235+
'bar',
236+
1
237+
]
238+
})
239+
240+
t.equal(result, '{"foo":["foo","bar",1]}')
241+
})
242+
243+
test('array items is a list of schema and additionalItems is false', (t) => {
244+
t.plan(1)
245+
246+
const schema = {
247+
type: 'object',
248+
properties: {
249+
foo: {
250+
type: 'array',
251+
items: [
252+
{
253+
type: 'string'
254+
}
255+
],
256+
additionalItems: false
257+
}
258+
}
259+
}
260+
261+
const stringify = build(schema)
262+
263+
try {
264+
stringify({
265+
foo: [
266+
'foo',
267+
'bar'
268+
]
269+
})
270+
t.fail()
271+
} catch (error) {
272+
t.ok(/does not match schema definition./.test(error.message))
273+
}
274+
})
275+
213276
// https://github.com/fastify/fast-json-stringify/issues/279
214277
test('object array with anyOf and symbol', (t) => {
215278
t.plan(1)

0 commit comments

Comments
 (0)