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
24 changes: 23 additions & 1 deletion src/error-handlers/contains.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as Instance from "@hyperjump/json-schema/instance/experimental";
import { getSchema } from "@hyperjump/json-schema/experimental";
import * as Schema from "@hyperjump/browser";
import * as JsonPointer from "@hyperjump/json-pointer";
import { getErrors } from "../error-handling.js";

/**
* @import { ContainsConstraints } from "../localization.js"
* @import { ErrorHandler, ErrorObject, NormalizedOutput } from "../index.d.ts"
*/

Expand All @@ -11,8 +15,26 @@ const contains = async (normalizedErrors, instance, localization) => {
const errors = [];
if (normalizedErrors["https://json-schema.org/keyword/contains"]) {
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/contains"]) {
const position = schemaLocation.lastIndexOf("/");
const parentLocation = schemaLocation.slice(0, position);

/** @type ContainsConstraints */
const containsConstraints = {};
const minContainsLocation = JsonPointer.append("minContains", parentLocation);
const minContainsNode = await getSchema(minContainsLocation);
/** @type number */
containsConstraints.minContains = Schema.value(minContainsNode) ?? 1;

const maxContainsLocation = JsonPointer.append("maxContains", parentLocation);
const maxContainsNode = await getSchema(maxContainsLocation);
/** @type number */
const maxContains = Schema.value(maxContainsNode);
if (maxContains !== undefined) {
containsConstraints.maxContains = maxContains;
}

errors.push({
message: localization.getContainsErrorMessage(),
message: localization.getContainsErrorMessage(containsConstraints),
instanceLocation: Instance.uri(instance),
schemaLocation: schemaLocation
});
Expand Down
30 changes: 0 additions & 30 deletions src/error-handlers/maxLength.js

This file was deleted.

30 changes: 0 additions & 30 deletions src/error-handlers/minLength.js

This file was deleted.

53 changes: 53 additions & 0 deletions src/error-handlers/string-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { getSchema } from "@hyperjump/json-schema/experimental";
import * as Schema from "@hyperjump/browser";
import * as Instance from "@hyperjump/json-schema/instance/experimental";

/**
* @import { StringConstraints } from "../localization.js"
* @import { ErrorHandler } from "../index.d.ts"
*/

/** @type ErrorHandler */
const stringHandler = async (normalizedErrors, instance, localization) => {
/** @type StringConstraints */
const constraints = {};

/** @type string[] */
const failedSchemaLocations = [];

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/minLength"]) {
if (!normalizedErrors["https://json-schema.org/keyword/minLength"][schemaLocation]) {
failedSchemaLocations.push(schemaLocation);
}

const keyword = await getSchema(schemaLocation);
/** @type number */
const minLength = Schema.value(keyword);
constraints.minLength = Math.max(constraints.minLength ?? Number.MIN_VALUE, minLength);
}

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/maxLength"]) {
if (!normalizedErrors["https://json-schema.org/keyword/maxLength"][schemaLocation]) {
failedSchemaLocations.push(schemaLocation);
}

const keyword = await getSchema(schemaLocation);
/** @type number */
const maxLength = Schema.value(keyword);
constraints.maxLength = Math.min(constraints.maxLength ?? Number.MAX_VALUE, maxLength);
}

if (failedSchemaLocations.length > 0) {
return [
{
message: localization.getStringErrorMessage(constraints),
instanceLocation: Instance.uri(instance),
schemaLocation: failedSchemaLocations.length > 1 ? failedSchemaLocations : failedSchemaLocations[0]
}
];
}

return [];
};

export default stringHandler;
10 changes: 4 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ import maxItemsErrorHandler from "./error-handlers/maxItems.js";
import minItemsErrorHandler from "./error-handlers/minItems.js";
import maxPropertiesErrorHandler from "./error-handlers/maxProperties.js";
import minPropertiesErrorHandler from "./error-handlers/minProperties.js";
import minLengthErrorHandler from "./error-handlers/minLength.js";
import multipleOfErrorHandler from "./error-handlers/multipleOf.js";
import notErrorHandler from "./error-handlers/not.js";
import numberRangeHandler from "./error-handlers/number-range-handler.js";
import patternErrorHandler from "./error-handlers/pattern.js";
import requiredErrorHandler from "./error-handlers/required.js";
import typeErrorHandler from "./error-handlers/type.js";
import uniqueItemsErrorHandler from "./error-handlers/uniqueItems.js";
import maxLengthErrorHandler from "./error-handlers/maxLength.js";
import stringErrorHandler from "./error-handlers/string-handler.js";
import patternErrorHandler from "./error-handlers/pattern.js";

/**
* @import { betterJsonSchemaErrors } from "./index.d.ts"
Expand Down Expand Up @@ -127,15 +126,14 @@ addErrorHandler(maxItemsErrorHandler);
addErrorHandler(minItemsErrorHandler);
addErrorHandler(maxPropertiesErrorHandler);
addErrorHandler(minPropertiesErrorHandler);
addErrorHandler(minLengthErrorHandler);
addErrorHandler(maxLengthErrorHandler);
addErrorHandler(multipleOfErrorHandler);
addErrorHandler(notErrorHandler);
addErrorHandler(numberRangeHandler);
addErrorHandler(patternErrorHandler);
addErrorHandler(requiredErrorHandler);
addErrorHandler(typeErrorHandler);
addErrorHandler(uniqueItemsErrorHandler);
addErrorHandler(stringErrorHandler);
addErrorHandler(patternErrorHandler);

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