|
| 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> |
0 commit comments