Skip to content

Commit b7b9b90

Browse files
authored
Merge pull request #49 from arpitkuriyal/string-range-handler
String range handler and contains errorHandler
2 parents c11aab9 + 33eb965 commit b7b9b90

File tree

9 files changed

+306
-93
lines changed

9 files changed

+306
-93
lines changed

src/error-handlers/contains.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import * as Instance from "@hyperjump/json-schema/instance/experimental";
2+
import { getSchema } from "@hyperjump/json-schema/experimental";
3+
import * as Schema from "@hyperjump/browser";
4+
import * as JsonPointer from "@hyperjump/json-pointer";
25
import { getErrors } from "../error-handling.js";
36

47
/**
8+
* @import { ContainsConstraints } from "../localization.js"
59
* @import { ErrorHandler, ErrorObject, NormalizedOutput } from "../index.d.ts"
610
*/
711

@@ -11,8 +15,26 @@ const contains = async (normalizedErrors, instance, localization) => {
1115
const errors = [];
1216
if (normalizedErrors["https://json-schema.org/keyword/contains"]) {
1317
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/contains"]) {
18+
const position = schemaLocation.lastIndexOf("/");
19+
const parentLocation = schemaLocation.slice(0, position);
20+
21+
/** @type ContainsConstraints */
22+
const containsConstraints = {};
23+
const minContainsLocation = JsonPointer.append("minContains", parentLocation);
24+
const minContainsNode = await getSchema(minContainsLocation);
25+
/** @type number */
26+
containsConstraints.minContains = Schema.value(minContainsNode) ?? 1;
27+
28+
const maxContainsLocation = JsonPointer.append("maxContains", parentLocation);
29+
const maxContainsNode = await getSchema(maxContainsLocation);
30+
/** @type number */
31+
const maxContains = Schema.value(maxContainsNode);
32+
if (maxContains !== undefined) {
33+
containsConstraints.maxContains = maxContains;
34+
}
35+
1436
errors.push({
15-
message: localization.getContainsErrorMessage(),
37+
message: localization.getContainsErrorMessage(containsConstraints),
1638
instanceLocation: Instance.uri(instance),
1739
schemaLocation: schemaLocation
1840
});

src/error-handlers/maxLength.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/error-handlers/minLength.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/error-handlers/string-handler.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 { StringConstraints } from "../localization.js"
7+
* @import { ErrorHandler } from "../index.d.ts"
8+
*/
9+
10+
/** @type ErrorHandler */
11+
const stringHandler = async (normalizedErrors, instance, localization) => {
12+
/** @type StringConstraints */
13+
const constraints = {};
14+
15+
/** @type string[] */
16+
const failedSchemaLocations = [];
17+
18+
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/minLength"]) {
19+
if (!normalizedErrors["https://json-schema.org/keyword/minLength"][schemaLocation]) {
20+
failedSchemaLocations.push(schemaLocation);
21+
}
22+
23+
const keyword = await getSchema(schemaLocation);
24+
/** @type number */
25+
const minLength = Schema.value(keyword);
26+
constraints.minLength = Math.max(constraints.minLength ?? Number.MIN_VALUE, minLength);
27+
}
28+
29+
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/maxLength"]) {
30+
if (!normalizedErrors["https://json-schema.org/keyword/maxLength"][schemaLocation]) {
31+
failedSchemaLocations.push(schemaLocation);
32+
}
33+
34+
const keyword = await getSchema(schemaLocation);
35+
/** @type number */
36+
const maxLength = Schema.value(keyword);
37+
constraints.maxLength = Math.min(constraints.maxLength ?? Number.MAX_VALUE, maxLength);
38+
}
39+
40+
if (failedSchemaLocations.length > 0) {
41+
return [
42+
{
43+
message: localization.getStringErrorMessage(constraints),
44+
instanceLocation: Instance.uri(instance),
45+
schemaLocation: failedSchemaLocations.length > 1 ? failedSchemaLocations : failedSchemaLocations[0]
46+
}
47+
];
48+
}
49+
50+
return [];
51+
};
52+
53+
export default stringHandler;

src/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,14 @@ import maxItemsErrorHandler from "./error-handlers/maxItems.js";
5555
import minItemsErrorHandler from "./error-handlers/minItems.js";
5656
import maxPropertiesErrorHandler from "./error-handlers/maxProperties.js";
5757
import minPropertiesErrorHandler from "./error-handlers/minProperties.js";
58-
import minLengthErrorHandler from "./error-handlers/minLength.js";
5958
import multipleOfErrorHandler from "./error-handlers/multipleOf.js";
6059
import notErrorHandler from "./error-handlers/not.js";
6160
import numberRangeHandler from "./error-handlers/number-range-handler.js";
62-
import patternErrorHandler from "./error-handlers/pattern.js";
6361
import requiredErrorHandler from "./error-handlers/required.js";
6462
import typeErrorHandler from "./error-handlers/type.js";
6563
import uniqueItemsErrorHandler from "./error-handlers/uniqueItems.js";
66-
import maxLengthErrorHandler from "./error-handlers/maxLength.js";
64+
import stringErrorHandler from "./error-handlers/string-handler.js";
65+
import patternErrorHandler from "./error-handlers/pattern.js";
6766

6867
/**
6968
* @import { betterJsonSchemaErrors } from "./index.d.ts"
@@ -127,15 +126,14 @@ addErrorHandler(maxItemsErrorHandler);
127126
addErrorHandler(minItemsErrorHandler);
128127
addErrorHandler(maxPropertiesErrorHandler);
129128
addErrorHandler(minPropertiesErrorHandler);
130-
addErrorHandler(minLengthErrorHandler);
131-
addErrorHandler(maxLengthErrorHandler);
132129
addErrorHandler(multipleOfErrorHandler);
133130
addErrorHandler(notErrorHandler);
134131
addErrorHandler(numberRangeHandler);
135-
addErrorHandler(patternErrorHandler);
136132
addErrorHandler(requiredErrorHandler);
137133
addErrorHandler(typeErrorHandler);
138134
addErrorHandler(uniqueItemsErrorHandler);
135+
addErrorHandler(stringErrorHandler);
136+
addErrorHandler(patternErrorHandler);
139137

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

0 commit comments

Comments
 (0)