Skip to content

Commit 95b15b6

Browse files
authoredFeb 24, 2022
Use root schemaEnv when resolving references in oneOf (#1901)
* Use root schemaEnv when resolving references in oneOf * Update discriminator test name
1 parent 6e53e43 commit 95b15b6

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed
 

‎lib/vocabularies/discriminator/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const def: CodeKeywordDefinition = {
6666
for (let i = 0; i < oneOf.length; i++) {
6767
let sch = oneOf[i]
6868
if (sch?.$ref && !schemaHasRulesButRef(sch, it.self.RULES)) {
69-
sch = resolveRef.call(it.self, it.schemaEnv, it.baseId, sch?.$ref)
69+
sch = resolveRef.call(it.self, it.schemaEnv.root, it.baseId, sch?.$ref)
7070
if (sch instanceof SchemaEnv) sch = sch.schema
7171
}
7272
const propSch = sch?.properties?.[tagName]

‎spec/discriminator.spec.ts

+82
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,88 @@ describe("discriminator keyword", function () {
159159
})
160160
})
161161

162+
describe("validation with deeply referenced schemas", () => {
163+
const schema = [
164+
{
165+
type: "object",
166+
properties: {
167+
container: {
168+
$ref: "#/definitions/Container",
169+
},
170+
},
171+
definitions: {
172+
BlockA: {
173+
type: "object",
174+
properties: {
175+
_type: {
176+
type: "string",
177+
enum: ["a"],
178+
},
179+
a: {type: "string"},
180+
},
181+
additionalProperties: false,
182+
required: ["_type"],
183+
title: "BlockA",
184+
},
185+
BlockB: {
186+
type: "object",
187+
properties: {
188+
_type: {
189+
type: "string",
190+
enum: ["b"],
191+
},
192+
b: {type: "string"},
193+
},
194+
additionalProperties: false,
195+
required: ["_type"],
196+
title: "BlockB",
197+
},
198+
Container: {
199+
type: "object",
200+
properties: {
201+
list: {
202+
type: "array",
203+
items: {
204+
oneOf: [
205+
{
206+
$ref: "#/definitions/BlockA",
207+
},
208+
{
209+
$ref: "#/definitions/BlockB",
210+
},
211+
],
212+
discriminator: {
213+
propertyName: "_type",
214+
},
215+
},
216+
},
217+
},
218+
},
219+
},
220+
},
221+
]
222+
223+
it("should validate data", () => {
224+
assertValid(schema, {
225+
container: {
226+
list: [
227+
{_type: "a", a: "foo"},
228+
{_type: "b", b: "bar"},
229+
],
230+
},
231+
})
232+
233+
assertInvalid(schema, {
234+
container: {
235+
list: [
236+
{_type: "a", b: "foo"},
237+
{_type: "b", b: "bar"},
238+
],
239+
},
240+
})
241+
})
242+
})
243+
162244
describe("valid schemas", () => {
163245
it("should have oneOf", () => {
164246
invalidSchema(

0 commit comments

Comments
 (0)
Please sign in to comment.