validatorV3

package
v1.64.58 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`
    
    	`github.com/aid297/aid/validator/validatorV3`
    )
    
    type UserRequest struct {
    	Firstname string  `json:"firstname" v-rule:"(required)(not-empty)" v-name:"firstname"`
    	Lastname  *string `json:"lastname" v-rule:"(required)" v-name:"lastname"`
    }
    
    func main() {
    	userRequest := UserRequest{}
    
    	wrong := validatorV3.APP.Validator.Once().Checker(&userRequest).Validate().WrongToString("\n")
    	fmt.Printf("错误:\n%v", wrong)
    	// 错误:
      // 问题1:[firstname] 不能为空 注意:如果字符串,数字这种类型的变量零值不会认为是空,只有字段不存在时才会认为是空,所以需要加入not-empty或者min>0的限制
    	// 问题2:[lastname] 不能为空
    }
    

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