diff --git a/src/core/plugins/spec/selectors.js b/src/core/plugins/spec/selectors.js index 82607ac8f1f..8dd36dc3ce2 100644 --- a/src/core/plugins/spec/selectors.js +++ b/src/core/plugins/spec/selectors.js @@ -494,12 +494,21 @@ export const validationErrors = (state, pathMethod) => { let paramValues = state.getIn(["meta", "paths", ...pathMethod, "parameters"], fromJS([])) const result = [] + const formatErrors = (errors) => { + const stringifyMap = (e) => { + return `${e.get("propKey") || e.get("index")}: ${ + Map.isMap(e.get("error")) ? formatErrors(e.get("error")) : e.get("error") + }` + } + return List.isList(errors) + ? errors.map((e) => (Map.isMap(e) ? stringifyMap(e) : e)) + : stringifyMap(errors) + } + paramValues.forEach( (p) => { let errors = p.get("errors") if (errors && errors.count()) { - errors - .map((e) => (Map.isMap(e) ? `${e.get("propKey")}: ${e.get("error")}` : e)) - .forEach((e) => result.push(e)) + formatErrors(errors).forEach((e) => result.push(e)) } }) return result diff --git a/test/unit/core/plugins/spec/selectors.js b/test/unit/core/plugins/spec/selectors.js index 29f6ca25a34..d26a93efe5b 100644 --- a/test/unit/core/plugins/spec/selectors.js +++ b/test/unit/core/plugins/spec/selectors.js @@ -1411,6 +1411,52 @@ describe("validationErrors", function() { } } } + }, + "/nested": { + post: { + parameters: { + arrayWithObjects: { + errors: [ + { + error: "Parameter string value must be valid JSON", + index: 0 + }, + { + error: "Value must be a string", + index: 1 + } + ] + }, + objectWithArray: { + errors: [ + { + error: { + error: { + error: "Value must be a number", + propKey: "b", + }, + index: 0, + }, + propKey: "a", + } + ] + }, + objectWithoutArray: { + errors: [ + { + error: { + error: { + error: "Value must be a string", + propKey: "e", + }, + propKey: "d", + }, + propKey: "c", + } + ] + } + } + } } } } @@ -1432,4 +1478,15 @@ describe("validationErrors", function() { "name: Value must be a string" ]) }) + + it("should return formatted validation errors for nested parameters", function () { + const result = validationErrors(state, ["/nested", "post"]) + + expect(result).toEqual([ + "0: Parameter string value must be valid JSON", + "1: Value must be a string", + "a: 0: b: Value must be a number", + "c: d: e: Value must be a string" + ]) + }) })