Skip to content

Commit

Permalink
fix incorrect missing property reporting with ownProperties option, c…
Browse files Browse the repository at this point in the history
…loses #1493
  • Loading branch information
epoberezkin committed Mar 19, 2021
1 parent 3ae14a3 commit 4f83021
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
9 changes: 4 additions & 5 deletions lib/vocabularies/code.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {AnySchema, SchemaMap} from "../types"
import type {SchemaCxt} from "../compile"
import type KeywordCxt from "../compile/context"
import {CodeGen, _, or, not, nil, strConcat, getProperty, Code, Name} from "../compile/codegen"
import {CodeGen, _, and, or, not, nil, strConcat, getProperty, Code, Name} from "../compile/codegen"
import {alwaysValidSchema} from "../compile/util"
import {Type} from "../compile/subschema"
import N from "../compile/names"
Expand All @@ -20,9 +20,8 @@ export function checkMissingProp(
missing: Name
): Code {
return or(
...properties.map(
(prop) =>
_`${noPropertyInData(gen, data, prop, opts.ownProperties)} && (${missing} = ${prop})`
...properties.map((prop) =>
and(noPropertyInData(gen, data, prop, opts.ownProperties), _`${missing} = ${prop}`)
)
)
}
Expand Down Expand Up @@ -61,7 +60,7 @@ export function noPropertyInData(
ownProperties?: boolean
): Code {
const cond = _`${data}${getProperty(property)} === undefined`
return ownProperties ? _`${cond} || !${isOwnProperty(gen, data, property)}` : cond
return ownProperties ? or(cond, not(isOwnProperty(gen, data, property))) : cond
}

export function allSchemaProperties(schemaMap?: SchemaMap): string[] {
Expand Down
29 changes: 28 additions & 1 deletion spec/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ describe("Validation errors", () => {
test(new _Ajv({$data: true}))
test(new _Ajv({$data: true, allErrors: true}))

function test(_ajv) {
function test(_ajv: Ajv) {
const schema = {
type: "object",
required: {$data: "0/req"},
Expand Down Expand Up @@ -348,6 +348,33 @@ describe("Validation errors", () => {
)
}
})

it("should include missing property with ownProperties option (issue #1493)", () => {
test(new _Ajv())
test(new _Ajv({ownProperties: true}))

function test(_ajv: Ajv): void {
const schema = {
type: "object",
required: ["a"],
properties: {
a: {type: "string"},
},
}

const validate = _ajv.compile(schema)
shouldBeValid(validate, {a: "abc"})
shouldBeInvalid(validate, {})
shouldBeError(
validate.errors?.[0],
"required",
"#/required",
"",
"should have required property 'a'",
{missingProperty: "a"}
)
}
})
})

describe('"dependencies" errors', () => {
Expand Down

0 comments on commit 4f83021

Please sign in to comment.