Documentation ¶
Overview ¶
A custom binder for the echo web framework that replaces echo's DefaultBinder. This one supports the same syntax as gongular's binder and uses go-playground/validator to validate the binded structs.
Index ¶
- Constants
- Variables
- func BadRequestError(err error) *echo.HTTPError
- func GetInvalidAnonymousFieldError(location string) error
- func GetInvalidTypeAtLocationError(location string) error
- func GetMissingParamAtLocationError(location, param string) error
- func GetNotSettableParamAtLocationError(location, param string) error
- func GetUnsupportedHttpMethodError(location, method string) error
- type Binder
Constants ¶
Variables ¶
var ( ErrorInvalidType = errors.New("binding element must be a pointer to a struct") ErrorInvalidAnonymousField = errors.New("binding element cannot have embedded fields that arent struct") )
Functions ¶
func BadRequestError ¶
func BadRequestError(err error) *echo.HTTPError
Types ¶
type Binder ¶
type Binder struct {
// contains filtered or unexported fields
}
A replacement for the echo.DefaultBinder that binds the Path, Query, Header, Body and Form params into nested structures that passed into the binder, and finally valiate the structure with the go-playground/validator package. For more information about the validator check: https://pkg.go.dev/github.com/go-playground/validator
To use this binder, just add it to the echo.Echo instance:
e := echo.New() e.Binder = echo_binder.New()
For example, for this struct defined:
type RequestExample struct { Body struct { Name string `json:"name" validate:"required"` } Query struct { PostId int `binder:"postId" validate:"required"` } Path struct { UserId int `binder:"id" validate:"required"` } Header struct { AcceptLanguage string `binder:"Accept-Language"` UserAgent string `binder:"User-Agent"` } }
And this code execution:
func requestHandler(c echo.Context) error { user := &RequestExample{} if err := binder.Bind(user, c); err != nil { return err } // Do something with the request }
The binder will bind the following params: From the body, the name field will be bound to the Name field of the struct. From the query, the postId field will be bound to the PostId field of the struct. From the path, the id field will be bound to the UserId field of the struct. From the header, the Accept-Language field will be bound to the AcceptLanguage field of the struct. From the header, the User-Agent field will be bound to the UserAgent field of the struct.