You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: Implement precompiled validator support in @rjsf/validator-ajv8
Fixesrjsf-team#3543 by implementing support for precompiled validators
- In `@rjsf/validator-ajv8` added support for precompiled validators as follows:
- Added a new `compileSchemaValidators()` API function used to generate the precompiled validators for a schema to an output file
- Updated the documentation for the `customizeValidator()` API function
- Added a new `AJV8PrecompiledValidator` implementation of the `ValidatorType` interface
- Refactored a large piece of the raw validation error processing from the `AJV8Validator` into a new `processRawValidationErrors()` function also used by the `AJV8PrecompiledValidator`
- Added a new `usePrecompiledValidator()` API function that is similar to `customizeValidator()` but returning a precompiled validator-based `ValidatorType` interface implementation
- Added some new types to the `types.ts` file in support of precompiled validators
- Updated the main `index.ts` file to export the new types and API functions
- Added 100% unit test coverage of the new feature
- This included implementing a node function to precompile the `superSchema.json` file found in the `test/harness` directory
- Updated the `validation.md` documentation for the new precompiled validator functionality
- Added a new `validator-ajv8.md` documentation file to the `api-reference` directory and the `sidebar.js`
- Updated the `CHANGELOG.md` file accordingly
Copy file name to clipboardExpand all lines: CHANGELOG.md
+8-3Lines changed: 8 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,6 @@ should change the heading of the (upcoming) version to include a major version b
25
25
## @rjsf/core
26
26
27
27
- Updated the `MultiSchemaField` to use the new `getDiscriminatorFieldFromSchema()` API
28
-
29
28
- Added new `experimental_defaultFormStateBehavior` prop to `Form` to specify alternate behavior when dealing with the rendering of array fields where `minItems` is set but field is not `required` (fixes [3363](https://github.com/rjsf-team/react-jsonschema-form/issues/3363)) ([3604](https://github.com/rjsf-team/react-jsonschema-form/pull/3604))
30
29
31
30
## @rjsf/fluent-ui
@@ -38,12 +37,18 @@ should change the heading of the (upcoming) version to include a major version b
38
37
- Updated `getDefaultFormState()` and `toPathSchema()` to use `getDiscriminatorFieldFromSchema()` to provide a discriminator field to `getClosestMatchingOption()` calls.
39
38
- Refactored the `retrieveSchema()` internal API functions to support implementing an internal `schemaParser()` API for use in precompiling schemas, in support of [#3543](https://github.com/rjsf-team/react-jsonschema-form/issues/3543)
40
39
- Fixed `toPathSchema()` to handle `properties` in an object along with `anyOf`/`oneOf`, fixing [#3628](https://github.com/rjsf-team/react-jsonschema-form/issues/3628) and [#1628](https://github.com/rjsf-team/react-jsonschema-form/issues/1628)
41
-
- Refactored optional parameters for `computeDefaults()` into destructured props object to reduce clutter when only specifying later of the optional argument ([3604](https://github.com/rjsf-team/react-jsonschema-form/pull/3604))
40
+
- Refactored optional parameters for `computeDefaults()` into destructured props object to reduce clutter when only specifying later of the optional argument, fixing [#3602](https://github.com/rjsf-team/react-jsonschema-form/issues/3602)
41
+
42
+
## @rjsf/validator-ajv8
43
+
44
+
- Added two new APIs `compileSchemaValidators()` and `usePrecompiledValidator()` implemented to support using precompiled validators build with AJV 8, fixing [#3543](https://github.com/rjsf-team/react-jsonschema-form/issues/3543)
42
45
43
46
## Dev / docs / playground
44
47
45
48
- Added documentation to `custom-templates` describing how to extend the `BaseInputTemplate`
46
-
- Added **minItems behavior for array field** live setting ([3604](https://github.com/rjsf-team/react-jsonschema-form/pull/3604))
49
+
- Added **minItems behavior for array field** live setting, fixing [#3602](https://github.com/rjsf-team/react-jsonschema-form/issues/3602)
50
+
- Added documentation to `validation` describing the new precompiled validators feature
51
+
- Added new `validator-ajv8.md` documentation to the `api-reference` directory as well as putting it into the `sidebar.js`
In RJSF version 5, the original, embedded AJV 6 validator implementation from earlier versions was extracted into its own package, `@rjsf/validator-ajv6`, which was immediately deprecated since AJV 6 is no longer getting maintenance updates.
4
+
A new `@rjsf/validator-ajv8` package was added that uses the AJV 8 package, including adding support for using precompiled validators.
5
+
Below are the exported API functions that are provided by this package.
6
+
See the [Validation documentation]('../usage/validation.md) for examples of using these APIs.
7
+
8
+
## Types
9
+
10
+
There are a few Typescript types that are exported by `@rjsf/validator-ajv8` in support of the APIs.
11
+
12
+
These types can be found on GitHub [here](https://github.com/rjsf-team/react-jsonschema-form/blob/main/packages/validator-ajv8/src/types.ts).
13
+
14
+
## APIs
15
+
16
+
### customizeValidator<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>()
17
+
18
+
Creates and returns a customized implementation of the `ValidatorType` with the given customization `options` if provided.
19
+
If a `localizer` is provided, it is used to translate the messages generated by the underlying AJV validation.
20
+
21
+
#### Parameters
22
+
23
+
-[options={}]: CustomValidatorOptionsType - The optional map of `CustomValidatorOptionsType` options that are used to create the `ValidatorType` instance
24
+
-[localizer]: Localizer | undefined - If provided, is used to localize a list of Ajv `ErrorObject`s after running the form validation using AJV
25
+
26
+
#### Returns
27
+
28
+
- ValidatorType<T, S, F>: The custom validator implementation resulting from the set of parameters provided
The function used to compile a schema into an output file in the form that allows it to be used as a precompiled validator.
33
+
The main reasons for using a precompiled validator is reducing code size, improving validation speed and, most importantly, avoiding dynamic code compilation when prohibited by a browser's Content Security Policy.
34
+
For more information about AJV code compilation see: https://ajv.js.org/standalone.html
35
+
36
+
#### Parameters
37
+
38
+
- schema: S - The schema to be compiled into a set of precompiled validators functions
39
+
- output: string - The name of the file into which the precompiled validator functions will be generated
40
+
-[options={}]: CustomValidatorOptionsType - The set of `CustomValidatorOptionsType` information used to alter the AJV validator used for compiling the schema. They are the same options that are passed to the `customizeValidator()` function in order to modify the behavior of the regular AJV-based validator.
41
+
42
+
### usePrecompiledValidator<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>()
43
+
44
+
Creates and returns a `ValidatorType` interface that is implemented with a precompiled validator.
45
+
If a `localizer` is provided, it is used to translate the messages generated by the underlying AJV validation.
46
+
47
+
> NOTE: The `validateFns` parameter is an object obtained by importing from a precompiled validation file created via the `compileSchemaValidators()` function.
48
+
49
+
#### Parameters
50
+
51
+
- validateFns: ValidatorFunctions<T> - The map of the validation functions that are created by the `compileSchemaValidators()` function
52
+
- rootSchema: S - The root schema that was used with the `compileSchemaValidators()` function
53
+
-[localizer]: Localizer | undefined - If provided, is used to localize a list of Ajv `ErrorObject`s after running the form validation using AJV
54
+
55
+
#### Returns
56
+
57
+
- ValidatorType<T, S, F>: The precompiled validator implementation resulting from the set of parameters provided
Copy file name to clipboardExpand all lines: packages/docs/docs/usage/validation.md
+76Lines changed: 76 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,82 @@ If you depend on having specifically formatted messages, then using this validat
13
13
14
14
It is also possible for you to provide your own implementation if you desire, as long as it fulfills the `ValidatorType` interface specified in `@rjsf/utils`.
15
15
16
+
## API documentation
17
+
18
+
The documentation for the APIs associated with the AJV 8 validator package can be found [here]('../api-reference/validator-ajv8.md')
19
+
20
+
## Precompiled validators
21
+
22
+
In 5.7.0, support for precompiled validators was added to the `@rjsf/validator-ajv8` package.
23
+
The main use case for this is to overcome issues with `unsafe-eval` warnings from the browser caused by strict Content Security Policy settings.
24
+
See the [Standalone Validation Code](https://ajv.js.org/standalone.html) section of the AJV documentation for more details about precompiled validators.
25
+
26
+
Due to how RJSF uses the AJV validator in determining `anyOf/oneOf` selections and how it resolves dependencies, if-then-else and references ($ref) in schemas via the `retrieveSchema()` utility method, RJSF provides its own schema compilation API built on-top-of the one provided by AJV 8.
27
+
If you are wanting to use a precompiled validator, two steps are required:
28
+
29
+
1. Precompiling the schema into a set of validator functions
30
+
2. Providing those precompiled validator functions to a `ValidatorType` implementation in the `Form`
31
+
32
+
### Schema precompilation
33
+
34
+
The first step in the process is to compile a schema into a set of validator functions that are saved into a commonJS-based Javascript file.
35
+
The `@rjsf/validator-ajv8` package exports the `compileSchemaValidators()` function that does this.
36
+
It is expected that this function will be used in a manner similar to the following:
If you are currently using the `customizeValidator()` function to provide `additionalMetaSchemas`, `customFormats`, `ajvOptionsOverrides` and/or `ajvFormatOptions` then you can pass those in as the optional third parameter to the `compileSchemaValidators()` function in a manner similar to:
It is highly recommended to create a `compileYourSchema.js` file (or what ever name you want) with code similar to what is shown above and then, using node, run the code as follows:
67
+
68
+
```
69
+
node compileYourSchema.js
70
+
```
71
+
72
+
> NOTE: You must have your schema provided within a file that can be parsed and turned into the set of precompiled validator functions.
73
+
74
+
### Using the precompiled validator
75
+
76
+
After you have completed step 1 having generated your precompiled schema functions into the `yourCompiledSchema.js` output file (or whatever you called it), then you need to create a `ValidatorType` implementation from it to use in the `Form`.
77
+
The `@rjsf/validator-ajv8` package exports the `usePrecompiledValidator()` function for this.
78
+
Here is an example of how to use your precompiled validator with your `Form`:
0 commit comments