validatorV3

package
v1.64.59 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

README

表单验证器

  1. 概览

    package main
    
    import (
    	`fmt`
    	`time`
    
    	`github.com/aid297/aid/ptr`
    	`github.com/aid297/aid/validator/validatorV3`
    )
    
    type UserRequest struct {
    	Firstname string    `json:"firstname" v-rule:"(min>0)" v-name:"姓"`
    	Lastname  *string   `json:"lastname" v-rule:"(required)(min>2)" v-name:"名"`
    	Age       int       `json:"int" v-rule:"(min>=18)(max<100)" v-name:"年龄"`
    	Score     int       `json:"score" v-rule:"(required)" v-name:"分数"`
    	Birthday  time.Time `json:"birthday" v-rule:"(required)" v-name:"生日"`
    }
    
    func main() {
    	userRequest := UserRequest{
    		Firstname: "",
    		Lastname:  ptr.New("张"),
    		Age:       180,
    	}
    
    	checker := validatorV3.APP.Validator.Once().Checker(&userRequest)
    
    	checker.Validate()
    	fmt.Printf("验证结果:%v\n", checker.OK())
    
    	for _, wrong := range checker.Wrongs() {
    		fmt.Printf("%v\n", wrong)
    	}
    	// 验证结果:false
    	// [姓] 长度错误 期望:> 0
    	// [名] 长度错误 期望:> 2
    	// [年龄] 长度错误 期望:< 100
    	// [生日] 不能为空
    }
    
  2. 规则说明:string

    type UserRequest struct {
    	Firstname string    `json:"firstname" v-rule:"(required)(min>0)(max<64)" v-name:"姓"`
    }
    
    • string类型支持验证规则:requiredmin>min>=max<max<=in:a,b,cnot-in:a,b,csize=size!=ex:fn-a,fn-b,fn-c
    • required:当前字符串长度不能等于0
    • min>:当前字符串长度不能小于xmax<同理。
    • min>=:当前字符串长度必须大于xmax<=同理。
    • innot-in :当前字符必须在x范围内或在x范围外。
    • size=size!=:当前字符串长度必须等于x或必须不等于x
    • ex:额外执行的验证程序,需要提前在验证器中注册。
  3. 规则说明:*string

    type UserRequest struct {
    	Firstname *string `json:"firstname" v-rule:"(required)(min>0)(max<64)" v-name:"姓"`
    }
    
    • *string类型支持验证规则:与string一致
    • required:当前字符串不能为nil长度不能等于0
  4. 规则说明:intint8int16int32int64

    type UserRequest struct {
    	Age int `v-rule:"(min>0)(max<100)" v-name:"年龄"`
    }
    
    • int类型支持验证规则:min>、min>=max<max<=innot-insize=size!=ex:fn-a,fn-b,fn-c
  5. 规则说明:*int*int8*int16*int32*int64

    type UserRequest struct {
    	Age *int `v-rule:"(min>0)(max<100)" v-name:"年龄"`
    }
    
    • *int类型支持验证规则:requiredmin>、min>=max<max<=innot-insize=size!=ex:fn-a,fn-b,fn-c
  6. 其他普通类型支持:uintuint8uint16uint32uint64*uint*uint8*uint16*uint32*uint64float32float64*float32*float64

  7. 特殊类型支持:time.Time*time.Time

    • 规则支持:requiredmin>、min>=max<max<=innot-inex:fn-a,fn-b,fn-c

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRequired      = errors.New("必填")
	ErrNotEmpty      = errors.New("不能为空")
	ErrInvalidLength = errors.New("长度错误")
	ErrInvalidValue  = errors.New("内容错误")
	ErrInvalidFormat = errors.New("格式错误")
	ErrInvalidType   = errors.New("类型错误")
)
View Source
var APP struct{ Validator Validator }

Functions

This section is empty.

Types

type Check added in v1.64.30

type Check struct {
	// contains filtered or unexported fields
}

Check 验证器

func (*Check) OK added in v1.64.30

func (my *Check) OK() bool

func (*Check) Validate added in v1.64.30

func (my *Check) Validate(exCheckFns ...any) Checker

func (*Check) Wrong added in v1.64.30

func (my *Check) Wrong() error

func (*Check) WrongToString added in v1.64.30

func (my *Check) WrongToString(limit string) (ret string)

func (*Check) Wrongs added in v1.64.30

func (my *Check) Wrongs() []error

type Checker added in v1.54.7

type Checker interface {
	Wrongs() []error
	OK() bool
	Wrong() error
	WrongToString(limit string) (ret string)
	Validate(exCheckFns ...any) Checker
}

func NewCheck added in v1.64.56

func NewCheck(data any) Checker

func WithFiber added in v1.54.2

func WithFiber[T any](c *fiber.Ctx, exCheckFns ...any) (form T, checker Checker)

func WithGin added in v1.54.2

func WithGin[T any](c *gin.Context, exCheckFns ...any) (form T, checker Checker)

type FieldInfo

type FieldInfo struct {
	Name      string // 字段名
	Value     any    // 实际值
	Kind      reflect.Kind
	Type      reflect.Type
	IsPtr     bool                        // 是否是指针
	IsNil     bool                        // 是否为空指针
	IsZero    bool                        // 是否是零值
	Required  bool                        // 是否必填
	VRuleTags anyArrayV2.AnyArray[string] // v-rule tag 的值
	VNameTags anyArrayV2.AnyArray[string] // v-name tag 的值
	// contains filtered or unexported fields
}

FieldInfo 保存了字段的相关信息。

func (FieldInfo) Check

func (my FieldInfo) Check() FieldInfo

func (FieldInfo) Wrongs

func (my FieldInfo) Wrongs() []error

type FieldRule

type FieldRule struct{ FieldRuleBase }

type FieldRuleBase

type FieldRuleBase struct{ Required string }

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

func (*Validator) Checker added in v1.54.7

func (*Validator) Checker(data any) Checker

func (*Validator) GetExFn added in v1.54.7

func (*Validator) GetExFn(key string) func(any) (err error)

func (*Validator) Once added in v1.56.6

func (*Validator) Once() *Validator

func (*Validator) RegisterExFn added in v1.54.11

func (*Validator) RegisterExFn(key string, fn func(any) (err error)) *Validator

Jump to

Keyboard shortcuts

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