Skip to content

Commit ed7cca7

Browse files
authored
Array should only have "length"-check if items is an Array (#570)
1 parent 63d2070 commit ed7cca7

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ function buildArray (location) {
563563
const arrayLength = obj.length
564564
`
565565

566-
if (!schema.additionalItems) {
566+
if (!schema.additionalItems && Array.isArray(itemsSchema)) {
567567
functionCode += `
568568
if (arrayLength > ${itemsSchema.length}) {
569569
throw new Error(\`Item at ${itemsSchema.length} does not match schema definition.\`)

test/array.test.js

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const test = require('tap').test
44
const validator = require('is-my-json-valid')
55
const build = require('..')
6+
const Ajv = require('ajv')
67

78
test('error on invalid largeArrayMechanism', (t) => {
89
t.plan(1)
@@ -265,7 +266,7 @@ test('array items is a list of schema and additionalItems is true, just the desc
265266
t.equal(result, '{"foo":["foo"]}')
266267
})
267268

268-
test('array items is a list of schema and additionalItems is false', (t) => {
269+
test('array items is a list of schema and additionalItems is false /1', (t) => {
269270
t.plan(1)
270271

271272
const schema = {
@@ -274,9 +275,7 @@ test('array items is a list of schema and additionalItems is false', (t) => {
274275
foo: {
275276
type: 'array',
276277
items: [
277-
{
278-
type: 'string'
279-
}
278+
{ type: 'string' }
280279
],
281280
additionalItems: false
282281
}
@@ -285,17 +284,55 @@ test('array items is a list of schema and additionalItems is false', (t) => {
285284

286285
const stringify = build(schema)
287286

288-
try {
289-
stringify({
290-
foo: [
291-
'foo',
292-
'bar'
293-
]
294-
})
295-
t.fail()
296-
} catch (error) {
297-
t.ok(/does not match schema definition./.test(error.message))
287+
t.throws(() => stringify({ foo: ['foo', 'bar'] }), new Error('Item at 1 does not match schema definition.'))
288+
})
289+
290+
test('array items is a list of schema and additionalItems is false /2', (t) => {
291+
t.plan(3)
292+
293+
const schema = {
294+
type: 'object',
295+
properties: {
296+
foo: {
297+
type: 'array',
298+
items: [
299+
{ type: 'string' },
300+
{ type: 'string' }
301+
],
302+
additionalItems: false
303+
}
304+
}
305+
}
306+
307+
const stringify = build(schema)
308+
309+
t.throws(() => stringify({ foo: [1, 'bar'] }), new Error('Item at 0 does not match schema definition.'))
310+
t.throws(() => stringify({ foo: ['foo', 1] }), new Error('Item at 1 does not match schema definition.'))
311+
t.throws(() => stringify({ foo: ['foo', 'bar', 'baz'] }), new Error('Item at 2 does not match schema definition.'))
312+
})
313+
314+
test('array items is a schema and additionalItems is false', (t) => {
315+
t.plan(2)
316+
317+
const schema = {
318+
type: 'object',
319+
properties: {
320+
foo: {
321+
type: 'array',
322+
items: { type: 'string' },
323+
additionalItems: false
324+
}
325+
}
298326
}
327+
328+
const stringify = build(schema)
329+
330+
// ajv ignores additionalItems if items is not an Array
331+
const ajv = new Ajv({ allErrors: true, strict: false })
332+
333+
const validate = ajv.compile(schema)
334+
t.same(stringify({ foo: ['foo', 'bar'] }), '{"foo":["foo","bar"]}')
335+
t.equal(validate({ foo: ['foo', 'bar'] }), true)
299336
})
300337

301338
// https://github.com/fastify/fast-json-stringify/issues/279

0 commit comments

Comments
 (0)