validateKit

package
v2.2.63 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Email

func Email(field interface{}) error

func Field

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

Field 验证字段.

func File

func File(field interface{}) error

File 字符串值是否包含有效的文件路径,以及该文件是否存在于计算机上.

PS: 传参对应的应当是"文件",是"目录"的话会返回error.

e.g.
	fmt.Println(validateKit.File("")) // Key: '' Error:Field validation for '' failed on the 'file' tag

	// 目录存在
	fmt.Println(validateKit.File("_chimera-lib"))                                         // Key: '' Error:Field validation for '' failed on the 'file' tag
	fmt.Println(validateKit.File("/Users/richelieu/GolandProjects/chimera/_chimera-lib")) // Key: '' Error:Field 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:Field validation for '' failed on the 'file' tag
	// 无效的文件路径
	fmt.Println(validateKit.File("chimera-lib\\config.yaml")) // Key: '' Error:Field 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:Field 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:Field validation for '' failed on the 'http_url' tag

func IP

func IP(field interface{}) error

IP

e.g.
	fmt.Println(validateKit.IP(""))          // Key: '' Error:Field validation for '' failed on the 'ip' tag
	fmt.Println(validateKit.IP("127.0.0.1")) // <nil>
	fmt.Println(validateKit.IP("127.001"))   // Key: '' Error:Field 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:Field validation for '' failed on the 'json' tag
	fmt.Println(validateKit.Json("[]")) // <nil>
	fmt.Println(validateKit.Json("{}")) // <nil>
	fmt.Println(validateKit.Json("[}")) // Key: '' Error:Field validation for '' failed on the 'json' tag

func New

func New(tagNameArgs ...string) *validator.Validate

New

@param tagNameArgs 不传参的话,将采用默认的tag name("validate")

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:Field validation for '' failed on the 'required' tag

	fmt.Println(validateKit.Required(""))    	// Key: '' Error:Field validation for '' failed on the 'required' tag
	fmt.Println(validateKit.Required("aaa")) 	// <nil>

	fmt.Println(validateKit.Required(0)) 		// Key: '' Error:Field validation for '' failed on the 'required' tag
	fmt.Println(validateKit.Required(1)) 		// <nil>

	fmt.Println(validateKit.Required(false)) 	// Key: '' Error:Field 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 验证结构体.

Types

This section is empty.

Jump to

Keyboard shortcuts

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