Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use root schemaEnv when resolving references in oneOf #1901

Merged
merged 2 commits into from Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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