Skip to content

Commit

Permalink
added: RE2 Option with fallback
Browse files Browse the repository at this point in the history
Signed-off-by: Efe Barlas <ebarlas@purdue.edu>
  • Loading branch information
efebarlas committed Jul 9, 2021
1 parent 001f829 commit b07542d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export interface CurrentOptions {
verbose?: boolean
discriminator?: boolean
unicodeRegExp?: boolean
useRe2?: boolean
timestamp?: "string" | "date" // JTD only
parseDate?: boolean // JTD only
allowDate?: boolean // JTD only
Expand Down Expand Up @@ -219,7 +220,8 @@ type RequiredInstanceOptions = {
| "validateSchema"
| "validateFormats"
| "int32range"
| "unicodeRegExp"]: NonNullable<Options[K]>
| "unicodeRegExp"
| "useRe2"]: NonNullable<Options[K]>
} & {code: InstanceCodeOptions}

export type InstanceOptions = Options & RequiredInstanceOptions
Expand Down Expand Up @@ -248,6 +250,7 @@ function requiredOptions(o: Options): RequiredInstanceOptions {
validateSchema: o.validateSchema ?? true,
validateFormats: o.validateFormats ?? true,
unicodeRegExp: o.unicodeRegExp ?? true,
useRe2: o.useRe2 ?? false,
int32range: o.int32range ?? true,
}
}
Expand Down
6 changes: 6 additions & 0 deletions lib/runtime/re2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as re2 from "re2"

type Re2 = typeof re2 & {code: string}
;(re2 as Re2).code = 'require("ajv/dist/runtime/re2").default'

export default re2 as Re2
24 changes: 22 additions & 2 deletions lib/vocabularies/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import type {KeywordCxt} from "../compile/validate"
import {CodeGen, _, and, or, not, nil, strConcat, getProperty, Code, Name} from "../compile/codegen"
import {alwaysValidSchema, Type} from "../compile/util"
import N from "../compile/names"

import {useFunc} from "../compile/util"
import re2 from "../runtime/re2"
import * as Re2 from "re2"
export function checkReportMissingProp(cxt: KeywordCxt, prop: string): void {
const {gen, data, it} = cxt
gen.if(noPropertyInData(gen, data, prop, it.opts.ownProperties), () => {
Expand Down Expand Up @@ -90,8 +92,26 @@ export function callValidateCode(
return context !== nil ? _`${func}.call(${context}, ${args})` : _`${func}(${args})`
}

export function usePattern({gen, it: {opts}}: KeywordCxt, pattern: string): Name {
export function usePattern({gen, it: {self, opts}}: KeywordCxt, pattern: string): Name {
const u = opts.unicodeRegExp ? "u" : ""
const useRe2 = opts.useRe2
//const {gen, data, $data, schema, schemaCode, it} = cxt
//const regExp = $data ? _`(new RegExp(${schemaCode}, ${u}))` : usePattern(cxt, schema)
if (u === "u" && useRe2) {
try {
const s = new Re2(pattern)
self.logger.log(s)
return gen.scopeValue("pattern", {
key: pattern,
ref: new Re2(pattern),
code: _`new ${useFunc(gen, re2)}(${pattern})`,
})
} catch (e) {
self.logger.log(
"Warning: Fallback to default regex engine. Please verify the safeness of your regex"
)
}
}
return gen.scopeValue("pattern", {
key: pattern,
ref: new RegExp(pattern, u),
Expand Down

0 comments on commit b07542d

Please sign in to comment.