source: imaps-frontend/node_modules/ajv-keywords/src/definitions/regexp.ts

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import type {CodeKeywordDefinition, KeywordCxt, JSONSchemaType, Name} from "ajv"
2import {_} from "ajv/dist/compile/codegen"
3import {usePattern} from "./_util"
4
5interface RegexpSchema {
6 pattern: string
7 flags?: string
8}
9
10const regexpMetaSchema: JSONSchemaType<RegexpSchema> = {
11 type: "object",
12 properties: {
13 pattern: {type: "string"},
14 flags: {type: "string", nullable: true},
15 },
16 required: ["pattern"],
17 additionalProperties: false,
18}
19
20const metaRegexp = /^\/(.*)\/([gimuy]*)$/
21
22export default function getDef(): CodeKeywordDefinition {
23 return {
24 keyword: "regexp",
25 type: "string",
26 schemaType: ["string", "object"],
27 code(cxt: KeywordCxt) {
28 const {data, schema} = cxt
29 const regx = getRegExp(schema)
30 cxt.pass(_`${regx}.test(${data})`)
31
32 function getRegExp(sch: string | RegexpSchema): Name {
33 if (typeof sch == "object") return usePattern(cxt, sch.pattern, sch.flags)
34 const rx = metaRegexp.exec(sch)
35 if (rx) return usePattern(cxt, rx[1], rx[2])
36 throw new Error("cannot parse string into RegExp")
37 }
38 },
39 metaSchema: {
40 anyOf: [{type: "string"}, regexpMetaSchema],
41 },
42 }
43}
44
45module.exports = getDef
Note: See TracBrowser for help on using the repository browser.