Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spec): format validation errors for nested parameters #9775

Merged
merged 4 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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([]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let paramValues = state.getIn(["meta", "paths", ...pathMethod, "parameters"], fromJS([]))
const paramValues = state.getIn(["meta", "paths", ...pathMethod, "parameters"], fromJS([]))

const result = []

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about doing the following?

if (paramValues.length === 0) return result

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to make it more clear what's happening if we don't have these values?
If so, makes sense to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to make it more clear what's happening if we don't have these values?

Well primary factor is performance here. This is not a memoized selector, which means it's executing always fully. We want to return as soon as we can to avoid logic of creating inner functions and performing the reductions as soon as possible.

const formatErrors = (errors) => {
char0n marked this conversation as resolved.
Show resolved Hide resolved
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"
])
})
})