From b8ec5ba65103764c06e635e2e521ba067135d54e Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun, 21 Nov 2021 19:21:55 +0000 Subject: [PATCH] use unicodeRegExp ajv option, fix #220 (regexp flags ignored when regexp is cached) --- package.json | 4 ++-- src/definitions/_util.ts | 13 +++++++++---- src/definitions/patternRequired.ts | 2 +- src/definitions/regexp.ts | 6 +++--- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index fb654ce..6b695cd 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "fast-deep-equal": "^3.1.3" }, "peerDependencies": { - "ajv": "^8.0.0" + "ajv": "^8.8.2" }, "devDependencies": { "@ajv-validator/config": "^0.2.3", @@ -48,7 +48,7 @@ "@types/uuid": "^8.3.0", "@typescript-eslint/eslint-plugin": "^4.4.1", "@typescript-eslint/parser": "^4.4.1", - "ajv": "^8.0.0", + "ajv": "^8.8.2", "ajv-formats": "^2.0.0", "chai": "^4.2.0", "eslint": "^7.2.0", diff --git a/src/definitions/_util.ts b/src/definitions/_util.ts index 1f01b12..68bcc01 100644 --- a/src/definitions/_util.ts +++ b/src/definitions/_util.ts @@ -1,5 +1,5 @@ import type {DefinitionOptions} from "./_types" -import type {SchemaObject, CodeGen, Name} from "ajv" +import type {SchemaObject, KeywordCxt, Name} from "ajv" import {_} from "ajv/dist/compile/codegen" const META_SCHEMA_ID = "http://json-schema.org/schema" @@ -8,10 +8,15 @@ export function metaSchemaRef({defaultMeta}: DefinitionOptions = {}): SchemaObje return defaultMeta === false ? {} : {$ref: defaultMeta || META_SCHEMA_ID} } -export function usePattern(gen: CodeGen, pattern: string, flags = "u"): Name { +export function usePattern( + {gen, it: {opts}}: KeywordCxt, + pattern: string, + flags = opts.unicodeRegExp ? "u" : "" +): Name { + const rx = new RegExp(pattern, flags) return gen.scopeValue("pattern", { - key: pattern, - ref: new RegExp(pattern, flags), + key: rx.toString(), + ref: rx, code: _`new RegExp(${pattern}, ${flags})`, }) } diff --git a/src/definitions/patternRequired.ts b/src/definitions/patternRequired.ts index e0f2379..63235c5 100644 --- a/src/definitions/patternRequired.ts +++ b/src/definitions/patternRequired.ts @@ -26,7 +26,7 @@ export default function getDef(): CodeKeywordDefinition { const matched = gen.let("matched", false) gen.forIn("key", data, (key) => { - gen.assign(matched, _`${usePattern(gen, pattern)}.test(${key})`) + gen.assign(matched, _`${usePattern(cxt, pattern)}.test(${key})`) gen.if(matched, () => gen.break()) }) diff --git a/src/definitions/regexp.ts b/src/definitions/regexp.ts index a27ea52..68ddef8 100644 --- a/src/definitions/regexp.ts +++ b/src/definitions/regexp.ts @@ -25,14 +25,14 @@ export default function getDef(): CodeKeywordDefinition { type: "string", schemaType: ["string", "object"], code(cxt: KeywordCxt) { - const {gen, data, schema} = cxt + const {data, schema} = cxt const regx = getRegExp(schema) cxt.pass(_`${regx}.test(${data})`) function getRegExp(sch: string | RegexpSchema): Name { - if (typeof sch == "object") return usePattern(gen, sch.pattern, sch.flags) + if (typeof sch == "object") return usePattern(cxt, sch.pattern, sch.flags) const rx = metaRegexp.exec(sch) - if (rx) return usePattern(gen, rx[1], rx[2]) + if (rx) return usePattern(cxt, rx[1], rx[2]) throw new Error("cannot parse string into RegExp") } },