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