echo_binder

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2022 License: GPL-3.0 Imports: 8 Imported by: 0

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

View Source
const (
	PathField   string = "Path"
	QueryField  string = "Query"
	BodyField   string = "Body"
	FormField   string = "Form"
	HeaderField string = "Header"

	TagIdentifier string = "binder"
)

Variables

View Source
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

func GetInvalidAnonymousFieldError

func GetInvalidAnonymousFieldError(location string) error

func GetInvalidTypeAtLocationError

func GetInvalidTypeAtLocationError(location string) error

func GetMissingParamAtLocationError

func GetMissingParamAtLocationError(location, param string) error

func GetNotSettableParamAtLocationError

func GetNotSettableParamAtLocationError(location, param string) error

func GetUnsupportedHttpMethodError

func GetUnsupportedHttpMethodError(location, method string) error

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.

func New

func New() *Binder

func (Binder) Bind

func (binder Binder) Bind(i interface{}, c echo.Context) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL