From b48b160849283fcbc48b8fd051e67ad135d00666 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 | 5 ++++- packages/validator-ajv6/src/types.ts | 4 ++++ packages/validator-ajv6/src/validator.ts | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/validator-ajv6/src/createAjvInstance.ts b/packages/validator-ajv6/src/createAjvInstance.ts index 8132c13787..3c7c8b29ae 100644 --- a/packages/validator-ajv6/src/createAjvInstance.ts +++ b/packages/validator-ajv6/src/createAjvInstance.ts @@ -16,15 +16,18 @@ 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. + * TBD * * @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 [validatorConfigOverrides] - The set of validator config override options */ export default function createAjvInstance( additionalMetaSchemas?: CustomValidatorOptionsType['additionalMetaSchemas'], customFormats?: CustomValidatorOptionsType['customFormats'], + validatorConfigOverrides: CustomValidatorOptionsType['validatorConfigOverrides'] = {}, ) { - const ajv = new Ajv(AJV_CONFIG); + const ajv = new Ajv({ ...AJV_CONFIG, ...validatorConfigOverrides }); // 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..f19a6055cd 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 */ + validatorConfigOverrides?: Options; } diff --git a/packages/validator-ajv6/src/validator.ts b/packages/validator-ajv6/src/validator.ts index d66b625aea..555cf00d3b 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, validatorConfigOverrides } = options; + this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, validatorConfigOverrides); } /** Transforms a ajv validation errors list: