Skip to content

Commit

Permalink
Add more schemas to validate against (rjsf-team#1130)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
aerfio authored and Troy Jaeger committed Mar 5, 2019
1 parent 3876f03 commit 2237e19
Show file tree
Hide file tree
Showing 9 changed files with 615 additions and 177 deletions.
33 changes: 33 additions & 0 deletions docs/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,39 @@ render((
> received as second argument.
> - The `validate()` function is called **after** the JSON schema validation.
### Custom schema validation

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:

```json
{
"$schema": "http://json-schema.org/draft-04/schema#",
...
}
```

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:

```jsx
const additionalMetaSchemas = require("ajv/lib/refs/json-schema-draft-04.json");

render((
<Form schema={schema}
additionalMetaSchemas={[additionalMetaSchemas]}/>
), document.getElementById("app"));
```

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.

`additionalMetaSchemas` also accepts more than one meta schema:

```jsx
render((
<Form schema={schema}
additionalMetaSchemas={[metaSchema1, metaSchema2]} />
), document.getElementById("app"));
```

### Custom error messages

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.
Expand Down
Loading

0 comments on commit 2237e19

Please sign in to comment.