Skip to content

Commit 5581001

Browse files
committed
Add documentation page for schema validator
1 parent c686c1b commit 5581001

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

packages/react-renderer-demo/src/app/pages/renderer/renderer-api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Form Renderer provides a lot of customization via props.
3030
|onCancel|func|A cancel callback, which receives `values` as the first argument.||
3131
|debug|func|A function which will be called with every form update, i.e. `({ values }) => setValues(values)`, please take a look [here](https://final-form.org/docs/react-final-form/types/FormProps#debug)||
3232
|initialValues|object|An object of fields names as keys and values as their values.||
33+
|[schemaValidatorMapper](/renderer/schema-validator)|object|Schema validators mapper. You can control schemas of your components, validators and actions.||
3334
|subscription|object|You can pass your own [subscription](https://final-form.org/docs/react-final-form/types/FormProps#subscription), which will be added to default settings.|`{ pristine: true, submitting: true, valid: true }`|
3435
|[validate](/renderer/validators)|func|A function which receives all form values and returns an object with errors.||
3536
|[validatorMapper](/renderer/validators#validatormapper)|object|A mapper containing custom validators, it's automatically merged with the default one.||
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import Grid from '@material-ui/core/Grid'
2+
3+
import ListOfContents from '../../src/helpers/list-of-contents';
4+
5+
<Grid container item>
6+
<Grid item xs={12} md={10}>
7+
8+
# Schema validation
9+
10+
Data Driven Forms contains default schema validator, that controls basic schema attributes: `name`, `component`, conditions, validators, etc. If you want to control your own validators, components or actions, use [schemaValidatorMapper](/renderer/renderer-api#optionalprops) prop.
11+
12+
It's a simple object containing three keys: `components`, `actions`, `validators`. Each of these is an another object with names as keys and functions as returns. Components' validators receive the whole field object, actions'/validators' validators receive two arguments: action/validator as the first one and the name of the field as the second one.
13+
14+
For returning an error please use `DefaultSchemaError` imported from the renderer's package. If the schema is correct, do nothing.
15+
16+
## Example
17+
18+
```jsx
19+
import FormRenderer, { DefaultSchemaError } from '@data-driven-forms/react-form-renderer';
20+
21+
const schemaValidatorMapper = {
22+
components: {
23+
// schema validators for components
24+
[component]: (field) => {
25+
if(someError(field)) {
26+
throw new DefaultSchemaError(`Error message in field ${field.name}`);
27+
}
28+
}
29+
},
30+
actions: {
31+
// schema validators for actions
32+
[actionName]: (action, fieldName) => {
33+
if(someError(action)) {
34+
throw new DefaultSchemaError(`Error message in field ${fieldName}`);
35+
}
36+
}
37+
},
38+
validators: {
39+
// schema validators for validators
40+
[validatorType]: (validator, fieldName) => {
41+
if(someError(validator)) {
42+
throw new DefaultSchemaError(`Error message in field ${fieldName}`);
43+
}
44+
}
45+
}
46+
}
47+
48+
...
49+
50+
<FormRenderer
51+
{...props}
52+
schemaValidatorMapper={schemaValidatorMapper}
53+
/>
54+
```
55+
56+
</Grid>
57+
<Grid item xs={false} md={2}>
58+
<ListOfContents file="renderer/schema-validator" />
59+
</Grid>
60+
</Grid>

packages/react-renderer-demo/src/app/src/components/navigation/documentation-pages.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export const docs = [
6363
component: 'field-array',
6464
linkText: 'FieldArray Provider'
6565
},
66+
{
67+
component: 'schema-validator',
68+
linkText: 'Schema validation'
69+
},
6670
{
6771
component: 'migration-guide',
6872
linkText: 'Migration guide 1.x to 2.x'

0 commit comments

Comments
 (0)