Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/error-handlers/anyOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,33 @@ const anyOfErrorHandler = async (normalizedErrors, instance, localization) => {
}

const alternatives = [];
const instanceLocation = Instance.uri(instance);

for (const alternative of anyOf) {
alternatives.push(await getErrors(alternative, instance, localization));
const typeErrors = alternative[instanceLocation]["https://json-schema.org/keyword/type"];
const match = !typeErrors || Object.values(typeErrors).every((isValid) => isValid);

if (match) {
alternatives.push(await getErrors(alternative, instance, localization));
}
}

errors.push({
message: localization.getAnyOfErrorMessage(),
alternatives: alternatives,
instanceLocation: Instance.uri(instance),
schemaLocations: [schemaLocation]
});
if (alternatives.length === 0) {
for (const alternative of anyOf) {
alternatives.push(await getErrors(alternative, instance, localization));
}
}

if (alternatives.length === 1) {
errors.push(...alternatives[0]);
} else {
errors.push({
message: localization.getAnyOfErrorMessage(),
alternatives: alternatives,
instanceLocation: Instance.uri(instance),
schemaLocations: [schemaLocation]
});
}
}

return errors;
Expand Down
44 changes: 31 additions & 13 deletions src/error-handlers/oneOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,44 @@ const oneOfErrorHandler = async (normalizedErrors, instance, localization) => {
}

const alternatives = [];
const instanceLocation = Instance.uri(instance);
let matchCount = 0;

for (const alternative of oneOf) {
const alternativeErrors = await getErrors(alternative, instance, localization);
if (alternativeErrors.length) {
const typeErrors = alternative[instanceLocation]["https://json-schema.org/keyword/type"];
const match = !typeErrors || Object.values(typeErrors).every((isValid) => isValid);

if (match) {
const alternativeErrors = await getErrors(alternative, instance, localization);
if (alternativeErrors.length) {
alternatives.push(alternativeErrors);
} else {
matchCount++;
}
}
}

if (matchCount === 0 && alternatives.length === 0) {
for (const alternative of oneOf) {
const alternativeErrors = await getErrors(alternative, instance, localization);
alternatives.push(alternativeErrors);
} else {
matchCount++;
}
}

/** @type ErrorObject */
const alternativeErrors = {
message: localization.getOneOfErrorMessage(matchCount),
instanceLocation: Instance.uri(instance),
schemaLocations: [schemaLocation]
};
if (alternatives.length) {
alternativeErrors.alternatives = alternatives;
if (alternatives.length === 1 && matchCount === 0) {
errors.push(...alternatives[0]);
} else {
/** @type ErrorObject */
const alternativeErrors = {
message: localization.getOneOfErrorMessage(matchCount),
instanceLocation: Instance.uri(instance),
schemaLocations: [schemaLocation]
};
if (alternatives.length) {
alternativeErrors.alternatives = alternatives;
}
errors.push(alternativeErrors);
}
errors.push(alternativeErrors);
}

return errors;
Expand Down
155 changes: 155 additions & 0 deletions src/test-suite/tests/anyOf.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,161 @@
},
"instance": 42,
"errors": []
},
{
"description": "anyOf single type match flattens errors (removes anyOf error message)",
"schema": {
"anyOf": [
{
"type": "string",
"maxLength": 2
},
{
"type": "number",
"minimum": 2
}
]
},
"instance": "foo",
"errors": [
{
"messageId": "maxLength-message",
"messageParams": {
"maxLength": 2
},
"instanceLocation": "#",
"schemaLocations": ["#/anyOf/0/maxLength"]
}
]
},
{
"description": "none of the alternatives match instance type, hence no filtering of alternatives",
"schema": {
"anyOf": [
{
"type": "string",
"maxLength": 2
},
{
"type": "number",
"minimum": 2
}
]
},
"instance": true,
"errors": [
{
"messageId": "anyOf-message",
"alternatives": [
[
{
"messageId": "type-message",
"messageParams": {
"expectedTypes": { "or": ["string"] }
},
"instanceLocation": "#",
"schemaLocations": ["#/anyOf/0/type"]
}
],
[
{
"messageId": "type-message",
"messageParams": {
"expectedTypes": { "or": ["number"] }
},
"instanceLocation": "#",
"schemaLocations": ["#/anyOf/1/type"]
}
]
],
"instanceLocation": "#",
"schemaLocations": ["#/anyOf"]
}
]
},
{
"description": "anyOf with multiple matching types does not collapse distinct errors",
"schema": {
"anyOf": [
{
"type": "string",
"pattern": "^[0-9]+$"
},
{
"type": "string",
"format": "email"
},
{
"type": "number",
"minimum": 2
}
]
},
"instance": "hello",
"errors": [
{
"messageId": "anyOf-message",
"alternatives": [
[
{
"messageId": "pattern-message",
"messageParams": {
"pattern": "^[0-9]+$"
},
"instanceLocation": "#",
"schemaLocations": ["#/anyOf/0/pattern"]
}
],
[
{
"messageId": "format-message",
"messageParams": {
"format": "email"
},
"instanceLocation": "#",
"schemaLocations": ["#/anyOf/1/format"]
}
]
],
"instanceLocation": "#",
"schemaLocations": ["#/anyOf"]
}
]
},
{
"description": "anyOf with $ref alternatives filters by type",
"compatibility": "2019",
"schema": {
"$defs": {
"bar": {
"type": "string",
"maxLength": 2
},
"baz": {
"type": "number",
"minimum": 2
}
},
"anyOf": [
{
"$ref": "#/$defs/bar"
},
{
"$ref": "#/$defs/baz"
}
]
},
"instance": "foo",
"errors": [
{
"messageId": "maxLength-message",
"messageParams": {
"maxLength": 2
},
"instanceLocation": "#",
"schemaLocations": ["#/$defs/bar/maxLength"]
}
]
}
]
}
Loading