Skip to content

Commit

Permalink
Added ajvOptionsOverrides to support user-provided AJV options overri…
Browse files Browse the repository at this point in the history
…ding

- 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
  • Loading branch information
heath-freenome committed Jul 15, 2022
1 parent b09d6d9 commit b48b160
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/validator-ajv6/src/createAjvInstance.ts
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions packages/validator-ajv6/src/types.ts
@@ -1,8 +1,12 @@
import { Options } from 'ajv';

/** The type describing how to customize the AJV6 validator
*/
export interface CustomValidatorOptionsType {
/** The list of additional meta schemas that the validator can access */
additionalMetaSchemas?: ReadonlyArray<object>;
/** 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;
}
4 changes: 2 additions & 2 deletions packages/validator-ajv6/src/validator.ts
Expand Up @@ -37,8 +37,8 @@ export default class AJV6Validator<T = any> implements ValidatorType<T> {
* @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:
Expand Down

0 comments on commit b48b160

Please sign in to comment.