Skip to content

Commit c74f8c9

Browse files
authored
Merge pull request #43 from arpitkuriyal/localisation
Localisation
2 parents 12ad849 + 061d229 commit c74f8c9

File tree

5 files changed

+261
-67
lines changed

5 files changed

+261
-67
lines changed

src/index.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const errorHandlers = [
111111
if (!normalizedErrors["https://json-schema.org/keyword/minLength"][schemaLocation]) {
112112
const keyword = await getSchema(schemaLocation);
113113
errors.push({
114-
message: `The instance should be at least ${Schema.value(keyword)} characters`,
114+
message: localization.getMinLengthErrorMessage(Schema.value(keyword)),
115115
instanceLocation: Instance.uri(instance),
116116
schemaLocation: schemaLocation
117117
});
@@ -131,7 +131,7 @@ const errorHandlers = [
131131
if (!normalizedErrors["https://json-schema.org/keyword/maxLength"][schemaLocation]) {
132132
const keyword = await getSchema(schemaLocation);
133133
errors.push({
134-
message: `The instance should be atmost ${Schema.value(keyword)} characters long.`,
134+
message: localization.getMaxLengthErrorMessage(Schema.value(keyword)),
135135
instanceLocation: Instance.uri(instance),
136136
schemaLocation: schemaLocation
137137
});
@@ -171,7 +171,7 @@ const errorHandlers = [
171171
if (!normalizedErrors["https://json-schema.org/keyword/maximum"][schemaLocation]) {
172172
const keyword = await getSchema(schemaLocation);
173173
errors.push({
174-
message: `The instance should be less than or equal to ${Schema.value(keyword)}.`,
174+
message: localization.getMaximumErrorMessage(Schema.value(keyword)),
175175
instanceLocation: Instance.uri(instance),
176176
schemaLocation: schemaLocation
177177
});
@@ -191,7 +191,7 @@ const errorHandlers = [
191191
if (!normalizedErrors["https://json-schema.org/keyword/minimum"][schemaLocation]) {
192192
const keyword = await getSchema(schemaLocation);
193193
errors.push({
194-
message: `The instance should be greater than or equal to ${Schema.value(keyword)}.`,
194+
message: localization.getMinimumErrorMessage(Schema.value(keyword)),
195195
instanceLocation: Instance.uri(instance),
196196
schemaLocation: schemaLocation
197197
});
@@ -211,7 +211,7 @@ const errorHandlers = [
211211
if (!normalizedErrors["https://json-schema.org/keyword/exclusiveMinimum"][schemaLocation]) {
212212
const keyword = await getSchema(schemaLocation);
213213
errors.push({
214-
message: `The instance should be greater than ${Schema.value(keyword)}.`,
214+
message: localization.getExclusiveMinimumErrorMessage(Schema.value(keyword)),
215215
instanceLocation: Instance.uri(instance),
216216
schemaLocation: schemaLocation
217217
});
@@ -231,7 +231,7 @@ const errorHandlers = [
231231
if (!normalizedErrors["https://json-schema.org/keyword/exclusiveMaximum"][schemaLocation]) {
232232
const keyword = await getSchema(schemaLocation);
233233
errors.push({
234-
message: `The instance should be less than ${Schema.value(keyword)}.`,
234+
message: localization.getExclusiveMaximumErrorMessage(Schema.value(keyword)),
235235
instanceLocation: Instance.uri(instance),
236236
schemaLocation: schemaLocation
237237
});
@@ -256,7 +256,7 @@ const errorHandlers = [
256256
required.delete(propertyName);
257257
}
258258
errors.push({
259-
message: `"${Instance.uri(instance)}" is missing required property(s): ${[...required].join(", ")}.`,
259+
message: localization.getRequiredErrorMessage(Instance.uri(instance), [...required]),
260260
instanceLocation: Instance.uri(instance),
261261
schemaLocation: schemaLocation
262262
});
@@ -276,7 +276,7 @@ const errorHandlers = [
276276
if (!normalizedErrors["https://json-schema.org/keyword/multipleOf"][schemaLocation]) {
277277
const keyword = await getSchema(schemaLocation);
278278
errors.push({
279-
message: `The instance should be of multiple of ${Schema.value(keyword)}.`,
279+
message: localization.getMultipleOfErrorMessage(Schema.value(keyword)),
280280
instanceLocation: Instance.uri(instance),
281281
schemaLocation: schemaLocation
282282
});
@@ -296,7 +296,7 @@ const errorHandlers = [
296296
if (!normalizedErrors["https://json-schema.org/keyword/maxProperties"][schemaLocation]) {
297297
const keyword = await getSchema(schemaLocation);
298298
errors.push({
299-
message: `The instance should have maximum ${Schema.value(keyword)} properties.`,
299+
message: localization.getMaxPropertiesErrorMessage(Schema.value(keyword)),
300300
instanceLocation: Instance.uri(instance),
301301
schemaLocation: schemaLocation
302302
});
@@ -316,7 +316,7 @@ const errorHandlers = [
316316
if (!normalizedErrors["https://json-schema.org/keyword/minProperties"][schemaLocation]) {
317317
const keyword = await getSchema(schemaLocation);
318318
errors.push({
319-
message: `The instance should have minimum ${Schema.value(keyword)} properties.`,
319+
message: localization.getMinPropertiesErrorMessage(Schema.value(keyword)),
320320
instanceLocation: Instance.uri(instance),
321321
schemaLocation: schemaLocation
322322
});
@@ -336,7 +336,7 @@ const errorHandlers = [
336336
if (!normalizedErrors["https://json-schema.org/keyword/const"][schemaLocation]) {
337337
const keyword = await getSchema(schemaLocation);
338338
errors.push({
339-
message: `The instance should be equal to ${Schema.value(keyword)}.`,
339+
message: localization.getConstErrorMessage(Schema.value(keyword)),
340340
instanceLocation: Instance.uri(instance),
341341
schemaLocation: schemaLocation
342342
});
@@ -366,23 +366,25 @@ const errorHandlers = [
366366
weight: leven(value, currentValue)
367367
}))
368368
.sort((a, b) => a.weight - b.weight)[0];
369-
370-
let suggestion = "";
369+
let message;
371370
if (
372371
allowedValues.length === 1
373372
|| (bestMatch && bestMatch.weight < bestMatch.value.length)
374373
) {
375-
suggestion = ` Did you mean "${bestMatch.value}"?`;
376-
errors.push({
377-
message: `Unexpected value "${currentValue}". ${suggestion}`,
378-
instanceLocation: Instance.uri(instance),
379-
schemaLocation: schemaLocation
374+
message = localization.getEnumErrorMessage({
375+
variant: "suggestion",
376+
instanceValue: currentValue,
377+
suggestion: bestMatch.value
378+
});
379+
} else {
380+
message = localization.getEnumErrorMessage({
381+
variant: "fallback",
382+
instanceValue: currentValue,
383+
allowedValues: allowedValues
380384
});
381-
continue;
382385
}
383-
384386
errors.push({
385-
message: `Unexpected value "${currentValue}". Expected one of: ${allowedValues.join(",")}.`,
387+
message,
386388
instanceLocation: Instance.uri(instance),
387389
schemaLocation: schemaLocation
388390
});
@@ -402,7 +404,7 @@ const errorHandlers = [
402404
if (!normalizedErrors["https://json-schema.org/keyword/maxItems"][schemaLocation]) {
403405
const keyword = await getSchema(schemaLocation);
404406
errors.push({
405-
message: `The instance should contain maximum ${Schema.value(keyword)} items in the array.`,
407+
message: localization.getMaxItemsErrorMessage(Schema.value(keyword)),
406408
instanceLocation: Instance.uri(instance),
407409
schemaLocation: schemaLocation
408410
});
@@ -422,7 +424,7 @@ const errorHandlers = [
422424
if (!normalizedErrors["https://json-schema.org/keyword/minItems"][schemaLocation]) {
423425
const keyword = await getSchema(schemaLocation);
424426
errors.push({
425-
message: `The instance should contain minimum ${Schema.value(keyword)} items in the array.`,
427+
message: localization.getMinItemsErrorMessage(Schema.value(keyword)),
426428
instanceLocation: Instance.uri(instance),
427429
schemaLocation: schemaLocation
428430
});
@@ -442,7 +444,7 @@ const errorHandlers = [
442444
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/uniqueItems"]) {
443445
if (!normalizedErrors["https://json-schema.org/keyword/uniqueItems"][schemaLocation]) {
444446
errors.push({
445-
message: `The instance should have unique items in the array.`,
447+
message: localization.getUniqueItemsErrorMessage(),
446448
instanceLocation: Instance.uri(instance),
447449
schemaLocation: schemaLocation
448450
});
@@ -462,7 +464,7 @@ const errorHandlers = [
462464
if (!normalizedErrors["https://json-schema.org/keyword/format"][schemaLocation]) {
463465
const keyword = await getSchema(schemaLocation);
464466
errors.push({
465-
message: `The instance should match the format: ${Schema.value(keyword)}.`,
467+
message: localization.getFormatErrorMessage(Schema.value(keyword)),
466468
instanceLocation: Instance.uri(instance),
467469
schemaLocation: schemaLocation
468470
});
@@ -482,7 +484,7 @@ const errorHandlers = [
482484
if (!normalizedErrors["https://json-schema.org/keyword/pattern"][schemaLocation]) {
483485
const keyword = await getSchema(schemaLocation);
484486
errors.push({
485-
message: `The instance should match the pattern: ${Schema.value(keyword)}.`,
487+
message: localization.getPatternErrorMessage(Schema.value(keyword)),
486488
instanceLocation: Instance.uri(instance),
487489
schemaLocation: schemaLocation
488490
});
@@ -499,7 +501,7 @@ const errorHandlers = [
499501
if (normalizedErrors["https://json-schema.org/keyword/contains"]) {
500502
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/contains"]) {
501503
errors.push({
502-
message: `A required value is missing from the list`,
504+
message: localization.getContainsErrorMessage(),
503505
instanceLocation: Instance.uri(instance),
504506
schemaLocation: schemaLocation
505507
});
@@ -522,7 +524,7 @@ const errorHandlers = [
522524
if (normalizedErrors["https://json-schema.org/keyword/not"]) {
523525
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/not"]) {
524526
errors.push({
525-
message: `The instance is not allowed to be used in this schema.`,
527+
message: localization.getNotErrorMessage(),
526528
instanceLocation: Instance.uri(instance),
527529
schemaLocation: schemaLocation
528530
});
@@ -539,9 +541,9 @@ const errorHandlers = [
539541
if (normalizedErrors["https://json-schema.org/validation"]) {
540542
for (const schemaLocation in normalizedErrors["https://json-schema.org/validation"]) {
541543
if (!normalizedErrors["https://json-schema.org/validation"][schemaLocation] && schemaLocation.endsWith("/additionalProperties")) {
542-
const notAllowedValue = Instance.uri(instance).split("/").pop();
544+
const notAllowedValue = /** @type string */(Instance.uri(instance).split("/").pop());
543545
errors.push({
544-
message: `The property "${notAllowedValue}" is not allowed.`,
546+
message: localization.getAdditionalPropertiesErrorMessage(notAllowedValue),
545547
instanceLocation: Instance.uri(instance),
546548
schemaLocation: schemaLocation
547549
});
@@ -567,7 +569,7 @@ const errorHandlers = [
567569

568570
if (missing.length > 0) {
569571
errors.push({
570-
message: `Property "${propertyName}" requires property(s): ${missing.join(", ")}.`,
572+
message: localization.getDependentRequiredErrorMessage(propertyName, [...missing]),
571573
instanceLocation: Instance.uri(instance),
572574
schemaLocation: schemaLocation
573575
});

0 commit comments

Comments
 (0)