From e79d08844a453c5b2d394007f190302d9e613af0 Mon Sep 17 00:00:00 2001 From: Luper Rouch Date: Mon, 24 Jan 2022 19:00:04 +0000 Subject: [PATCH] feat(index): add AjvClass option (#44) --- README.md | 9 +++++++++ __tests__/index.spec.js | 10 ++++++++++ index.js | 4 +++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 707afc1..ce0616e 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,15 @@ expect.extend(matchersWithOptions({ formats }, (ajv) => { })); ``` +You can also customize the `Ajv` class with the `AjvClass` option: + +```js +import Ajv2020 from 'ajv/dist/2020' +import { matchersWithOptions } from 'jest-json-schema'; + +expect.extend(matchersWithOptions({ AjvClass: Ajv2020 })); +``` + ### Verbose errors Ajv supports a verbose option flag which enables more information about individual diff --git a/__tests__/index.spec.js b/__tests__/index.spec.js index e2e2dd1..65331a1 100644 --- a/__tests__/index.spec.js +++ b/__tests__/index.spec.js @@ -29,5 +29,15 @@ describe('index', () => { matchersWithOptions({}, callback); expect(callback).toHaveBeenCalled(); }); + + it('accepts a custom Ajv class', () => { + const FakeAjvClass = jest.fn().mockImplementation(() => ({ + opts: { code: { formats: {} } }, + addFormat: jest.fn(), + addKeyword: jest.fn(), + })); + matchersWithOptions({ AjvClass: FakeAjvClass }); + expect(FakeAjvClass).toHaveBeenCalled(); + }); }); }); diff --git a/index.js b/index.js index fdd35c6..f183b26 100644 --- a/index.js +++ b/index.js @@ -21,10 +21,12 @@ function matchersWithOptions(userOptions = {}, extendAjv) { const defaultOptions = { allErrors: true, strict: false, + AjvClass: Ajv, }; const options = Object.assign(defaultOptions, userOptions); - const ajv = new Ajv(options); + const { AjvClass, ...ajvOptions } = options; + const ajv = new AjvClass(ajvOptions); addFormats(ajv); if (typeof extendAjv === 'function') { extendAjv(ajv);