validateKit

package
v3.0.94 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Email

func Email(field interface{}) error

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 IPv4

func IPv4(field interface{}) error

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 Port

func Port(field interface{}) error

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 StructCtx

func StructCtx(ctx context.Context, s interface{}) error

func StructExcept

func StructExcept(s interface{}, fields ...string) error

StructExcept 验证结构体,排除指定的字段.

@param fields 支持嵌套

func StructExceptCtx

func StructExceptCtx(ctx context.Context, s interface{}, fields ...string) error

func StructFiltered

func StructFiltered(s interface{}, fn validator.FilterFunc) error

StructFiltered 验证结构体,过滤指定的字段.

func StructFilteredCtx

func StructFilteredCtx(ctx context.Context, s interface{}, fn validator.FilterFunc) error

func StructPartial

func StructPartial(s interface{}, fields ...string) error

StructPartial 验证结构体,只验证指定的字段.

@param fields 支持嵌套

func StructPartialCtx

func StructPartialCtx(ctx context.Context, s interface{}, fields ...string) error

func ValidateMap

func ValidateMap(data map[string]interface{}, rules map[string]interface{}) map[string]interface{}

ValidateMap 验证map.

func ValidateMapCtx

func ValidateMapCtx(ctx context.Context, data map[string]interface{}, rules map[string]interface{}) map[string]interface{}

func Var

func Var(field interface{}, tag string) error

Var 验证变量.

func VarCtx

func VarCtx(ctx context.Context, field interface{}, tag string) error

func VarWithValue

func VarWithValue(field interface{}, other interface{}, tag string) error

VarWithValue 用于验证一个值是否满足另一个值的某种条件.

@param field 要验证的值 @param other 要与之比较的值 @param tag 一个表示验证规则的字符串

func VarWithValueCtx

func VarWithValueCtx(ctx context.Context, field interface{}, other interface{}, tag string) error

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).

Jump to

Keyboard shortcuts

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