-
Notifications
You must be signed in to change notification settings - Fork 9.1k
fix(spec): format validation errors for nested parameters #9775
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
Changes from 2 commits
2dcc387
ae32bfe
1a6d813
58d9886
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -494,12 +494,38 @@ export const validationErrors = (state, pathMethod) => { | |
let paramValues = state.getIn(["meta", "paths", ...pathMethod, "parameters"], fromJS([])) | ||
const result = [] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about doing the following? if (paramValues.length === 0) return result There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. |
||
paramValues.forEach( (p) => { | ||
const getErrorsWithPaths = (errors, path = []) => { | ||
const getNestedErrorsWithPaths = (e, path) => { | ||
const currPath = [...path, e.get("propKey") || e.get("index")] | ||
return Map.isMap(e.get("error")) | ||
? getErrorsWithPaths(e.get("error"), currPath) | ||
: { error: e.get("error"), path: currPath } | ||
} | ||
|
||
return List.isList(errors) | ||
? errors.map((e) => (Map.isMap(e) ? getNestedErrorsWithPaths(e, path) : { error: e, path })) | ||
: getNestedErrorsWithPaths(errors, path) | ||
} | ||
|
||
const formatError = (error, path, paramName) => { | ||
path = path.reduce((acc, curr) => { | ||
return typeof curr === "number" | ||
? `${acc}[${curr}]` | ||
: acc | ||
? `${acc}.${curr}` | ||
: curr | ||
}, "") | ||
return `For '${paramName}'${path ? ` at path '${path}'` : ""}: ${error}.` | ||
} | ||
|
||
paramValues.forEach( (p, key) => { | ||
const paramName = key.split(".").slice(1, -1).join(".") | ||
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)) | ||
const errorsWithPaths= getErrorsWithPaths(errors) | ||
errorsWithPaths.forEach(({error, path}) => { | ||
result.push(formatError(error, path, paramName)) | ||
}) | ||
} | ||
}) | ||
return result | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.