Skip to content

Commit

Permalink
refactor: move flag to usePattern function
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Apr 11, 2021
1 parent 4cdcc94 commit 5cab9bf
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 26 deletions.
20 changes: 10 additions & 10 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ const defaultOptions = {
allErrors: false,
verbose: false,
discriminator: false, // *
unicodeRegExp: true // *
$comment: false, // *
formats: {},
keywords: {},
schemas: {},
logger: undefined,
loadSchema: undefined, // *, function(uri: string): Promise {}
unicodeRegExp: true
// options to modify validated data:
removeAdditional: false,
useDefaults: false, // *
Expand Down Expand Up @@ -168,6 +168,15 @@ Include the reference to the part of the schema (`schema` and `parentSchema`) an

Support [discriminator keyword](./json-schema.md#discriminator) from [OpenAPI specification](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md).

### unicodeRegExp

By default Ajv uses unicode flag "u" with "pattern" and "patternProperties", as per JSON Schema spec. See [RegExp.prototype.unicode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode) .

Option values:

- `true` (default) - use unicode flag "u".
- `false` - do not use flag "u".

### $comment

Log or pass the value of `$comment` keyword to a function.
Expand Down Expand Up @@ -240,15 +249,6 @@ Option values:
- `true` - coerce scalar data types.
- `"array"` - in addition to coercions between scalar types, coerce scalar data to an array with one element and vice versa (as required by the schema).

### unicodeRegExp

By default Ajv passes unicode flag "u" to RegExp function when evaluating "pattern" and "patternProperties". See [RegExp.prototype.unicode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode) .

Option values:

- `true` (default) - add unicode flag "u".
- `false` - remove unicode flag "u".

## Advanced options

### meta
Expand Down
2 changes: 1 addition & 1 deletion lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ export interface CurrentOptions {
allowUnionTypes?: boolean
validateFormats?: boolean
// validation and reporting options:
unicodeRegExp?: boolean
$data?: boolean
allErrors?: boolean
verbose?: boolean
discriminator?: boolean
unicodeRegExp?: boolean
$comment?:
| true
| ((comment: string, schemaPath?: string, rootSchema?: AnySchemaObject) => unknown)
Expand Down
6 changes: 1 addition & 5 deletions lib/vocabularies/applicator/additionalProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ const def: CodeKeywordDefinition & AddedKeywordDefinition = {
definedProp = nil
}
if (patProps.length) {
const regExpFlags = it.opts.unicodeRegExp ? "u" : ""
definedProp = or(
definedProp,
...patProps.map((p) => _`${usePattern(gen, p, regExpFlags)}.test(${key})`)
)
definedProp = or(definedProp, ...patProps.map((p) => _`${usePattern(cxt, p)}.test(${key})`))
}
return not(definedProp)
}
Expand Down
3 changes: 1 addition & 2 deletions lib/vocabularies/applicator/patternProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ const def: CodeKeywordDefinition = {

function validateProperties(pat: string): void {
gen.forIn("key", data, (key) => {
const regExpFlags = it.opts.unicodeRegExp ? "u" : ""
gen.if(_`${usePattern(gen, pat, regExpFlags)}.test(${key})`, () => {
gen.if(_`${usePattern(cxt, pat)}.test(${key})`, () => {
cxt.subschema(
{
keyword: "patternProperties",
Expand Down
7 changes: 4 additions & 3 deletions lib/vocabularies/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ export function callValidateCode(
return context !== nil ? _`${func}.call(${context}, ${args})` : _`${func}(${args})`
}

export function usePattern(gen: CodeGen, pattern: string, regExpFlags = "u"): Name {
export function usePattern({gen, it: {opts}}: KeywordCxt, pattern: string): Name {
const u = opts.unicodeRegExp ? "u" : ""
return gen.scopeValue("pattern", {
key: pattern,
ref: new RegExp(pattern, regExpFlags),
code: _`new RegExp(${pattern}, ${regExpFlags})`,
ref: new RegExp(pattern, u),
code: _`new RegExp(${pattern}, ${u})`,
})
}

Expand Down
8 changes: 3 additions & 5 deletions lib/vocabularies/validation/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ const def: CodeKeywordDefinition = {
$data: true,
error,
code(cxt: KeywordCxt) {
const {gen, data, $data, schema, schemaCode, it} = cxt
const regExpFlags = it.opts.unicodeRegExp ? "u" : ""
const {data, $data, schema, schemaCode, it} = cxt
// TODO regexp should be wrapped in try/catchs
const regExp = $data
? _`(new RegExp(${schemaCode}, ${regExpFlags}))`
: usePattern(gen, schema, regExpFlags)
const u = it.opts.unicodeRegExp ? "u" : ""
const regExp = $data ? _`(new RegExp(${schemaCode}, ${u}))` : usePattern(cxt, schema)
cxt.fail$data(_`!${regExp}.test(${data})`)
},
}
Expand Down

0 comments on commit 5cab9bf

Please sign in to comment.