Documentation ¶
Index ¶
- func Email(field interface{}) error
- func File(field interface{}) error
- func HttpUrl(field interface{}) error
- func IP(field interface{}) error
- func IPv4(field interface{}) error
- func Json(field interface{}) error
- func New(tagNameArgs ...string) *validator.Validate
- func Port(field interface{}) error
- func Required(field interface{}) error
- func Struct(s interface{}) error
- func StructCtx(ctx context.Context, s interface{}) error
- func StructExcept(s interface{}, fields ...string) error
- func StructExceptCtx(ctx context.Context, s interface{}, fields ...string) error
- func StructFiltered(s interface{}, fn validator.FilterFunc) error
- func StructFilteredCtx(ctx context.Context, s interface{}, fn validator.FilterFunc) error
- func StructPartial(s interface{}, fields ...string) error
- func StructPartialCtx(ctx context.Context, s interface{}, fields ...string) error
- func ValidateMap(data map[string]interface{}, rules map[string]interface{}) map[string]interface{}
- func ValidateMapCtx(ctx context.Context, data map[string]interface{}, rules map[string]interface{}) map[string]interface{}
- func Var(field interface{}, tag string) error
- func VarCtx(ctx context.Context, field interface{}, tag string) error
- func VarWithValue(field interface{}, other interface{}, tag string) error
- func VarWithValueCtx(ctx context.Context, field interface{}, other interface{}, tag string) error
- type Validatable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func File ¶
func File(field interface{}) error
File 字符串值是否包含有效的文件路径,以及该文件是否存在于计算机上.
PS: 传参对应的应当是"文件",是"目录"的话会返回error. e.g. fmt.Println(validateKit.File("")) // Key: '' Error:Var validation for '' failed on the 'file' tag // 目录存在 fmt.Println(validateKit.File("_chimera-lib")) // Key: '' Error:Var validation for '' failed on the 'file' tag fmt.Println(validateKit.File("/Users/richelieu/GolandProjects/chimera/_chimera-lib")) // Key: '' Error:Var validation for '' failed on the 'file' tag // 文件存在 fmt.Println(validateKit.File("_chimera-lib/config.yaml")) // <nil> fmt.Println(validateKit.File("/Users/richelieu/GolandProjects/chimera/_chimera-lib/config.yaml")) // <nil> // 文件不存在 fmt.Println(validateKit.File("/Users/richelieu/GolandProjects/chimera/_chimera-lib/config111.yaml")) // Key: '' Error:Var validation for '' failed on the 'file' tag // 无效的文件路径 fmt.Println(validateKit.File("chimera-lib\\config.yaml")) // Key: '' Error:Var validation for '' failed on the 'file' tag
func HttpUrl ¶
func HttpUrl(field interface{}) error
HttpUrl
PS: 要以 "http://" 或 "https://" 开头. e.g. fmt.Println(validateKit.HttpUrl("")) // Key: '' Error:Var validation for '' failed on the 'http_url' tag fmt.Println(validateKit.HttpUrl("https://github.com/go-playground/validator")) // <nil> fmt.Println(validateKit.HttpUrl("http://github.com/go-playground/validator")) // <nil> fmt.Println(validateKit.HttpUrl("ftp://github.com/go-playground/validator")) // Key: '' Error:Var validation for '' failed on the 'http_url' tag
func IP ¶
func IP(field interface{}) error
IP
e.g. fmt.Println(validateKit.IP("")) // Key: '' Error:Var validation for '' failed on the 'ip' tag fmt.Println(validateKit.IP("127.0.0.1")) // <nil> fmt.Println(validateKit.IP("127.001")) // Key: '' Error:Var validation for '' failed on the 'ip' tag
func Json ¶
func Json(field interface{}) error
Json 字符串值是否为有效的JSON.
e.g. fmt.Println(validateKit.Json("")) // Key: '' Error:Var validation for '' failed on the 'json' tag fmt.Println(validateKit.Json("[]")) // <nil> fmt.Println(validateKit.Json("{}")) // <nil> fmt.Println(validateKit.Json("[}")) // Key: '' Error:Var validation for '' failed on the 'json' tag
func New ¶
func New(tagNameArgs ...string) *validator.Validate
New
PS: (1) 默认的tag name: "validate"; (2) Gin使用的tag name: "binding".
@param tagNameArgs 不传参的话,将采用默认的tag name
func Required ¶
func Required(field interface{}) error
Required 必填,非零值(zero value)
e.g. fmt.Println(validateKit.Required(nil)) // Key: '' Error:Var validation for '' failed on the 'required' tag fmt.Println(validateKit.Required("")) // Key: '' Error:Var validation for '' failed on the 'required' tag fmt.Println(validateKit.Required("aaa")) // <nil> fmt.Println(validateKit.Required(0)) // Key: '' Error:Var validation for '' failed on the 'required' tag fmt.Println(validateKit.Required(1)) // <nil> fmt.Println(validateKit.Required(false)) // Key: '' Error:Var validation for '' failed on the 'required' tag fmt.Println(validateKit.Required(true)) // <nil>
func Struct ¶
func Struct(s interface{}) error
Struct 验证结构体.
@param s 如果为nil,将返回error(e.g. validator: (nil *main.User))
func StructExceptCtx ¶
func StructFiltered ¶
func StructFiltered(s interface{}, fn validator.FilterFunc) error
StructFiltered 验证结构体,过滤指定的字段.
func StructFilteredCtx ¶
func StructPartialCtx ¶
func ValidateMap ¶
ValidateMap 验证map.
func ValidateMapCtx ¶
func VarWithValue ¶
VarWithValue 用于验证一个值是否满足另一个值的某种条件.
@param field 要验证的值 @param other 要与之比较的值 @param tag 一个表示验证规则的字符串
Types ¶
type Validatable ¶
type Validatable interface {
Validate() error
}
Validatable Deprecated: 纯粹用 github.com/go-playground/validator/v10 吧.
参考: "github.com/ardielle/ardielle-go/rdl"中的 rdl.Validatable !!!: 如果结构体实现了 Validatable 接口,方法体内部不能调用 validateKit.Struct,以免发生递归死循环(但可以调用 validateKit.Sign).
Click to show internal directories.
Click to hide internal directories.