1 import type {CodeKeywordDefinition, ErrorObject, KeywordErrorDefinition} from "../../types"
2 import type {KeywordCxt} from "../../compile/validate"
3 import {usePattern} from "../code"
4 import {_, str} from "../../compile/codegen"
6 export type PatternError = ErrorObject<"pattern", {pattern: string}, string | {$data: string}>
8 const error: KeywordErrorDefinition = {
9 message: ({schemaCode}) => str`must match pattern "${schemaCode}"`,
10 params: ({schemaCode}) => _`{pattern: ${schemaCode}}`,
13 const def: CodeKeywordDefinition = {
19 code(cxt: KeywordCxt) {
20 const {gen, data, $data, schema, schemaCode} = cxt
21 const regExp = $data ? _`(new RegExp(${schemaCode}, "u"))` : usePattern(gen, schema) // TODO regexp should be wrapped in try/catch
22 cxt.fail$data(_`!${regExp}.test(${data})`)