Skip to content

Commit 207e90d

Browse files
committed
Fix type check in anyOf/oneOf error handlers
1 parent 438dcb1 commit 207e90d

4 files changed

Lines changed: 99 additions & 2 deletions

File tree

src/error-handlers/anyOf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const anyOfErrorHandler = async (normalizedErrors, instance, localization) => {
2020
const instanceLocation = Instance.uri(instance);
2121

2222
for (const alternative of anyOf) {
23-
const typeErrors = alternative[instanceLocation]["https://json-schema.org/keyword/type"];
23+
const typeErrors = alternative[instanceLocation]?.["https://json-schema.org/keyword/type"];
2424
const match = !typeErrors || Object.values(typeErrors).every((isValid) => isValid);
2525

2626
if (match) {

src/error-handlers/oneOf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const oneOfErrorHandler = async (normalizedErrors, instance, localization) => {
2121
let matchCount = 0;
2222

2323
for (const alternative of oneOf) {
24-
const typeErrors = alternative[instanceLocation]["https://json-schema.org/keyword/type"];
24+
const typeErrors = alternative[instanceLocation]?.["https://json-schema.org/keyword/type"];
2525
const match = !typeErrors || Object.values(typeErrors).every((isValid) => isValid);
2626

2727
if (match) {

src/test-suite/tests/anyOf.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,53 @@
207207
"schemaLocations": ["#/$defs/bar/maxLength"]
208208
}
209209
]
210+
},
211+
{
212+
"description": "anyOf with no declared type",
213+
"schema": {
214+
"anyOf": [
215+
{
216+
"properties": {
217+
"foo": { "type": "string" }
218+
}
219+
},
220+
{
221+
"properties": {
222+
"foo": { "type": "boolean" }
223+
}
224+
}
225+
]
226+
},
227+
"instance": { "foo": 42 },
228+
"errors": [
229+
{
230+
"messageId": "anyOf-message",
231+
"alternatives": [
232+
[
233+
{
234+
"messageId": "type-message",
235+
"messageParams": {
236+
"expectedTypes": { "or": ["string"] }
237+
},
238+
"instanceLocation": "#/foo",
239+
"schemaLocations": ["https://example.com/main#/anyOf/0/properties/foo/type"]
240+
}
241+
],
242+
[
243+
{
244+
"messageId": "type-message",
245+
"messageParams": {
246+
"expectedTypes": { "or": ["boolean"] }
247+
},
248+
"instanceLocation": "#/foo",
249+
"schemaLocations": ["https://example.com/main#/anyOf/1/properties/foo/type"]
250+
}
251+
]
252+
],
253+
"instanceLocation": "#",
254+
"schemaLocations": ["https://example.com/main#/anyOf"]
255+
}
256+
]
210257
}
211258
]
212259
}

src/test-suite/tests/oneOf.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,56 @@
272272
]
273273
}
274274
]
275+
},
276+
{
277+
"description": "oneOf with no declared type",
278+
"schema": {
279+
"oneOf": [
280+
{
281+
"properties": {
282+
"foo": { "type": "string" }
283+
}
284+
},
285+
{
286+
"properties": {
287+
"foo": { "type": "boolean" }
288+
}
289+
}
290+
]
291+
},
292+
"instance": { "foo": 42 },
293+
"errors": [
294+
{
295+
"messageId": "oneOf-message",
296+
"messageParams": {
297+
"matchCount": 0
298+
},
299+
"alternatives": [
300+
[
301+
{
302+
"messageId": "type-message",
303+
"messageParams": {
304+
"expectedTypes": { "or": ["string"] }
305+
},
306+
"instanceLocation": "#/foo",
307+
"schemaLocations": ["https://example.com/main#/oneOf/0/properties/foo/type"]
308+
}
309+
],
310+
[
311+
{
312+
"messageId": "type-message",
313+
"messageParams": {
314+
"expectedTypes": { "or": ["boolean"] }
315+
},
316+
"instanceLocation": "#/foo",
317+
"schemaLocations": ["https://example.com/main#/oneOf/1/properties/foo/type"]
318+
}
319+
]
320+
],
321+
"instanceLocation": "#",
322+
"schemaLocations": ["https://example.com/main#/oneOf"]
323+
}
324+
]
275325
}
276326
]
277327
}

0 commit comments

Comments
 (0)