Skip to content

Commit

Permalink
Merge pull request #1519 from asaid-0/fix-1509
Browse files Browse the repository at this point in the history
Add error path to strict mode tuple warning
  • Loading branch information
epoberezkin authored Mar 29, 2021
2 parents f3d29b4 + cbd2718 commit cf8d31f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
7 changes: 4 additions & 3 deletions lib/vocabularies/applicator/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ export function validateTuple(
})

function checkStrictTuple(sch: AnySchemaObject): void {
const {opts, errSchemaPath} = it
const l = schArr.length
const fullTuple = l === sch.minItems && (l === sch.maxItems || sch[extraItems] === false)
if (it.opts.strictTuples && !fullTuple) {
const msg = `"${keyword}" is ${l}-tuple, but minItems or maxItems/${extraItems} are not specified or different`
checkStrictMode(it, msg, it.opts.strictTuples)
if (opts.strictTuples && !fullTuple) {
const msg = `"${keyword}" is ${l}-tuple, but minItems or maxItems/${extraItems} are not specified or different at path "${errSchemaPath}"`
checkStrictMode(it, msg, opts.strictTuples)
}
}
}
Expand Down
41 changes: 28 additions & 13 deletions spec/options/strict.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,34 +208,49 @@ describe("strict mode", () => {

//@ts-expect-error
const badSchema1: JSONSchemaType<MyTuple> = {
type: "array",
items: [{type: "string"}, {type: "number"}],
additionalItems: false,
type: "object",
properties: {
test: {
type: "array",
items: [{type: "string"}, {type: "number"}],
additionalItems: false,
},
},
}
should.throw(() => {
ajv.compile(badSchema1)
}, / minItems or maxItems\/additionalItems are not specified or different/)
}, / minItems or maxItems\/additionalItems are not specified or different at path "#\/properties\/test"/)

//@ts-expect-error
const badSchema2: JSONSchemaType<MyTuple> = {
type: "array",
items: [{type: "string"}, {type: "number"}],
minItems: 2,
type: "object",
properties: {
test: {
type: "array",
items: [{type: "string"}, {type: "number"}],
minItems: 2,
},
},
}
should.throw(() => {
ajv.compile(badSchema2)
}, / minItems or maxItems\/additionalItems are not specified or different/)
}, / minItems or maxItems\/additionalItems are not specified or different at path "#\/properties\/test"/)

//@ts-expect-error
const badSchema3: JSONSchemaType<MyTuple> = {
type: "array",
items: [{type: "string"}, {type: "number"}],
minItems: 2,
maxItems: 3,
type: "object",
properties: {
test: {
type: "array",
items: [{type: "string"}, {type: "number"}],
minItems: 2,
maxItems: 3,
},
},
}
should.throw(() => {
ajv.compile(badSchema3)
}, / minItems or maxItems\/additionalItems are not specified or different/)
}, / minItems or maxItems\/additionalItems are not specified or different at path "#\/properties\/test"/)
})
})

Expand Down

0 comments on commit cf8d31f

Please sign in to comment.