From 6e2f0ed1acb4e4457dcfa0b422c66f2d9eee6f62 Mon Sep 17 00:00:00 2001 From: Heath Chiavettone Date: Fri, 15 Jul 2022 08:26:30 -0700 Subject: [PATCH] Added ajvOptionsOverrides to support user-provided AJV options overriding - 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 #1668 --- packages/validator-ajv6/src/createAjvInstance.ts | 8 ++++++-- packages/validator-ajv6/src/types.ts | 4 ++++ packages/validator-ajv6/src/validator.ts | 4 ++-- .../validator-ajv6/test/createAjvInstance.test.ts | 12 ++++++++++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/validator-ajv6/src/createAjvInstance.ts b/packages/validator-ajv6/src/createAjvInstance.ts index 8132c13787..17d61f917d 100644 --- a/packages/validator-ajv6/src/createAjvInstance.ts +++ b/packages/validator-ajv6/src/createAjvInstance.ts @@ -15,16 +15,20 @@ export const DATA_URL_FORMAT_REGEX = /^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*) /** Creates an Ajv version 6 implementation object with standard support for the 'color` and `data-url` custom formats. * If `additionalMetaSchemas` are provided then the Ajv instance is modified to add each of the meta schemas in the - * list. If `customFormats` are provided then those additional formats are added to the list of supported formats. + * list. If `customFormats` are provided then those additional formats are added to the list of supported formats. If + * `ajvOptionsOverrides` are provided then they are spread on top of the default `AJV_CONFIG` options when constructing + * the `Ajv` instance. * * @param [additionalMetaSchemas] - The list of additional meta schemas that the validator can access * @param [customFormats] - The set of additional custom formats that the validator will support + * @param [ajvOptionsOverrides={}] - The set of validator config override options */ export default function createAjvInstance( additionalMetaSchemas?: CustomValidatorOptionsType['additionalMetaSchemas'], customFormats?: CustomValidatorOptionsType['customFormats'], + ajvOptionsOverrides: CustomValidatorOptionsType['ajvOptionsOverrides'] = {}, ) { - const ajv = new Ajv(AJV_CONFIG); + const ajv = new Ajv({ ...AJV_CONFIG, ...ajvOptionsOverrides }); // add custom formats ajv.addFormat('data-url', DATA_URL_FORMAT_REGEX); diff --git a/packages/validator-ajv6/src/types.ts b/packages/validator-ajv6/src/types.ts index 56d4f14365..f0bb0f51aa 100644 --- a/packages/validator-ajv6/src/types.ts +++ b/packages/validator-ajv6/src/types.ts @@ -1,3 +1,5 @@ +import { Options } from 'ajv'; + /** The type describing how to customize the AJV6 validator */ export interface CustomValidatorOptionsType { @@ -5,4 +7,6 @@ export interface CustomValidatorOptionsType { additionalMetaSchemas?: ReadonlyArray; /** The set of additional custom formats that the validator will support */ customFormats?: { [k: string]: string | RegExp | ((data: string) => boolean) }; + /** The set of config overrides that will be passed to the AJV validator constructor on top of the defaults */ + ajvOptionsOverrides?: Options; } diff --git a/packages/validator-ajv6/src/validator.ts b/packages/validator-ajv6/src/validator.ts index d66b625aea..a989ac3d49 100644 --- a/packages/validator-ajv6/src/validator.ts +++ b/packages/validator-ajv6/src/validator.ts @@ -37,8 +37,8 @@ export default class AJV6Validator implements ValidatorType { * @param options - The `CustomValidatorOptionsType` options that are used to create the AJV instance */ constructor (options: CustomValidatorOptionsType) { - const { additionalMetaSchemas, customFormats } = options; - this.ajv = createAjvInstance(additionalMetaSchemas, customFormats); + const { additionalMetaSchemas, customFormats, ajvOptionsOverrides } = options; + this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides); } /** Transforms a ajv validation errors list: diff --git a/packages/validator-ajv6/test/createAjvInstance.test.ts b/packages/validator-ajv6/test/createAjvInstance.test.ts index 2f1d64fdeb..0cd48530c9 100644 --- a/packages/validator-ajv6/test/createAjvInstance.test.ts +++ b/packages/validator-ajv6/test/createAjvInstance.test.ts @@ -12,6 +12,10 @@ export const CUSTOM_OPTIONS: CustomValidatorOptionsType = { customFormats: { 'phone-us': /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/, 'area-code': /\d{3}/, + }, + ajvOptionsOverrides: { + $data: true, + verbose: true, } }; @@ -43,13 +47,17 @@ describe('createAjvInstance()', () => { describe('no additional meta schemas or custom formats', () => { let ajv: AjvType; beforeAll(() => { - ajv = createAjvInstance(CUSTOM_OPTIONS.additionalMetaSchemas, CUSTOM_OPTIONS.customFormats); + ajv = createAjvInstance( + CUSTOM_OPTIONS.additionalMetaSchemas, + CUSTOM_OPTIONS.customFormats, + CUSTOM_OPTIONS.ajvOptionsOverrides + ); }); afterAll(() => { (Ajv as unknown as jest.Mock).mockClear(); }); it('expect a new Ajv to be constructed with the AJV_CONFIG', () => { - expect(Ajv).toHaveBeenCalledWith(AJV_CONFIG); + expect(Ajv).toHaveBeenCalledWith({ ...AJV_CONFIG, ...CUSTOM_OPTIONS.ajvOptionsOverrides }); }); it('addFormat() was called twice', () => { expect(ajv.addFormat).toHaveBeenCalledTimes(4);