Skip to content

Commit

Permalink
fix minContains: 0, closes #1819
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Nov 16, 2021
1 parent 62048a1 commit 2a407d7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/vocabularies/applicator/contains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ const def: CodeKeywordDefinition = {
const valid = gen.name("valid")
if (max === undefined && min === 1) {
validateItems(valid, () => gen.if(valid, () => gen.break()))
} else if (min === 0) {
gen.let(valid, true)
if (max !== undefined) gen.if(_`${data}.length > 0`, validateItemsWithCount)
} else {
gen.let(valid, false)
if (min === 0) {
gen.if(_`${data}.length > 0`, validateItemsWithCount, () => gen.assign(valid, true))
} else {
validateItemsWithCount()
}
validateItemsWithCount()
}
cxt.result(valid, () => cxt.reset())

Expand Down
19 changes: 19 additions & 0 deletions spec/issues/1819_mincontains.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import _Ajv from "../ajv2020"
import * as assert from "assert"

describe("`minContains: 0` without valid items (issue #1819)", () => {
const ajv = new _Ajv()

const schema = {
"type": "array",
"minContains": 0,
"maxContains": 1,
"contains": {"type": "number"}
}

const validate = ajv.compile(schema)

it("no items valid", () => assert.strictEqual(validate(["foo"]), true))
it("1 item valid", () => assert.strictEqual(validate(["foo", 1]), true))
it("2 items invalid", () => assert.strictEqual(validate(["foo", 1, 2]), false))
})

0 comments on commit 2a407d7

Please sign in to comment.