diff --git a/lib/compile/util.ts b/lib/compile/util.ts index e6a40468a..933535c98 100644 --- a/lib/compile/util.ts +++ b/lib/compile/util.ts @@ -128,7 +128,7 @@ export const mergeEvaluated: MergeEvaluated = { gen.if( _`${from} === true`, () => gen.assign(to, true), - () => gen.code(_`Object.assign(${to}, ${from})`) + () => gen.assign(to, _`${to} || {}`).code(_`Object.assign(${to}, ${from})`) ) }), mergeToName: (gen, from, to) => diff --git a/spec/issues/1515_evaluated_properties_nested_anyof.spec.ts b/spec/issues/1515_evaluated_properties_nested_anyof.spec.ts new file mode 100644 index 000000000..b9c6597cc --- /dev/null +++ b/spec/issues/1515_evaluated_properties_nested_anyof.spec.ts @@ -0,0 +1,28 @@ +import _Ajv from "../ajv2019" +import * as assert from "assert" + +describe("tracking evaluated properties with nested anyOf", () => { + it("should initialize evaluated properties", () => { + const ajv = new _Ajv() + + const schema = { + type: "object", + anyOf: [ + { + required: ["foo"], + properties: {foo: {}}, + }, + { + anyOf: [ + { + properties: {bar: {}}, + }, + ], + }, + ], + } + + const validate = ajv.compile(schema) + assert.strictEqual(validate({bar: 1}), true) + }) +})