Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Expose the internal ajv variable in the validator implementation classes #3991

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ should change the heading of the (upcoming) version to include a major version b

- fix `getFieldNames`. Now correctly defines an array of primitives.

## @rjsf/validator-ajv6

- Updated the `AJV6Validator` class to expose the internal `ajv` object, allowing access to support a fix related to [#3972](https://github.com/rjsf-team/react-jsonschema-form/issues/3972)

## @rjsf/validator-ajv8

- Updated the `AJV8Validator` class to expose the internal `ajv` object, allowing access to support a fix related to [#3972](https://github.com/rjsf-team/react-jsonschema-form/issues/3972)

## Dev / docs / playground

- Updated the documentation to describe how to use the newly exposed `ajv` variable

# 5.15.0

## @rjsf/mui
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/docs/api-reference/validator-ajv8.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ If a `localizer` is provided, it is used to translate the messages generated by

#### Returns

- ValidatorType<T, S, F>: The custom validator implementation resulting from the set of parameters provided
- AJV8Validator<T, S, F>: The custom validator implementation resulting from the set of parameters provided

### compileSchemaValidators<S extends StrictRJSFSchema = RJSFSchema>()

Expand Down
15 changes: 15 additions & 0 deletions packages/docs/docs/usage/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,21 @@ const validator = customizeValidator({ ajvOptionsOverrides });
render(<Form schema={schema} validator={validator} />, document.getElementById('app'));
```

## Using the raw Ajv instance

The `customizeValidator()` function returns the `AJV8Validator` (or `AJV6Validator` depending on the library you use) implementation class, which has an internal raw `ajv` instance within it.
If you need to do some deep customization of the instance using any of the `ajv` libraries (like `ajv-keywords`), you can do so using this raw instance as follows:

```ts
import { customizeValidator } from '@rjsf/validator-ajv6';
import ajvKeywords from 'ajv-keywords';

const validator = customizeValidator();
ajvKeywords(validator.ajv, ['your-keyword']);

// use your update validator with a `Form`
```

## Ajv8 validator differences

There are a few differences in configuring the Ajv 8 validator.
Expand Down
4 changes: 2 additions & 2 deletions packages/validator-ajv6/src/customizeValidator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '@rjsf/utils';
import { FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';

import { CustomValidatorOptionsType } from './types';
import AJV6Validator from './validator';
Expand All @@ -13,6 +13,6 @@ export default function customizeValidator<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any
>(options: CustomValidatorOptionsType = {}): ValidatorType<T, S, F> {
>(options: CustomValidatorOptionsType = {}) {
return new AJV6Validator<T, S, F>(options);
}
4 changes: 1 addition & 3 deletions packages/validator-ajv6/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ export default class AJV6Validator<T = any, S extends StrictRJSFSchema = RJSFSch
implements ValidatorType<T, S, F>
{
/** The AJV instance to use for all validations
*
* @private
*/
private ajv: Ajv;
ajv: Ajv;

/** Constructs an `AJV6Validator` instance using the `options`
*
Expand Down
4 changes: 2 additions & 2 deletions packages/validator-ajv8/src/customizeValidator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '@rjsf/utils';
import { FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';

import { CustomValidatorOptionsType, Localizer } from './types';
import AJV8Validator from './validator';
Expand All @@ -15,6 +15,6 @@ export default function customizeValidator<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any
>(options: CustomValidatorOptionsType = {}, localizer?: Localizer): ValidatorType<T, S, F> {
>(options: CustomValidatorOptionsType = {}, localizer?: Localizer) {
return new AJV8Validator<T, S, F>(options, localizer);
}
2 changes: 1 addition & 1 deletion packages/validator-ajv8/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class AJV8Validator<T = any, S extends StrictRJSFSchema = RJSFSch
*
* @private
*/
private ajv: Ajv;
ajv: Ajv;

/** The Localizer function to use for localizing Ajv errors
*
Expand Down
7 changes: 0 additions & 7 deletions packages/validator-ajv8/test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ describe('AJV8Validator', () => {
name: 'John Doe',
};

// @ts-expect-error - accessing private Ajv instance to verify compilation happens once
const addSchemaSpy = jest.spyOn(validator.ajv, 'addSchema');

// Call isValid twice with the same schema
Expand Down Expand Up @@ -123,7 +122,6 @@ describe('AJV8Validator', () => {
name: 'John Doe',
};

// @ts-expect-error - accessing private Ajv instance to verify compilation happens once
const ajvInstance = validator.ajv;
const originalGetSchema = ajvInstance.getSchema.bind(ajvInstance);
const getSchemaSpy = jest.spyOn(ajvInstance, 'getSchema');
Expand Down Expand Up @@ -345,7 +343,6 @@ describe('AJV8Validator', () => {
},
};

// @ts-expect-error - accessing private Ajv instance to verify compilation happens once
const compileSpy = jest.spyOn(validator.ajv, 'compile');
compileSpy.mockClear();

Expand Down Expand Up @@ -589,7 +586,6 @@ describe('AJV8Validator', () => {
name: 'John Doe',
};

// @ts-expect-error - accessing private Ajv instance to verify compilation happens once
const addSchemaSpy = jest.spyOn(validator.ajv, 'addSchema');
addSchemaSpy.mockClear();

Expand Down Expand Up @@ -803,7 +799,6 @@ describe('AJV8Validator', () => {
},
};

// @ts-expect-error - accessing private Ajv instance to verify compilation happens once
const compileSpy = jest.spyOn(validator.ajv, 'compile');
compileSpy.mockClear();

Expand Down Expand Up @@ -1047,7 +1042,6 @@ describe('AJV8Validator', () => {
name: 'John Doe',
};

// @ts-expect-error - accessing private Ajv instance to verify compilation happens once
const addSchemaSpy = jest.spyOn(validator.ajv, 'addSchema');
addSchemaSpy.mockClear();

Expand Down Expand Up @@ -1449,7 +1443,6 @@ describe('AJV8Validator', () => {
},
};

// @ts-expect-error - accessing private Ajv instance to verify compilation happens once
const compileSpy = jest.spyOn(validator.ajv, 'compile');
compileSpy.mockClear();

Expand Down