Skip to content

Commit

Permalink
Use root schemaEnv when resolving references in oneOf (#1901)
Browse files Browse the repository at this point in the history
* Use root schemaEnv when resolving references in oneOf

* Update discriminator test name
  • Loading branch information
asprouse committed Feb 24, 2022
1 parent 6e53e43 commit 95b15b6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/vocabularies/discriminator/index.ts
Expand Up @@ -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]
Expand Down
82 changes: 82 additions & 0 deletions spec/discriminator.spec.ts
Expand Up @@ -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(
Expand Down

0 comments on commit 95b15b6

Please sign in to comment.