Skip to content

Commit

Permalink
feat(index): add AjvClass option (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
flupke authored Jan 24, 2022
1 parent e695fcd commit e79d088
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e79d088

Please sign in to comment.