main
Last change
on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
961 bytes
|
Rev | Line | |
---|
[79a0317] | 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"
|
---|
| 5 |
|
---|
| 6 | export type PatternError = ErrorObject<"pattern", {pattern: string}, string | {$data: string}>
|
---|
| 7 |
|
---|
| 8 | const error: KeywordErrorDefinition = {
|
---|
| 9 | message: ({schemaCode}) => str`must match pattern "${schemaCode}"`,
|
---|
| 10 | params: ({schemaCode}) => _`{pattern: ${schemaCode}}`,
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | const def: CodeKeywordDefinition = {
|
---|
| 14 | keyword: "pattern",
|
---|
| 15 | type: "string",
|
---|
| 16 | schemaType: "string",
|
---|
| 17 | $data: true,
|
---|
| 18 | error,
|
---|
| 19 | code(cxt: KeywordCxt) {
|
---|
| 20 | const {data, $data, schema, schemaCode, it} = cxt
|
---|
| 21 | // TODO regexp should be wrapped in try/catchs
|
---|
| 22 | const u = it.opts.unicodeRegExp ? "u" : ""
|
---|
| 23 | const regExp = $data ? _`(new RegExp(${schemaCode}, ${u}))` : usePattern(cxt, schema)
|
---|
| 24 | cxt.fail$data(_`!${regExp}.test(${data})`)
|
---|
| 25 | },
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | export default def
|
---|
Note:
See
TracBrowser
for help on using the repository browser.