fix(spec): format validation errors for nested parameters (#9775)
Refs #9774
This commit is contained in:
@@ -491,15 +491,43 @@ export const canExecuteScheme = ( state, path, method ) => {
|
||||
|
||||
export const validationErrors = (state, pathMethod) => {
|
||||
pathMethod = pathMethod || []
|
||||
let paramValues = state.getIn(["meta", "paths", ...pathMethod, "parameters"], fromJS([]))
|
||||
const paramValues = state.getIn(["meta", "paths", ...pathMethod, "parameters"], fromJS([]))
|
||||
const result = []
|
||||
|
||||
paramValues.forEach( (p) => {
|
||||
let errors = p.get("errors")
|
||||
if (paramValues.length === 0) return result
|
||||
|
||||
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(".")
|
||||
const 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
|
||||
|
||||
Reference in New Issue
Block a user