Skip to content

Commit afc3066

Browse files
Added ajvOptionsOverrides to support user-provided AJV options overriding (rjsf-team#2929)
- Updated `CustomValidatorOptionsType` to add `ajvOptionsOverrides` - Updated the `createAjvInstance()` function to spread any `ajvOptionsOverrides` on top of the `AJV_CONFIG` - Updated `AJV6Validator` constructor to pass `ajvOptionsOverrides` to `createAjvInstance()` - Updated tests to add test-case for the `$data` flag mentioned in rjsf-team#1668
1 parent 8fc91a3 commit afc3066

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

packages/validator-ajv6/src/createAjvInstance.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ export const DATA_URL_FORMAT_REGEX = /^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*)
1515

1616
/** Creates an Ajv version 6 implementation object with standard support for the 'color` and `data-url` custom formats.
1717
* 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.
1921
*
2022
* @param [additionalMetaSchemas] - The list of additional meta schemas that the validator can access
2123
* @param [customFormats] - The set of additional custom formats that the validator will support
24+
* @param [ajvOptionsOverrides={}] - The set of validator config override options
2225
*/
2326
export default function createAjvInstance(
2427
additionalMetaSchemas?: CustomValidatorOptionsType['additionalMetaSchemas'],
2528
customFormats?: CustomValidatorOptionsType['customFormats'],
29+
ajvOptionsOverrides: CustomValidatorOptionsType['ajvOptionsOverrides'] = {},
2630
) {
27-
const ajv = new Ajv(AJV_CONFIG);
31+
const ajv = new Ajv({ ...AJV_CONFIG, ...ajvOptionsOverrides });
2832

2933
// add custom formats
3034
ajv.addFormat('data-url', DATA_URL_FORMAT_REGEX);

packages/validator-ajv6/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import { Options } from 'ajv';
2+
13
/** The type describing how to customize the AJV6 validator
24
*/
35
export interface CustomValidatorOptionsType {
46
/** The list of additional meta schemas that the validator can access */
57
additionalMetaSchemas?: ReadonlyArray<object>;
68
/** The set of additional custom formats that the validator will support */
79
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;
812
}

packages/validator-ajv6/src/validator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export default class AJV6Validator<T = any> implements ValidatorType<T> {
3737
* @param options - The `CustomValidatorOptionsType` options that are used to create the AJV instance
3838
*/
3939
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);
4242
}
4343

4444
/** Transforms a ajv validation errors list:

packages/validator-ajv6/test/createAjvInstance.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export const CUSTOM_OPTIONS: CustomValidatorOptionsType = {
1212
customFormats: {
1313
'phone-us': /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/,
1414
'area-code': /\d{3}/,
15+
},
16+
ajvOptionsOverrides: {
17+
$data: true,
18+
verbose: true,
1519
}
1620
};
1721

@@ -43,13 +47,17 @@ describe('createAjvInstance()', () => {
4347
describe('no additional meta schemas or custom formats', () => {
4448
let ajv: AjvType;
4549
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+
);
4755
});
4856
afterAll(() => {
4957
(Ajv as unknown as jest.Mock).mockClear();
5058
});
5159
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 });
5361
});
5462
it('addFormat() was called twice', () => {
5563
expect(ajv.addFormat).toHaveBeenCalledTimes(4);

0 commit comments

Comments
 (0)