UNPKG

json-schema-library

Version:

Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation

28 lines (25 loc) 807 B
import { Keyword, JsonSchemaValidatorParams } from "../Keyword"; export const patternKeyword: Keyword = { id: "pattern", keyword: "pattern", addValidate: ({ schema }) => typeof schema.pattern === "string", validate: validatePattern }; function validatePattern({ node, data, pointer = "#" }: JsonSchemaValidatorParams) { const { schema } = node; if (typeof data !== "string") { return; } const pattern = new RegExp(schema.pattern, "u"); if (pattern.test(data) === false) { return node.createError("pattern-error", { pattern: schema.pattern, description: schema.patternExample || schema.pattern, received: data, schema, value: data, pointer }); } return undefined; }