Skip to content

amomama/formatters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AMO Formatters

Formatters package create validation response like:

{
    "message": "Unprocessable Entity",
    "errors": [
        {
            "attribute": "email",
            "validator": {
                "name": "email"
            }
        },
        {
            "attribute": "password",
            "validator": {
                "name": "required"
            }
        }
    ]
}

Install

go get github.com/amomama/formatters

Examples

In fiber

// 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)
}

With net.http

// 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
    }
	
	...
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages