Skip to content

Commit 642e042

Browse files
authored
fix cutting off arrays with more than 100 items in anyOf and oneOf (#237)
* fix cutting off arrays with more than 100 items in anyOf and oneOf * fix cutting off arrays with more than 100 items in anyOf and oneOf * fix cutting off arrays with more than 100 items in anyOf and oneOf
1 parent 5aa872b commit 642e042

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
10791079
schema.anyOf.forEach((s, index) => {
10801080
var nestedResult = nested(laterCode, name, key, s, externalSchema, fullSchema, subKey !== '' ? subKey : 'i' + index, isArray)
10811081
code += `
1082-
${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(s, { depth: null })}, obj${accessor}))
1082+
${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(s, { depth: null, maxArrayLength: null })}, obj${accessor}))
10831083
${nestedResult.code}
10841084
`
10851085
laterCode = nestedResult.laterCode
@@ -1092,7 +1092,7 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
10921092
schema.oneOf.forEach((s, index) => {
10931093
var nestedResult = nested(laterCode, name, key, s, externalSchema, fullSchema, subKey !== '' ? subKey : 'i' + index, isArray)
10941094
code += `
1095-
${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(s, { depth: null })}, obj${accessor}))
1095+
${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(s, { depth: null, maxArrayLength: null })}, obj${accessor}))
10961096
${nestedResult.code}
10971097
`
10981098
laterCode = nestedResult.laterCode

test/anyof.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,29 @@ test('anyOf looks for all of the array items', (t) => {
435435
t.fail()
436436
}
437437
})
438+
439+
test('anyOf with enum with more than 100 entries', (t) => {
440+
t.plan(1)
441+
442+
const schema = {
443+
title: 'type array that may have any of declared items',
444+
type: 'array',
445+
items: {
446+
anyOf: [
447+
{
448+
type: 'string',
449+
enum: ['EUR', 'USD', ...(new Set([...new Array(200)].map(() => Math.random().toString(36).substr(2, 3)))).values()]
450+
},
451+
{ type: 'null' }
452+
]
453+
}
454+
}
455+
const stringify = build(schema)
456+
457+
try {
458+
const value = stringify(['EUR', 'USD', null])
459+
t.is(value, '["EUR","USD",null]')
460+
} catch (e) {
461+
t.fail()
462+
}
463+
})

test/oneof.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,29 @@ test('oneOf and $ref: multiple levels should throw at build.', (t) => {
397397
t.fail(e)
398398
}
399399
})
400+
401+
test('oneOf with enum with more than 100 entries', (t) => {
402+
t.plan(1)
403+
404+
const schema = {
405+
title: 'type array that may have one of declared items',
406+
type: 'array',
407+
items: {
408+
oneOf: [
409+
{
410+
type: 'string',
411+
enum: ['EUR', 'USD', ...(new Set([...new Array(200)].map(() => Math.random().toString(36).substr(2, 3)))).values()]
412+
},
413+
{ type: 'null' }
414+
]
415+
}
416+
}
417+
const stringify = build(schema)
418+
419+
try {
420+
const value = stringify(['EUR', 'USD', null])
421+
t.is(value, '["EUR","USD",null]')
422+
} catch (e) {
423+
t.fail()
424+
}
425+
})

0 commit comments

Comments
 (0)