From 68ab7a4e4932f35afa7cd646e8dedd5c1d579cb6 Mon Sep 17 00:00:00 2001 From: Ashwin Ramaswami Date: Wed, 6 Mar 2019 14:32:13 -0800 Subject: [PATCH] Add schemaPath to errors object in transformErrors (2) (#1205) * #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 --- docs/validation.md | 9 +++++++++ src/validate.js | 3 ++- test/validate_test.js | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/validation.md b/docs/validation.md index 70e7afa805..9f259729aa 100644 --- a/docs/validation.md +++ b/docs/validation.md @@ -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. diff --git a/src/validate.js b/src/validate.js index 5a51b62bcf..9e8d34f114 100644 --- a/src/validate.js +++ b/src/validate.js @@ -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 @@ -152,6 +152,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 09b54404ca..b881a8e396 100644 --- a/test/validate_test.js +++ b/test/validate_test.js @@ -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"); }); @@ -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" );