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
64 changes: 33 additions & 31 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/minLength"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should be at least ${Schema.value(keyword)} characters`,
message: localization.getMinLengthErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -131,7 +131,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/maxLength"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should be atmost ${Schema.value(keyword)} characters long.`,
message: localization.getMaxLengthErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand Down Expand Up @@ -171,7 +171,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/maximum"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should be less than or equal to ${Schema.value(keyword)}.`,
message: localization.getMaximumErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -191,7 +191,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/minimum"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should be greater than or equal to ${Schema.value(keyword)}.`,
message: localization.getMinimumErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -211,7 +211,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/exclusiveMinimum"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should be greater than ${Schema.value(keyword)}.`,
message: localization.getExclusiveMinimumErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -231,7 +231,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/exclusiveMaximum"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should be less than ${Schema.value(keyword)}.`,
message: localization.getExclusiveMaximumErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -256,7 +256,7 @@ const errorHandlers = [
required.delete(propertyName);
}
errors.push({
message: `"${Instance.uri(instance)}" is missing required property(s): ${[...required].join(", ")}.`,
message: localization.getRequiredErrorMessage(Instance.uri(instance), [...required]),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -276,7 +276,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/multipleOf"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should be of multiple of ${Schema.value(keyword)}.`,
message: localization.getMultipleOfErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -296,7 +296,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/maxProperties"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should have maximum ${Schema.value(keyword)} properties.`,
message: localization.getMaxPropertiesErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -316,7 +316,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/minProperties"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should have minimum ${Schema.value(keyword)} properties.`,
message: localization.getMinPropertiesErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -336,7 +336,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/const"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should be equal to ${Schema.value(keyword)}.`,
message: localization.getConstErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand Down Expand Up @@ -366,23 +366,25 @@ const errorHandlers = [
weight: leven(value, currentValue)
}))
.sort((a, b) => a.weight - b.weight)[0];

let suggestion = "";
let message;
if (
allowedValues.length === 1
|| (bestMatch && bestMatch.weight < bestMatch.value.length)
) {
suggestion = ` Did you mean "${bestMatch.value}"?`;
errors.push({
message: `Unexpected value "${currentValue}". ${suggestion}`,
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
message = localization.getEnumErrorMessage({
variant: "suggestion",
instanceValue: currentValue,
suggestion: bestMatch.value
});
} else {
message = localization.getEnumErrorMessage({
variant: "fallback",
instanceValue: currentValue,
allowedValues: allowedValues
});
continue;
}

errors.push({
message: `Unexpected value "${currentValue}". Expected one of: ${allowedValues.join(",")}.`,
message,
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -402,7 +404,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/maxItems"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should contain maximum ${Schema.value(keyword)} items in the array.`,
message: localization.getMaxItemsErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -422,7 +424,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/minItems"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should contain minimum ${Schema.value(keyword)} items in the array.`,
message: localization.getMinItemsErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -442,7 +444,7 @@ const errorHandlers = [
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/uniqueItems"]) {
if (!normalizedErrors["https://json-schema.org/keyword/uniqueItems"][schemaLocation]) {
errors.push({
message: `The instance should have unique items in the array.`,
message: localization.getUniqueItemsErrorMessage(),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -462,7 +464,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/format"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should match the format: ${Schema.value(keyword)}.`,
message: localization.getFormatErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -482,7 +484,7 @@ const errorHandlers = [
if (!normalizedErrors["https://json-schema.org/keyword/pattern"][schemaLocation]) {
const keyword = await getSchema(schemaLocation);
errors.push({
message: `The instance should match the pattern: ${Schema.value(keyword)}.`,
message: localization.getPatternErrorMessage(Schema.value(keyword)),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -499,7 +501,7 @@ const errorHandlers = [
if (normalizedErrors["https://json-schema.org/keyword/contains"]) {
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/contains"]) {
errors.push({
message: `A required value is missing from the list`,
message: localization.getContainsErrorMessage(),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -522,7 +524,7 @@ const errorHandlers = [
if (normalizedErrors["https://json-schema.org/keyword/not"]) {
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/not"]) {
errors.push({
message: `The instance is not allowed to be used in this schema.`,
message: localization.getNotErrorMessage(),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -539,9 +541,9 @@ const errorHandlers = [
if (normalizedErrors["https://json-schema.org/validation"]) {
for (const schemaLocation in normalizedErrors["https://json-schema.org/validation"]) {
if (!normalizedErrors["https://json-schema.org/validation"][schemaLocation] && schemaLocation.endsWith("/additionalProperties")) {
const notAllowedValue = Instance.uri(instance).split("/").pop();
const notAllowedValue = /** @type string */(Instance.uri(instance).split("/").pop());
errors.push({
message: `The property "${notAllowedValue}" is not allowed.`,
message: localization.getAdditionalPropertiesErrorMessage(notAllowedValue),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand All @@ -567,7 +569,7 @@ const errorHandlers = [

if (missing.length > 0) {
errors.push({
message: `Property "${propertyName}" requires property(s): ${missing.join(", ")}.`,
message: localization.getDependentRequiredErrorMessage(propertyName, [...missing]),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand Down
Loading