From d7f91f6569b55bc241057f37fa1910c6998eb1ec Mon Sep 17 00:00:00 2001 From: Andrew Sprouse Date: Wed, 16 Feb 2022 16:24:29 -0500 Subject: [PATCH 1/2] Use root schemaEnv when resolving references in oneOf --- lib/vocabularies/discriminator/index.ts | 2 +- spec/discriminator.spec.ts | 82 +++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) 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..48b4e6f28 100644 --- a/spec/discriminator.spec.ts +++ b/spec/discriminator.spec.ts @@ -159,6 +159,88 @@ describe("discriminator keyword", function () { }) }) + describe("validation with 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( From 39f7c5f8a270ee9db7b7c5a820cd167411dcd1ab Mon Sep 17 00:00:00 2001 From: Andrew Sprouse Date: Wed, 16 Feb 2022 17:16:31 -0500 Subject: [PATCH 2/2] Update discriminator test name --- spec/discriminator.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/discriminator.spec.ts b/spec/discriminator.spec.ts index 48b4e6f28..28ff12146 100644 --- a/spec/discriminator.spec.ts +++ b/spec/discriminator.spec.ts @@ -159,7 +159,7 @@ describe("discriminator keyword", function () { }) }) - describe("validation with referenced schemas", () => { + describe("validation with deeply referenced schemas", () => { const schema = [ { type: "object",