Skip to content

Commit

Permalink
Add schemaPath to errors object in transformErrors (2) (#1205)
Browse files Browse the repository at this point in the history
* #838 return schemaPath in transformErrors

* #818 document structure of errors object in transformErrors

* fix: take into account ajv bug

* doc: re-add doc for transformErrors
  • Loading branch information
epicfaace authored Mar 6, 2019
1 parent 58a3534 commit 68ab7a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docs/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,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 @@ -142,7 +142,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 @@ -152,6 +152,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 @@ -304,6 +304,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 @@ -410,6 +411,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

0 comments on commit 68ab7a4

Please sign in to comment.