Skip to content

Commit 45200ba

Browse files
heath-freenomeRozamo
authored andcommitted
Fix: Expose the internal ajv variable in the validator implementation classes (rjsf-team#3991)
* Fix: Expose the internal `ajv` variable in the validator implementation 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 * - Responded to reviewer feedback
1 parent 78327aa commit 45200ba

File tree

8 files changed

+34
-16
lines changed

8 files changed

+34
-16
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ should change the heading of the (upcoming) version to include a major version b
2121

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

24+
## @rjsf/validator-ajv6
25+
26+
- 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)
27+
28+
## @rjsf/validator-ajv8
29+
30+
- 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)
31+
32+
## Dev / docs / playground
33+
34+
- Updated the documentation to describe how to use the newly exposed `ajv` variable
35+
2436
# 5.15.0
2537

2638
## @rjsf/mui

packages/docs/docs/api-reference/validator-ajv8.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ If a `localizer` is provided, it is used to translate the messages generated by
2525

2626
#### Returns
2727

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

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

packages/docs/docs/usage/validation.md

+15
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,21 @@ const validator = customizeValidator({ ajvOptionsOverrides });
585585
render(<Form schema={schema} validator={validator} />, document.getElementById('app'));
586586
```
587587

588+
## Using the raw Ajv instance
589+
590+
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.
591+
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:
592+
593+
```ts
594+
import { customizeValidator } from '@rjsf/validator-ajv6';
595+
import ajvKeywords from 'ajv-keywords';
596+
597+
const validator = customizeValidator();
598+
ajvKeywords(validator.ajv, ['your-keyword']);
599+
600+
// use your update validator with a `Form`
601+
```
602+
588603
## Ajv8 validator differences
589604

590605
There are a few differences in configuring the Ajv 8 validator.

packages/validator-ajv6/src/customizeValidator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '@rjsf/utils';
1+
import { FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';
22

33
import { CustomValidatorOptionsType } from './types';
44
import AJV6Validator from './validator';
@@ -13,6 +13,6 @@ export default function customizeValidator<
1313
T = any,
1414
S extends StrictRJSFSchema = RJSFSchema,
1515
F extends FormContextType = any
16-
>(options: CustomValidatorOptionsType = {}): ValidatorType<T, S, F> {
16+
>(options: CustomValidatorOptionsType = {}) {
1717
return new AJV6Validator<T, S, F>(options);
1818
}

packages/validator-ajv6/src/validator.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ export default class AJV6Validator<T = any, S extends StrictRJSFSchema = RJSFSch
3131
implements ValidatorType<T, S, F>
3232
{
3333
/** The AJV instance to use for all validations
34-
*
35-
* @private
3634
*/
37-
private ajv: Ajv;
35+
ajv: Ajv;
3836

3937
/** Constructs an `AJV6Validator` instance using the `options`
4038
*

packages/validator-ajv8/src/customizeValidator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '@rjsf/utils';
1+
import { FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';
22

33
import { CustomValidatorOptionsType, Localizer } from './types';
44
import AJV8Validator from './validator';
@@ -15,6 +15,6 @@ export default function customizeValidator<
1515
T = any,
1616
S extends StrictRJSFSchema = RJSFSchema,
1717
F extends FormContextType = any
18-
>(options: CustomValidatorOptionsType = {}, localizer?: Localizer): ValidatorType<T, S, F> {
18+
>(options: CustomValidatorOptionsType = {}, localizer?: Localizer) {
1919
return new AJV8Validator<T, S, F>(options, localizer);
2020
}

packages/validator-ajv8/src/validator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class AJV8Validator<T = any, S extends StrictRJSFSchema = RJSFSch
2929
*
3030
* @private
3131
*/
32-
private ajv: Ajv;
32+
ajv: Ajv;
3333

3434
/** The Localizer function to use for localizing Ajv errors
3535
*

packages/validator-ajv8/test/validator.test.ts

-7
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ describe('AJV8Validator', () => {
9292
name: 'John Doe',
9393
};
9494

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

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

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

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

@@ -589,7 +586,6 @@ describe('AJV8Validator', () => {
589586
name: 'John Doe',
590587
};
591588

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

@@ -803,7 +799,6 @@ describe('AJV8Validator', () => {
803799
},
804800
};
805801

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

@@ -1047,7 +1042,6 @@ describe('AJV8Validator', () => {
10471042
name: 'John Doe',
10481043
};
10491044

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

@@ -1449,7 +1443,6 @@ describe('AJV8Validator', () => {
14491443
},
14501444
};
14511445

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

0 commit comments

Comments
 (0)