# README
bytedance/go-tagexpr
This is a "binder and validator" extended by bytedance/go-tagexpr.
It can be used by hertz as a "binder" or "validator".
Usage
- default validate tag:
vd
- validate rule: bytedance/go-tagexpr
package main
import (
"context"
"fmt"
"net/http"
"time"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/hertz-contrib/binding/go_tagexpr"
)
type Test struct {
Q int `query:"q" vd:"$>100"`
}
func main() {
binder := go_tagexpr.NewBinder()
// If you need to configure the validator, you can use the following
//go_tagexpr.MustRegTypeUnmarshal
h := server.New(server.WithCustomBinder(binder))
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
var req Test
err := ctx.BindAndValidate(&req)
if err != nil {
fmt.Println(err)
ctx.String(400, err.Error())
return
}
fmt.Println(req)
ctx.JSON(200, req)
})
go h.Spin()
time.Sleep(100 * time.Millisecond)
hc := http.Client{Timeout: 1000 * time.Second}
hc.Get("http://127.0.0.1:8888/ping?q=99")
time.Sleep(1 * time.Second)
}
# Packages
No description provided by the author
# Functions
B2s converts byte slice to a string without memory allocation.
Bind binds data from *protocol.Request to obj.
BindAndValidate binds data from *protocol.Request to obj and validates them if needed.
MustRegTypeUnmarshal registers unmarshal function of type.
MustRegValidateFunc registers validator function expression.
No description provided by the author
No description provided by the author
S2b converts string to a byte slice without memory allocation.
SetErrorFactory customizes the factory of validation error.
SetLooseZeroMode if set to true, the empty string request parameter is bound to the zero value of parameter.
UseGJSONUnmarshaler uses github.com/bytedance/go-tagexpr/v2/binding/gjson as json library NOTE:
UseGJSONUnmarshaler will remain in effect once it has been called.
UseStdJSONUnmarshaler uses encoding/json as json library NOTE:
The current version uses encoding/json by default.
UseThirdPartyJSONUnmarshaler uses third-party json library for binding NOTE:
UseThirdPartyJSONUnmarshaler will remain in effect once it has been called.
Validate validates obj with "vd" tag NOTE:
obj should be a pointer.