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
  • Loading branch information
epoberezkin authored and andriyl committed Jun 16, 2021
1 parent df3922f commit eedb126
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
14 changes: 7 additions & 7 deletions lib/vocabularies/code.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type {AnySchema, SchemaMap} from "../types"
import type {SchemaCxt} from "../compile"
import type {KeywordCxt} from "../compile/validate"
import {CodeGen, _, or, not, nil, strConcat, getProperty, Code, Name} from "../compile/codegen"
import {alwaysValidSchema, Type} from "../compile/util"
import type KeywordCxt from "../compile/context"
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"

export function checkReportMissingProp(cxt: KeywordCxt, prop: string): void {
Expand All @@ -19,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 @@ -60,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 @@ -312,7 +312,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 @@ -347,6 +347,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 eedb126

Please sign in to comment.