Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ render((
> Notes:
> - The `transformErrors()` function must return the list of errors. Modifying the list in place without returning it will result in an error.

Each element in the `errors` list passed to `transformErrors` has the following properties:

- `name`: name of the error, for example, "required" or "minLength"
- `message`: message, for example, "is a required property" or "should NOT be shorter than 3 characters"
- `params`: an object with the error params returned by ajv ([see doc](https://github.com/epoberezkin/ajv#error-parameters) for more info).
- `property`: a string in Javascript property accessor notation to the data path of the field with the error. For example, `.name` or `['first-name']`.
- `stack`: full error name, for example ".name is a required property".
- `schemaPath`: JSON pointer to the schema of the keyword that failed validation. For example, `#/fields/firstName/required`. (Note: this may sometimes be wrong due to a [https://github.com/epoberezkin/ajv/issues/512](bug in ajv)).

### Error List Display

To disable rendering of the error list at the top of the form, you can set the `showErrorList` prop to `false`. Doing so will still validate the form, but only the inline display will show.
Expand Down
3 changes: 2 additions & 1 deletion src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function transformAjvErrors(errors = []) {
}

return errors.map(e => {
const { dataPath, keyword, message, params } = e;
const { dataPath, keyword, message, params, schemaPath } = e;
let property = `${dataPath}`;

// put data in expected format
Expand All @@ -139,6 +139,7 @@ function transformAjvErrors(errors = []) {
message,
params, // specific to ajv
stack: `${property} ${message}`.trim(),
schemaPath,
};
});
}
Expand Down
4 changes: 4 additions & 0 deletions test/validate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ describe("Validation", () => {
expect(errors).to.have.length.of(1);
expect(errors[0].name).eql("type");
expect(errors[0].property).eql(".properties['foo'].required");
expect(errors[0].schemaPath).eql("#/definitions/stringArray/type"); // TODO: This schema path is wrong due to a bug in ajv; change this test when https://github.com/epoberezkin/ajv/issues/512 is fixed.
expect(errors[0].message).eql("should be array");
});

Expand Down Expand Up @@ -345,6 +346,9 @@ describe("Validation", () => {

it("should validate a minLength field", () => {
expect(comp.state.errors).to.have.length.of(1);
expect(comp.state.errors[0].schemaPath).eql(
"#/properties/foo/minLength"
);
expect(comp.state.errors[0].message).eql(
"should NOT be shorter than 10 characters"
);
Expand Down