validation

package
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PhoneValidate = NewValidator("phoneValidate",
		func(fl validator.FieldLevel) bool {
			ok, _ := regexp.MatchString(`^1[3-9][0-9]{9}$`, fl.Field().String())
			return ok
		}, func(v Validation) validator.RegisterTranslationsFunc {
			return func(ut ut.Translator) error {
				return ut.Add(v.Tag(), "{0}不是一个合法的手机号", true)
			}
		}, func(v Validation) validator.TranslationFunc {
			return func(ut ut.Translator, fe validator.FieldError) string {
				t, _ := ut.T(v.Tag(), fe.Field())
				return t
			}
		})

	EmailValidate = NewValidator("emailValidate",
		func(fl validator.FieldLevel) bool {
			ok, _ := regexp.MatchString(`^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$`, fl.Field().String())
			return ok
		}, func(v Validation) validator.RegisterTranslationsFunc {
			return func(ut ut.Translator) error {
				return ut.Add(v.Tag(), "{0}不是一个合法的邮箱", true)
			}
		}, func(v Validation) validator.TranslationFunc {
			return func(ut ut.Translator, fe validator.FieldError) string {
				t, _ := ut.T(v.Tag(), fe.Field())
				return t
			}
		})
)

Functions

func GetDefaultValidator added in v1.5.2

func GetDefaultValidator() *validator.Validate

func GetTranslator

func GetTranslator() ut.Translator

func LoadValidation added in v1.5.2

func LoadValidation(valids []Validation)

func ValidatorTranslate

func ValidatorTranslate(err error) any

Types

type Validation

type Validation interface {
	Tag() string
	Validator() validator.Func
	RegisterTranslation() validator.RegisterTranslationsFunc
	Translation() validator.TranslationFunc
}

func NewValidator

func NewValidator(tag string,
	validFunc validator.Func,
	regTranFunc func(Validation) validator.RegisterTranslationsFunc,
	tranFunc func(Validation) validator.TranslationFunc,
) Validation

Jump to

Keyboard shortcuts

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