Skip to content

Commit

Permalink
Fix: Expose the internal ajv variable in the validator implementati…
Browse files Browse the repository at this point in the history
…on classes

Fixes: rjsf-team#3972 indirectly by exposing the `ajv` variable for use in the issue
- In `@rjsf/ajv6`, updated `AJV6Validator` to make the `ajv` variable public and changed the return of the `customizeValidator()` function to remove the interface return
- In `@rjsf/ajv8`, updated `AJV8Validator` to make the `ajv` variable public and changed the return of the `customizeValidator()` function to remove the interface return
  - Also removed some `@ts-expect-error` tags that are no longer needed due to the exposed variable
- Updated the `CHANGELOG.md` accordingly
  • Loading branch information
heath-freenome committed Dec 5, 2023
1 parent 297dac0 commit a31d32d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 16 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ it according to semantic versioning. For example, if your PR adds a breaking cha
should change the heading of the (upcoming) version to include a major version bump.
-->
# 5.15.1

## @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 instanse 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

0 comments on commit a31d32d

Please sign in to comment.