Skip to content

Commit e330905

Browse files
committed
Add live example of schema validator
1 parent 5581001 commit e330905

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

packages/react-renderer-demo/src/app/pages/renderer/schema-validator.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Grid from '@material-ui/core/Grid'
2-
2+
import RawComponent from '@docs/raw-component';
33
import ListOfContents from '../../src/helpers/list-of-contents';
44

55
<Grid container item>
@@ -13,7 +13,7 @@ It's a simple object containing three keys: `components`, `actions`, `validators
1313

1414
For returning an error please use `DefaultSchemaError` imported from the renderer's package. If the schema is correct, do nothing.
1515

16-
## Example
16+
## Schema
1717

1818
```jsx
1919
import FormRenderer, { DefaultSchemaError } from '@data-driven-forms/react-form-renderer';
@@ -53,6 +53,10 @@ const schemaValidatorMapper = {
5353
/>
5454
```
5555

56+
## Example
57+
58+
<RawComponent source="schema-validator" />
59+
5660
</Grid>
5761
<Grid item xs={false} md={2}>
5862
<ListOfContents file="renderer/schema-validator" />
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react';
2+
import FormRenderer, { componentTypes, DefaultSchemaError } from '@data-driven-forms/react-form-renderer';
3+
import { FormTemplate, componentMapper } from '@data-driven-forms/pf4-component-mapper';
4+
5+
const schema = {
6+
fields: [
7+
{
8+
component: componentTypes.TEXT_FIELD,
9+
name: 'i-am-okey',
10+
label: 'I am label'
11+
},
12+
{
13+
component: componentTypes.TEXT_FIELD,
14+
name: 'need-label'
15+
}
16+
]
17+
};
18+
19+
const schemaValidatorMapper = {
20+
components: {
21+
[componentTypes.TEXT_FIELD]: (field) => {
22+
if (!field.label) {
23+
throw new DefaultSchemaError(`Missing label prop in "${field.name}" component`);
24+
}
25+
}
26+
}
27+
};
28+
29+
const SchemaValidationExample = () => (
30+
<div className="pf4">
31+
<FormRenderer
32+
FormTemplate={FormTemplate}
33+
componentMapper={componentMapper}
34+
schema={schema}
35+
onSubmit={console.log}
36+
schemaValidatorMapper={schemaValidatorMapper}
37+
/>
38+
</div>
39+
);
40+
41+
export default SchemaValidationExample;

0 commit comments

Comments
 (0)