diff --git a/docs/validation.md b/docs/validation.md index 4978ba7070..1425305ced 100644 --- a/docs/validation.md +++ b/docs/validation.md @@ -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. diff --git a/src/validate.js b/src/validate.js index 80d599bd43..43c1f27ee1 100644 --- a/src/validate.js +++ b/src/validate.js @@ -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 @@ -139,6 +139,7 @@ function transformAjvErrors(errors = []) { message, params, // specific to ajv stack: `${property} ${message}`.trim(), + schemaPath, }; }); } diff --git a/test/validate_test.js b/test/validate_test.js index 072ed61ec2..0b294532c3 100644 --- a/test/validate_test.js +++ b/test/validate_test.js @@ -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"); }); @@ -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" );