MCPcopy Index your code
hub / github.com/labstack/echo / ExampleValueBinder_BindErrors

Function ExampleValueBinder_BindErrors

binder_external_test.go:17–53  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15)
16
17func ExampleValueBinder_BindErrors() {
18 // example route function that binds query params to different destinations and returns all bind errors in one go
19 routeFunc := func(c *echo.Context) error {
20 var opts struct {
21 IDs []int64
22 Active bool
23 }
24 length := int64(50) // default length is 50
25
26 b := echo.QueryParamsBinder(c)
27
28 errs := b.Int64("length", &length).
29 Int64s("ids", &opts.IDs).
30 Bool("active", &opts.Active).
31 BindErrors() // returns all errors
32 if errs != nil {
33 for _, err := range errs {
34 bErr := err.(*echo.BindingError)
35 log.Printf("in case you want to access what field: %s values: %v failed", bErr.Field, bErr.Values)
36 }
37 return fmt.Errorf("%v fields failed to bind", len(errs))
38 }
39 fmt.Printf("active = %v, length = %v, ids = %v", opts.Active, length, opts.IDs)
40
41 return c.JSON(http.StatusOK, opts)
42 }
43
44 e := echo.New()
45 c := e.NewContext(
46 httptest.NewRequest(http.MethodGet, "/api/endpoint?active=true&length=25&ids=1&ids=2&ids=3", nil),
47 httptest.NewRecorder(),
48 )
49
50 _ = routeFunc(c)
51
52 // Output: active = true, length = 25, ids = [1 2 3]
53}
54
55func ExampleValueBinder_BindError() {
56 // example route function that binds query params to different destinations and stops binding on first bind error

Callers

nothing calls this directly

Calls 6

JSONMethod · 0.95
BindErrorsMethod · 0.80
BoolMethod · 0.80
Int64sMethod · 0.80
Int64Method · 0.80
NewContextMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…