validateKit

package
v2.8.122 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 License: Apache-2.0 Imports: 4 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 added in v2.2.50

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 added in v2.2.47

func Struct(s interface{}) error

Struct 验证结构体.

@param s 如果为nil,将返回error(e.g. validator: (nil *main.User))

func Var added in v2.8.116

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

Var 验证变量.

Types

type Validatable added in v2.8.109

type Validatable interface {
	Validate() error
}

Validatable 参考: "github.com/ardielle/ardielle-go/rdl"中的 rdl.Validatable

!!!: 如果结构体实现了 Validatable 接口,方法体内部不能调用 validateKit.Struct,以免发生递归死循环(但可以调用 validateKit.New).

Jump to

Keyboard shortcuts

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