Skip to content

Commit 33eb965

Browse files
committed
remove pattern from the stringHandler
1 parent 3e4e441 commit 33eb965

File tree

7 files changed

+48
-33
lines changed

7 files changed

+48
-33
lines changed

src/error-handlers/pattern.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { getSchema } from "@hyperjump/json-schema/experimental";
2+
import * as Schema from "@hyperjump/browser";
3+
import * as Instance from "@hyperjump/json-schema/instance/experimental";
4+
5+
/**
6+
* @import { ErrorHandler, ErrorObject } from "../index.d.ts"
7+
*/
8+
9+
/** @type ErrorHandler */
10+
const pattern = async (normalizedErrors, instance, localization) => {
11+
/** @type ErrorObject[] */
12+
const errors = [];
13+
14+
if (normalizedErrors["https://json-schema.org/keyword/pattern"]) {
15+
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/pattern"]) {
16+
if (!normalizedErrors["https://json-schema.org/keyword/pattern"][schemaLocation]) {
17+
const keyword = await getSchema(schemaLocation);
18+
errors.push({
19+
message: localization.getPatternErrorMessage(Schema.value(keyword)),
20+
instanceLocation: Instance.uri(instance),
21+
schemaLocation: schemaLocation
22+
});
23+
}
24+
}
25+
}
26+
27+
return errors;
28+
};
29+
export default pattern;

src/error-handlers/string-handler.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@ const stringHandler = async (normalizedErrors, instance, localization) => {
3737
constraints.maxLength = Math.min(constraints.maxLength ?? Number.MAX_VALUE, maxLength);
3838
}
3939

40-
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/pattern"]) {
41-
if (!normalizedErrors["https://json-schema.org/keyword/pattern"][schemaLocation]) {
42-
failedSchemaLocations.push(schemaLocation);
43-
}
44-
45-
const keyword = await getSchema(schemaLocation);
46-
/** @type string */
47-
const pattern = Schema.value(keyword);
48-
constraints.pattern = pattern;
49-
}
50-
5140
if (failedSchemaLocations.length > 0) {
5241
return [
5342
{

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ import numberRangeHandler from "./error-handlers/number-range-handler.js";
6161
import requiredErrorHandler from "./error-handlers/required.js";
6262
import typeErrorHandler from "./error-handlers/type.js";
6363
import uniqueItemsErrorHandler from "./error-handlers/uniqueItems.js";
64-
import stringHandler from "./error-handlers/string-handler.js";
64+
import stringErrorHandler from "./error-handlers/string-handler.js";
65+
import patternErrorHandler from "./error-handlers/pattern.js";
6566

6667
/**
6768
* @import { betterJsonSchemaErrors } from "./index.d.ts"
@@ -131,7 +132,8 @@ addErrorHandler(numberRangeHandler);
131132
addErrorHandler(requiredErrorHandler);
132133
addErrorHandler(typeErrorHandler);
133134
addErrorHandler(uniqueItemsErrorHandler);
134-
addErrorHandler(stringHandler);
135+
addErrorHandler(stringErrorHandler);
136+
addErrorHandler(patternErrorHandler);
135137

136138
export { setNormalizationHandler } from "./normalized-output.js";
137139
export { addErrorHandler } from "./error-handling.js";

src/keyword-error-message.test.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ describe("Error messages", async () => {
614614
{
615615
schemaLocation: "https://example.com/main#/pattern",
616616
instanceLocation: "#",
617-
message: localization.getStringErrorMessage({ pattern: "^[a-z]+$" })
617+
message: localization.getPatternErrorMessage("^[a-z]+$")
618618
}
619619
]);
620620
});
@@ -883,7 +883,7 @@ describe("Error messages", async () => {
883883
{
884884
schemaLocation: "https://example.com/main#/anyOf/1/properties/ID/pattern",
885885
instanceLocation: "#/ID",
886-
message: localization.getStringErrorMessage({ pattern: "^[0-9\\-]+$" })
886+
message: localization.getPatternErrorMessage("^[0-9\\-]+$")
887887
}
888888
]);
889889
});
@@ -1355,7 +1355,7 @@ describe("Error messages", async () => {
13551355
expect(result.errors).to.eql([
13561356
{
13571357
instanceLocation: "#/Foo",
1358-
message: localization.getStringErrorMessage({ pattern: "^[a-z]*$" }),
1358+
message: localization.getPatternErrorMessage("^[a-z]*$"),
13591359
schemaLocation: "https://example.com/main#/propertyNames/pattern"
13601360
}
13611361
]);
@@ -1382,7 +1382,7 @@ describe("Error messages", async () => {
13821382
expect(result.errors).to.eql([
13831383
{
13841384
instanceLocation: "#/Foo",
1385-
message: localization.getStringErrorMessage({ pattern: "^[a-z]*$" }),
1385+
message: localization.getPatternErrorMessage("^[a-z]*$"),
13861386
schemaLocation: "https://example.com/main#/propertyNames/pattern"
13871387
}
13881388
]);
@@ -1409,7 +1409,7 @@ describe("Error messages", async () => {
14091409
expect(result.errors).to.eql([
14101410
{
14111411
instanceLocation: "#*/Foo",
1412-
message: localization.getStringErrorMessage({ pattern: "^[a-z]*$" }),
1412+
message: localization.getPatternErrorMessage("^[a-z]*$"),
14131413
schemaLocation: "https://example.com/main#/propertyNames/pattern"
14141414
}
14151415
]);
@@ -1503,8 +1503,7 @@ describe("Error messages", async () => {
15031503
$schema: "https://json-schema.org/draft/2020-12/schema",
15041504
allOf: [
15051505
{ minLength: 3 },
1506-
{ maxLength: 5 },
1507-
{ pattern: "^[a-z]+$" }
1506+
{ maxLength: 5 }
15081507
]
15091508
}, schemaUri);
15101509

@@ -1521,10 +1520,6 @@ describe("Error messages", async () => {
15211520
{
15221521
absoluteKeywordLocation: "https://example.com/main#/allOf/1/maxLength",
15231522
instanceLocation: "#"
1524-
},
1525-
{
1526-
absoluteKeywordLocation: "https://example.com/main#/allOf/2/pattern",
1527-
instanceLocation: "#"
15281523
}
15291524
]
15301525
};
@@ -1533,11 +1528,10 @@ describe("Error messages", async () => {
15331528
expect(result.errors).to.eql([{
15341529
schemaLocation: [
15351530
"https://example.com/main#/allOf/0/minLength",
1536-
"https://example.com/main#/allOf/1/maxLength",
1537-
"https://example.com/main#/allOf/2/pattern"
1531+
"https://example.com/main#/allOf/1/maxLength"
15381532
],
15391533
instanceLocation: "#",
1540-
message: localization.getStringErrorMessage({ minLength: 3, maxLength: 5, pattern: "^[a-z]+$" })
1534+
message: localization.getStringErrorMessage({ minLength: 3, maxLength: 5 })
15411535
}]);
15421536
});
15431537
});

src/localization.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { FluentBundle, FluentResource } from "@fluent/bundle";
1818
* @typedef {{
1919
* minLength?: number;
2020
* maxLength?: number;
21-
* pattern?: string;
2221
* }} StringConstraints
2322
*/
2423

@@ -119,10 +118,6 @@ export class Localization {
119118
messages.push(this._formatMessage("string-error-maxLength", constraints));
120119
}
121120

122-
if (constraints.pattern) {
123-
messages.push(this._formatMessage("string-error-pattern", constraints));
124-
}
125-
126121
return this._formatMessage("string-error", {
127122
constraints: new Intl.ListFormat(this.locale, { type: "conjunction" }).format(messages)
128123
});
@@ -176,6 +171,11 @@ export class Localization {
176171
return this._formatMessage("format-error", { format });
177172
}
178173

174+
/** @type (pattern: string) => string */
175+
getPatternErrorMessage(pattern) {
176+
return this._formatMessage("pattern-error", { pattern });
177+
}
178+
179179
/** @type (constraints: ContainsConstraints) => string */
180180
getContainsErrorMessage(constraints) {
181181
if (constraints.maxContains) {

src/normalization-handlers/contains.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { evaluateSchema } from "../normalized-output.js";
22
import * as Instance from "@hyperjump/json-schema/instance/experimental";
3+
34
/**
45
* @import { KeywordHandler, NormalizedOutput } from "../index.d.ts"
56
* @import { EvaluatedItemsContext } from "./unevaluatedItems.js"

src/translations/en-US.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ type-error = The instance should be of type {$expected} but found {$actual}.
33
string-error = Expected a string {$constraints}.
44
string-error-minLength = at least {$minLength} characters long
55
string-error-maxLength = at most {$maxLength} characters long
6-
string-error-pattern = to match the pattern {$pattern}
76
87
number-error = Expected a number {$constraints}.
98
number-error-minimum = greater than {$minimum}
@@ -20,6 +19,7 @@ max-items-error = The instance should contain a maximum of {$limit} items in the
2019
min-items-error = The instance should contain a minimum of {$limit} items in the array.
2120
unique-items-error = The instance should have unique items in the array.
2221
format-error = The instance should match the format: {$format}.
22+
pattern-error = The instance should match the pattern: {$pattern}.
2323
2424
contains-error-min = The array must contain at least {$minContains ->
2525
[one] item that passes

0 commit comments

Comments
 (0)