Skip to content

Commit 0beb3f2

Browse files
committed
handler more case of anyOf
1 parent 3805b9c commit 0beb3f2

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

src/error-handlers/anyOf.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ const anyOfErrorHandler = async (normalizedErrors, instance, localization) => {
165165
continue;
166166
}
167167

168-
// TODO: Handle alternatives with const
169-
// TODO: Handle alternatives with enum
170-
// TODO: Handle null alternatives
171-
// TODO: Handle boolean alternatives
172168
// TODO: Handle string alternatives
173169
// TODO: Handle array alternatives
174170
// TODO: Handle alternatives without a type

src/keyword-error-message.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ describe("Error messages", async () => {
856856
{
857857
schemaLocation: "https://example.com/main#/anyOf",
858858
instanceLocation: "#",
859-
message: localization.getTypeErrorMessage(["string", "number"], "boolean")
859+
message: localization.getEnumErrorMessage({ allowedTypes: ["string", "number"] }, false)
860860
}
861861
]);
862862
});

src/localization.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,23 @@ export class Localization {
239239
* @returns {string}
240240
*/
241241
getEnumErrorMessage(constraints, currentValue) {
242-
if (constraints.allowedValues) {
242+
/** @type {"suggestion" | "types" | "values" | "both"} */
243+
let variant = "suggestion";
244+
245+
/** @type string */
246+
let allowedValues = "";
247+
248+
/** @type string */
249+
let expectedTypes = "";
250+
251+
const instanceValue = JSON.stringify(currentValue);
252+
253+
if (constraints.allowedValues && constraints.allowedValues.length > 0 && constraints.allowedTypes?.length === 0) {
243254
const bestMatch = constraints.allowedValues
244255
.map((value) => {
245256
const r = {
246257
value: JSON.stringify(value),
247-
weight: leven(JSON.stringify(value), JSON.stringify(currentValue))
258+
weight: leven(JSON.stringify(value), instanceValue)
248259
};
249260
return r;
250261
})
@@ -254,19 +265,27 @@ export class Localization {
254265
return this._formatMessage("enum-error", {
255266
variant: "suggestion",
256267
suggestion: bestMatch.value,
257-
instanceValue: JSON.stringify(currentValue)
258-
});
259-
} else {
260-
return this._formatMessage("enum-error", {
261-
variant: "fallback",
262-
allowedValues: new Intl.ListFormat(this.locale, { type: "disjunction" })
263-
.format(constraints.allowedValues.map((value) => JSON.stringify(value))),
264-
instanceValue: JSON.stringify(currentValue)
268+
instanceValue
265269
});
266270
}
271+
272+
variant = "values";
273+
allowedValues = new Intl.ListFormat(this.locale, { type: "disjunction" })
274+
.format(constraints.allowedValues.map((value) => JSON.stringify(value)));
267275
}
268276

269-
return "";
277+
if (constraints.allowedTypes && constraints.allowedTypes.length > 0) {
278+
variant = variant === "values" ? "both" : "types";
279+
expectedTypes = new Intl.ListFormat(this.locale, { type: "disjunction" })
280+
.format(constraints.allowedTypes.map((value) => JSON.stringify(value)));
281+
}
282+
283+
return this._formatMessage("enum-error", {
284+
variant,
285+
allowedValues,
286+
expectedTypes,
287+
instanceValue
288+
});
270289
}
271290

272291
/** @type () => string */

src/translations/en-US.ftl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Non-type specific messages
22
type-error = The instance should be of type {$expected} but found {$actual}.
33
const-error = The instance should be equal to {$expectedValue}.
4-
enum-error = { $variant ->
5-
[suggestion] Unexpected value {$instanceValue}. Did you mean {$suggestion}?
6-
*[fallback] Unexpected value {$instanceValue}. Expected one of: {$allowedValues}.
4+
enum-error = Unexpected value {$instanceValue}. { $variant ->
5+
[types] Expected a {$expectedTypes}.
6+
[values] Expected one of: ${allowedValues}.
7+
[both] Expected a type of {$expectedTypes}, or one of: ${allowedValues}.
8+
[suggestion] Did you mean {$suggestion}?
79
}
810

911
# String messages

0 commit comments

Comments
 (0)