Skip to content

Commit 92233e4

Browse files
aerfioepicfaace
authored andcommitted
Add more schemas to validate against (#1130)
* Add more schemas to validate against * Edit validate.js so that it calls addMetaSchema conditionally * Add props which accepts custom schemas for ajv * remove unnecesary fallback * rename variable name * add documentation for `metaSchema` prop * Update docs/validation.md Co-Authored-By: aerfio <[email protected]> * Update src/validate.js Co-Authored-By: aerfio <[email protected]> * Update src/validate.js Co-Authored-By: aerfio <[email protected]> * Update docs/validation.md Co-Authored-By: aerfio <[email protected]> * Update docs/validation.md Co-Authored-By: aerfio <[email protected]> * Update docs/validation.md Co-Authored-By: aerfio <[email protected]> * return errors from validation in validateFormData * add initial tests for meta schemas * add one proper test case * add more test cases * Update docs/validation.md Co-Authored-By: aerfio <[email protected]> * Add test cases for multiple metaSchemas in validateFormData * Change prop name and update docs * Change additionalMetaSchemas prop to array only and remove console.error * make sure that validateFormData's additionalMetaShemas argument is always an array * Commit "broken" test to see whether they pass * Fix tests and create new Ajv instance when additionalMetaSchemas changes * fix: create new ajv instance when additionalMetaSchemas prop is null, add tests * handle validation errors * fix tests and delete validation errors * Delete unneeded field in return statement * dont stop showing red overlay when validation errors are present * Make errors and test more sensible * rename variable * handle only $schema related errors * fix tests
1 parent aebfab9 commit 92233e4

File tree

9 files changed

+615
-177
lines changed

9 files changed

+615
-177
lines changed

docs/validation.md

+33
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,39 @@ render((
4747
> received as second argument.
4848
> - The `validate()` function is called **after** the JSON schema validation.
4949
50+
### Custom schema validation
51+
52+
To have your schemas validated against any other meta schema than draft-07 (the current version of [JSON Schema](http://json-schema.org/)), make sure your schema has a `$schema` attribute that enables the validator to use the correct meta schema. For example:
53+
54+
```json
55+
{
56+
"$schema": "http://json-schema.org/draft-04/schema#",
57+
...
58+
}
59+
```
60+
61+
Note that react-jsonschema-form only supports the latest version of JSON Schema, draft-07, by default. To support additional meta schemas pass them through the `additionalMetaSchemas` prop to your `Form` component:
62+
63+
```jsx
64+
const additionalMetaSchemas = require("ajv/lib/refs/json-schema-draft-04.json");
65+
66+
render((
67+
<Form schema={schema}
68+
additionalMetaSchemas={[additionalMetaSchemas]}/>
69+
), document.getElementById("app"));
70+
```
71+
72+
In this example `schema` passed as props to `Form` component can be validated against draft-07 (default) and by draft-04 (added), depending on the value of `$schema` attribute.
73+
74+
`additionalMetaSchemas` also accepts more than one meta schema:
75+
76+
```jsx
77+
render((
78+
<Form schema={schema}
79+
additionalMetaSchemas={[metaSchema1, metaSchema2]} />
80+
), document.getElementById("app"));
81+
```
82+
5083
### Custom error messages
5184

5285
Validation error messages are provided by the JSON Schema validation by default. If you need to change these messages or make any other modifications to the errors from the JSON Schema validation, you can define a transform function that receives the list of JSON Schema errors and returns a new list.

0 commit comments

Comments
 (0)