Skip to content

Commit 74e1d69

Browse files
committed
Use configured regExp engine for $data pattern keyword
1 parent 82735a1 commit 74e1d69

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

lib/vocabularies/code.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,21 @@ export function callValidateCode(
9292

9393
const newRegExp = _`new RegExp`
9494

95-
export function usePattern({gen, it: {opts}}: KeywordCxt, pattern: string): Name {
95+
export function usePattern({gen, it: {opts}}: KeywordCxt, pattern: string | Code): Code {
9696
const u = opts.unicodeRegExp ? "u" : ""
9797
const {regExp} = opts.code
98-
const rx = regExp(pattern, u)
98+
const regExpCode = regExp.code === "new RegExp" ? newRegExp : useFunc(gen, regExp)
99+
100+
if (typeof pattern == "string") {
101+
const rx = regExp(pattern, u)
102+
return gen.scopeValue("pattern", {
103+
key: rx.toString(),
104+
ref: rx,
105+
code: _`${regExpCode}(${pattern}, ${u})`,
106+
})
107+
}
99108

100-
return gen.scopeValue("pattern", {
101-
key: rx.toString(),
102-
ref: rx,
103-
code: _`${regExp.code === "new RegExp" ? newRegExp : useFunc(gen, regExp)}(${pattern}, ${u})`,
104-
})
109+
return _`${regExpCode}(${pattern}, ${u})`
105110
}
106111

107112
export function validateArray(cxt: KeywordCxt): Name {

lib/vocabularies/validation/pattern.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ const def: CodeKeywordDefinition = {
1717
$data: true,
1818
error,
1919
code(cxt: KeywordCxt) {
20-
const {data, $data, schema, schemaCode, it} = cxt
20+
const {data, $data, schema, schemaCode} = cxt
2121
// 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)
22+
const regExp = usePattern(cxt, $data ? schemaCode : schema)
2423
cxt.fail$data(_`!${regExp}.test(${data})`)
2524
},
2625
}

spec/options/options_code.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ describe("code generation options", () => {
4646
})
4747
})
4848

49+
describe("regExp option", () => {
50+
it("should use code.regExp for $data pattern values", () => {
51+
const calls: {pattern: string; u: string}[] = []
52+
const regExp: any = (pattern: string, u: string) => {
53+
calls.push({pattern, u})
54+
return {test: () => true}
55+
}
56+
regExp.code = "regExp"
57+
58+
const ajv = new _Ajv({$data: true, code: {regExp}})
59+
const validate = ajv.compile({
60+
type: "object",
61+
properties: {
62+
pattern: {type: "string"},
63+
value: {type: "string", pattern: {$data: "1/pattern"}},
64+
},
65+
})
66+
67+
validate({pattern: "(", value: "abc"}).should.equal(true)
68+
calls.should.have.length(1)
69+
calls[0].pattern.should.equal("(")
70+
})
71+
})
72+
4973
describe("passContext option", () => {
5074
let ajv: Ajv, contexts: any[]
5175

0 commit comments

Comments
 (0)