Skip to content

Commit

Permalink
fix(spec): format validation errors for nested parameters
Browse files Browse the repository at this point in the history
Refs #9774
  • Loading branch information
glowcloud committed Apr 3, 2024
1 parent 3110954 commit 2dcc387
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/core/plugins/spec/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions test/unit/core/plugins/spec/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
]
}
}
}
}
}
}
Expand All @@ -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"
])
})
})

0 comments on commit 2dcc387

Please sign in to comment.