Formatters package create validation response like:
{
"message": "Unprocessable Entity",
"errors": [
{
"attribute": "email",
"validator": {
"name": "email"
}
},
{
"attribute": "password",
"validator": {
"name": "required"
}
}
]
}
go get github.com/amomama/formatters
// Handler code
request := security.NewAuthenticateRequest(ctx, sh.validator)
err := request.Validate()
if err != nil {
code, response := formatters.ValidationResponse(err)
return ctx.Status(code).JSON(response)
}
// Handler code
func (handler *MenuHandler) Create(w http.ResponseWriter, r *http.Request) {
err = handler.validator.Validate(menuDTO)
if err != nil {
code, response := formatters.ValidationResponse(err)
w.WriteHeader(code)
data, _ := := json.Marshal(response)
w.Write(data)
return
}
...
}