json-schema-library
Version:
Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation
27 lines (24 loc) • 761 B
text/typescript
import ucs2decode from "../utils/punycode.ucs2decode";
import { Keyword, JsonSchemaValidatorParams } from "../Keyword";
export const maxLengthKeyword: Keyword = {
id: "maxLength",
keyword: "maxLength",
addValidate: ({ schema }) => !isNaN(schema.maxLength),
validate: validateMaxLength
};
function validateMaxLength({ node, data, pointer = "#" }: JsonSchemaValidatorParams) {
if (typeof data !== "string") {
return;
}
const { schema } = node;
const length = ucs2decode(data).length;
if (schema.maxLength < length) {
return node.createError("max-length-error", {
maxLength: schema.maxLength,
length,
pointer,
schema,
value: data
});
}
}