You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure of why you would like to change the param name that failed. withMessage accepts an object as a param, so the right place to perform this kind of error response should be there.
e.g.
.withMessage({reason: "name_esists",message: "name is duplicated"})
Hi, I know this doesn't good practice but you know that we could meet some projects that just need an array of errors key, such as mapping their i18n keys like: for(key in keys) return errors[key]
I have rewrite the result as my needs:
const format = (e: ValidationError): Result<any> => {
return typeof e.msg === "object"
? e.msg
: e.nestedErrors
? e.msg !== "Invalid value(s)"
? { require_one: e.msg } // show error message if not default message
: { require_one: e.nestedErrors.reduce((o, er) => Object.assign(o, this.format(er)), {} as ValidationError) }
: { [e.param]: e.msg };
}
const result = validationResult(this.req).formatWith(e => format(e));
...but it's not good at all.
Everything would be great if this provides a simple method to change it independently.
My project needs to return a custom error format including the error param/key, so please consider to add this feature.
This new additional method could be
.withParam()
similar to the.withMessage()
Examples
this returns
{ "name" : "string only" }
Expected
this returns
{ "name_exists" : "string only" }
The text was updated successfully, but these errors were encountered: