File tree 4 files changed +22
-6
lines changed
4 files changed +22
-6
lines changed Original file line number Diff line number Diff line change @@ -15,16 +15,20 @@ export const DATA_URL_FORMAT_REGEX = /^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*)
15
15
16
16
/** Creates an Ajv version 6 implementation object with standard support for the 'color` and `data-url` custom formats.
17
17
* If `additionalMetaSchemas` are provided then the Ajv instance is modified to add each of the meta schemas in the
18
- * list. If `customFormats` are provided then those additional formats are added to the list of supported formats.
18
+ * list. If `customFormats` are provided then those additional formats are added to the list of supported formats. If
19
+ * `ajvOptionsOverrides` are provided then they are spread on top of the default `AJV_CONFIG` options when constructing
20
+ * the `Ajv` instance.
19
21
*
20
22
* @param [additionalMetaSchemas] - The list of additional meta schemas that the validator can access
21
23
* @param [customFormats] - The set of additional custom formats that the validator will support
24
+ * @param [ajvOptionsOverrides={ }] - The set of validator config override options
22
25
*/
23
26
export default function createAjvInstance (
24
27
additionalMetaSchemas ?: CustomValidatorOptionsType [ 'additionalMetaSchemas' ] ,
25
28
customFormats ?: CustomValidatorOptionsType [ 'customFormats' ] ,
29
+ ajvOptionsOverrides : CustomValidatorOptionsType [ 'ajvOptionsOverrides' ] = { } ,
26
30
) {
27
- const ajv = new Ajv ( AJV_CONFIG ) ;
31
+ const ajv = new Ajv ( { ... AJV_CONFIG , ... ajvOptionsOverrides } ) ;
28
32
29
33
// add custom formats
30
34
ajv . addFormat ( 'data-url' , DATA_URL_FORMAT_REGEX ) ;
Original file line number Diff line number Diff line change
1
+ import { Options } from 'ajv' ;
2
+
1
3
/** The type describing how to customize the AJV6 validator
2
4
*/
3
5
export interface CustomValidatorOptionsType {
4
6
/** The list of additional meta schemas that the validator can access */
5
7
additionalMetaSchemas ?: ReadonlyArray < object > ;
6
8
/** The set of additional custom formats that the validator will support */
7
9
customFormats ?: { [ k : string ] : string | RegExp | ( ( data : string ) => boolean ) } ;
10
+ /** The set of config overrides that will be passed to the AJV validator constructor on top of the defaults */
11
+ ajvOptionsOverrides ?: Options ;
8
12
}
Original file line number Diff line number Diff line change @@ -37,8 +37,8 @@ export default class AJV6Validator<T = any> implements ValidatorType<T> {
37
37
* @param options - The `CustomValidatorOptionsType` options that are used to create the AJV instance
38
38
*/
39
39
constructor ( options : CustomValidatorOptionsType ) {
40
- const { additionalMetaSchemas, customFormats } = options ;
41
- this . ajv = createAjvInstance ( additionalMetaSchemas , customFormats ) ;
40
+ const { additionalMetaSchemas, customFormats, ajvOptionsOverrides } = options ;
41
+ this . ajv = createAjvInstance ( additionalMetaSchemas , customFormats , ajvOptionsOverrides ) ;
42
42
}
43
43
44
44
/** Transforms a ajv validation errors list:
Original file line number Diff line number Diff line change @@ -12,6 +12,10 @@ export const CUSTOM_OPTIONS: CustomValidatorOptionsType = {
12
12
customFormats : {
13
13
'phone-us' : / \( ? \d { 3 } \) ? [ \s - ] ? \d { 3 } [ \s - ] ? \d { 4 } $ / ,
14
14
'area-code' : / \d { 3 } / ,
15
+ } ,
16
+ ajvOptionsOverrides : {
17
+ $data : true ,
18
+ verbose : true ,
15
19
}
16
20
} ;
17
21
@@ -43,13 +47,17 @@ describe('createAjvInstance()', () => {
43
47
describe ( 'no additional meta schemas or custom formats' , ( ) => {
44
48
let ajv : AjvType ;
45
49
beforeAll ( ( ) => {
46
- ajv = createAjvInstance ( CUSTOM_OPTIONS . additionalMetaSchemas , CUSTOM_OPTIONS . customFormats ) ;
50
+ ajv = createAjvInstance (
51
+ CUSTOM_OPTIONS . additionalMetaSchemas ,
52
+ CUSTOM_OPTIONS . customFormats ,
53
+ CUSTOM_OPTIONS . ajvOptionsOverrides
54
+ ) ;
47
55
} ) ;
48
56
afterAll ( ( ) => {
49
57
( Ajv as unknown as jest . Mock ) . mockClear ( ) ;
50
58
} ) ;
51
59
it ( 'expect a new Ajv to be constructed with the AJV_CONFIG' , ( ) => {
52
- expect ( Ajv ) . toHaveBeenCalledWith ( AJV_CONFIG ) ;
60
+ expect ( Ajv ) . toHaveBeenCalledWith ( { ... AJV_CONFIG , ... CUSTOM_OPTIONS . ajvOptionsOverrides } ) ;
53
61
} ) ;
54
62
it ( 'addFormat() was called twice' , ( ) => {
55
63
expect ( ajv . addFormat ) . toHaveBeenCalledTimes ( 4 ) ;
You can’t perform that action at this time.
0 commit comments