diff --git a/lib/vocabularies/discriminator/index.ts b/lib/vocabularies/discriminator/index.ts index ec00064f9..98f0f8cfb 100644 --- a/lib/vocabularies/discriminator/index.ts +++ b/lib/vocabularies/discriminator/index.ts @@ -66,7 +66,7 @@ const def: CodeKeywordDefinition = { for (let i = 0; i < oneOf.length; i++) { let sch = oneOf[i] if (sch?.$ref && !schemaHasRulesButRef(sch, it.self.RULES)) { - sch = resolveRef.call(it.self, it.schemaEnv, it.baseId, sch?.$ref) + sch = resolveRef.call(it.self, it.schemaEnv.root, it.baseId, sch?.$ref) if (sch instanceof SchemaEnv) sch = sch.schema } const propSch = sch?.properties?.[tagName] diff --git a/spec/discriminator.spec.ts b/spec/discriminator.spec.ts index e44d60de0..28ff12146 100644 --- a/spec/discriminator.spec.ts +++ b/spec/discriminator.spec.ts @@ -159,6 +159,88 @@ describe("discriminator keyword", function () { }) }) + describe("validation with deeply referenced schemas", () => { + const schema = [ + { + type: "object", + properties: { + container: { + $ref: "#/definitions/Container", + }, + }, + definitions: { + BlockA: { + type: "object", + properties: { + _type: { + type: "string", + enum: ["a"], + }, + a: {type: "string"}, + }, + additionalProperties: false, + required: ["_type"], + title: "BlockA", + }, + BlockB: { + type: "object", + properties: { + _type: { + type: "string", + enum: ["b"], + }, + b: {type: "string"}, + }, + additionalProperties: false, + required: ["_type"], + title: "BlockB", + }, + Container: { + type: "object", + properties: { + list: { + type: "array", + items: { + oneOf: [ + { + $ref: "#/definitions/BlockA", + }, + { + $ref: "#/definitions/BlockB", + }, + ], + discriminator: { + propertyName: "_type", + }, + }, + }, + }, + }, + }, + }, + ] + + it("should validate data", () => { + assertValid(schema, { + container: { + list: [ + {_type: "a", a: "foo"}, + {_type: "b", b: "bar"}, + ], + }, + }) + + assertInvalid(schema, { + container: { + list: [ + {_type: "a", b: "foo"}, + {_type: "b", b: "bar"}, + ], + }, + }) + }) + }) + describe("valid schemas", () => { it("should have oneOf", () => { invalidSchema(