Skip to content

Commit c967820

Browse files
Address 4618 documentation issues (rjsf-team#4624)
Improves the documentation for precompiled validators based on rjsf-team#4618 feedback - Updated `validation.md` to improve the documentation - Updated `CHANGELOG.md` to reflect this change
1 parent 0754b1c commit c967820

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ should change the heading of the (upcoming) version to include a major version b
2727
- Added `getChakra` to package exports
2828
- Restored the `ui:options` customization
2929

30+
## Dev / docs / playground
31+
32+
- Updated precompiled schemas documentation in `validation.md` based on v6 changes, addressingg [#4618](https://github.com/rjsf-team/react-jsonschema-form/issues/4618)
33+
3034
# 6.0.0-beta.7
3135

3236
## @rjsf/core

packages/docs/docs/usage/validation.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ The `@rjsf/validator-ajv8` package exports the `compileSchemaValidators()` funct
3232
It is expected that this function will be used in a manner similar to the following:
3333

3434
```cjs
35-
const compileSchemaValidators = require('@rjsf/validator-ajv8/dist/compileSchemaValidators').default;
35+
const compileSchemaValidators = require('@rjsf/validator-ajv8/compileSchemaValidators').default;
3636
const yourSchema = require('path_to/yourSchema'); // If your schema is a js file
3737

3838
compileSchemaValidators(yourSchema, 'path_to/yourCompiledSchema.js');
3939
```
4040

41-
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:
41+
If you are currently using the `customizeValidator()` function to provide `additionalMetaSchemas`, `customFormats`,
42+
`ajvOptionsOverrides` and/or `ajvFormatOptions` then you can pass those in as the optional third parameter to the
43+
`compileSchemaValidators()` function in a manner similar to:
4244

4345
```cjs
4446
const { compileSchemaValidators } = require('@rjsf/validator-ajv8');
@@ -76,23 +78,32 @@ The `@rjsf/validator-ajv8` package exports the `createPrecompiledValidator()` fu
7678
Here is an example of how to use your precompiled validator with your `Form`:
7779

7880
```tsx
81+
import { useMemo } from 'react';
7982
import { createPrecompiledValidator, ValidatorFunctions } from '@rjsf/validator-ajv8';
80-
import Form from '@rjsf/core'; // Or whatever theme you use
83+
import Form, { FormProps } from '@rjsf/core'; // Or whatever theme you use
8184

8285
import yourSchema from 'path_to/yourSchema'; // This needs to be the same file that was precompiled
83-
import * as precompiledValidator from 'path_to/yourCompiledSchema';
86+
import * as precompiledValidatorFns from 'path_to/yourCompiledSchema';
8487

85-
const validator = createPrecompiledValidator(precompiledValidator as ValidatorFunctions);
88+
function MyForm(props: Omit<FormProps, 'validator' | 'schema'>) {
89+
// Memoize the validator to avoid rerenders
90+
const validator = useMemo(
91+
() => createPrecompiledValidator(precompiledValidatorFns, yourSchema),
92+
[validatorFns, yourSchema],
93+
);
94+
95+
return <Form schema={yourSchema} validator={validator} />;
96+
}
8697

87-
render(<Form schema={yourSchema} validator={validator} />, document.getElementById('app'));
98+
render(<MyForm />, document.getElementById('app'));
8899
```
89100

90101
### Dynamically pre-compiling validators
91102

92103
For more advanced cases when schema needs to be precompiled on request - `compileSchemaValidatorsCode` can be used.
93104

94105
```ts
95-
import { compileSchemaValidatorsCode } from '@rjsf/validator-ajv8/dist/compileSchemaValidators';
106+
import { compileSchemaValidatorsCode } from '@rjsf/validator-ajv8/compileSchemaValidators';
96107

97108
const code = compileSchemaValidatorsCode(schema, options);
98109
```
@@ -140,15 +151,15 @@ const regexp = new RegExp(
140151
Object.keys(validatorsBundleReplacements)
141152
.map((key) => key.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'))
142153
.join('|'),
143-
'g'
154+
'g',
144155
);
145156

146157
function wrapAjvBundle(code: string) {
147158
return `function(${Object.values(validatorsBundleReplacements)
148159
.map(([name]) => name)
149160
.join(', ')}){\nvar exports = {};\n${code.replace(
150161
regexp,
151-
(req) => validatorsBundleReplacements[req][0]
162+
(req) => validatorsBundleReplacements[req][0],
152163
)};\nreturn exports;\n}`;
153164
}
154165

@@ -206,7 +217,7 @@ React.useEffect(() => {
206217
evaluateValidator(
207218
schemaId, // some schema id to avoid evaluating it multiple times
208219
code, // result of compileSchemaValidatorsCode returned from the server
209-
nonce // nonce script tag attribute to allow this ib content security policy for the page
220+
nonce, // nonce script tag attribute to allow this ib content security policy for the page
210221
).then(setPrecompiledValidator);
211222
}, [entityType.id]);
212223

@@ -353,7 +364,7 @@ const schema: RJSFSchema = {
353364

354365
render(
355366
<Form schema={schema} validator={validator} transformErrors={transformErrors} />,
356-
document.getElementById('app')
367+
document.getElementById('app'),
357368
);
358369
```
359370

0 commit comments

Comments
 (0)