ruleimpl

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	REGEX_DOMAIN = `^([0-9a-zA-Z][0-9a-zA-Z\-]{0,62}\.)+([a-zA-Z]{0,62})$`
	REGEX_EMAIL  = `^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)+$`
	//=========================
	REGEX_URL  = `(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]`
	REGEX_IP   = "满足ipv4或者ipv6一个即可"
	REGEX_IPV4 = `^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$`
	REGEX_IPV6 = `` /* 336-byte string literal not displayed */
	REGEX_MAC  = `^([0-9A-Fa-f]{2}[\-:]){5}[0-9A-Fa-f]{2}$`
	//=============================================
	REGEX_PASSPORT = `^[a-zA-Z]{1}\w{5,17}$`
	// 密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线):
	REGEX_PASSWORD = `^[\w\S]{6,18}$`
	// 强密码
	REGEX_PASSWORD2 = `待实现`
	// 强密码(必须包含大小写字母和数字的组合,可以使用特殊字符,长度在8-16之间):
	REGEX_PASSWORD3 = `待实现`
	// 强密码(必须包含大小写字母和数字的组合,可以使用特殊字符(~!@#$%^&*),长度在8-16之间):
	REGEX_PASSWORD4 = `^(.*\d)(.*[a-z])(.*[A-Z])[a-zA-Z0-9~!@#$%^&*]{8,16}$`
	//==============================================
	REGEX_PHONE       = `^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^16[\d]{9}$|^17[0,2,3,5,6,7,8]{1}\d{8}$|^18[\d]{9}$|^19[\d]{9}$`
	REGEX_PHONE_LOOSE = `^1(3|4|5|6|7|8|9)\d{9}$`
	REGEX_POST_CODE   = `^\d{6}$`
	//=============================================
	REGEX_QQ          = `^[1-9][0-9]{4,}$`
	REGEX_RESIDENT_ID = `` /* 166-byte string literal not displayed */
	REGEX_TELPHONE    = `^((\d{3,4})|\d{3,4}-)?\d{7,8}$`
)
View Source
const (
	Required           = "required"
	RequiredIf         = "required-if"
	RequiredUnless     = "required-unless"
	RequiredWith       = "required-with"
	RequiredWithAll    = "required-with-all"
	RequiredWithout    = "required-without"
	RequiredWithoutAll = "required-without-all"
	Between            = "between"
	Size               = "size"
	Length             = "length"
	MaxLength          = "max-length"
	MinLength          = "min-length"
	Lte                = "lte"
	Lt                 = "lt"
	Gte                = "gte"
	Gt                 = "gt"
	NotEq              = "not-eq"
	Eq                 = "eq"
	Different          = "different"
	Same               = "same"
	Max                = "max"
	Min                = "min"
	In                 = "in"
	NotIn              = "not-in"

	//::::::::::::::::::::::::::::::::::::::::::::::::::
	Date       = "date"
	DateTime   = "datetime"
	DataFormat = "date-format"

	Before      = "before"
	BeforeEqual = "before-equal"
	After       = "after"
	AfterEqual  = "after-equal"

	//=============================================
	Array = "#array"
	Enums = "#enums"

	// ==============================================
	RegexDomain = `domain`
	RegexEmail  = `email`
	//=========================
	RegexUrl  = `url`
	RegexIP   = "ip"
	RegexIPV4 = "ipv4"
	RegexIPV6 = "ipv6"
	RegexMAC  = `mac`
	//=============================================
	RegexPassport  = `passport`
	RegexPassword  = `password`
	RegexPassword2 = "password2"
	RegexPassword3 = "password3"
	//==============================================
	RegexPhone     = `phone`
	RegexPhoneLoos = `phone-loose`
	RegexPostCode  = `postcode`
	//=============================================
	RegexQQ         = `qq`
	RegexResidentID = "resident-id"
	RegexTelephone  = `telephone`
	RegexBankCard   = "bank-card"

	// ===============================================
	JsonRuleName    = "json"
	IntegerRuleName = "integer"
	FloatRuleName   = "float"
	BooleanRuleName = "boolean"
	// ==================================================
	RegexRuleName    = "regex"
	NotRegexRuleName = "not-regex"
)

Variables

View Source
var RegexRuleMapToFunc = map[string]ValidFuncImpl{
	RegexQQ:         RegexRuleMatchQQ,
	RegexDomain:     RegexRuleMatchDomain,
	RegexUrl:        RegexRuleMatchUrl,
	RegexMAC:        RegexRuleMatchMac,
	RegexIP:         RegexRuleMatchIp,
	RegexIPV4:       RegexRuleMatchIpv4,
	RegexIPV6:       RegexRuleMatchIpv6,
	RegexBankCard:   RegexRuleMatchBankCard,
	RegexResidentID: RegexRuleMatchResidentId,
	RegexPostCode:   RegexRuleMatchPostCode,
	RegexPassword:   RegexRuleMatchPassword,
	RegexPassword2:  RegexRuleMatchPassword2,
	RegexPassword3:  RegexRuleMatchPassword3,
	RegexPassport:   RegexRuleMatchPassport,
	RegexPhone:      RegexRuleMatchPhone,
	RegexTelephone:  RegexRuleMatchTelephone,
	RegexPhoneLoos:  RegexRuleMatchPhoneLoose,
	RegexEmail:      RegexRuleMatchEmail,
}
View Source
var RuleMsgMap = map[string]string{
	Required:           `The {field} field is required`,
	RequiredIf:         "The {field} field is required",
	RequiredUnless:     "The {field} field is required",
	RequiredWith:       "The {field} field is required",
	RequiredWithAll:    "The {field} field is required",
	RequiredWithout:    "The {field} field is required",
	RequiredWithoutAll: "The {field} field is required",

	Between:   "The {field} value `{value}` must be between {min} and {max}",
	Size:      "The {field} value `{value}` length must be {size}",
	Length:    "The {field} value `{value}` length must be between {min} and {max}",
	MaxLength: "The {field} value `{value}` length must be equal or lesser than {max}",
	MinLength: "The {field} value `{value}` length must be equal or greater than {min}",

	Lte:       "The {field} value `{value}` must be lesser than or equal to field {field1} value `{value1}`",
	Lt:        "The {field} value `{value}` must be lesser than field {field1} value `{value1}`",
	Gte:       "The {field} value `{value}` must be greater than or equal to field {field1} value `{value1}`",
	Gt:        "The {field} value `{value}` must be greater than field {field1} value `{value1}`",
	NotEq:     "The {field} value `{value}` must not be equal to field {field1} value `{value1}`",
	Eq:        "The {field} value `{value}` must be equal to field {field1} value `{value1}`",
	Different: "The {field} value `{value}` must be different from field {field1} value `{value1}`",
	Same:      "The {field} value `{value}` must be the same as field {field1} value `{value1}`",

	Max:   "The {field} value `{value}` must be equal or lesser than {max}",
	Min:   "The {field} value `{value}` must be equal or greater than {min}",
	In:    "The {field} value `{value}` is not in acceptable range: {pattern}",
	NotIn: "The {field} value `{value}` must not be in range: {pattern}",

	RegexDomain: "The {field} value `{value}` is not a valid domain format",
	RegexEmail:  "The {field} value `{value}` is not a valid email address",

	RegexUrl:  "The {field} value `{value}` is not a valid URL address",
	RegexIP:   "The {field} value `{value}` is not a valid IP address",
	RegexIPV4: "The {field} value `{value}` is not a valid IPv4 address",
	RegexIPV6: "The {field} value `{value}` is not a valid IPv6 address",
	RegexMAC:  "The {field} value `{value}` is not a valid MAC address",

	RegexPassport:  "The {field} value `{value}` is not a valid passport format",
	RegexPassword:  "The {field} value `{value}` is not a valid password format",
	RegexPassword2: "The {field} value `{value}` is not a valid password format",
	RegexPassword3: "The {field} value `{value}` is not a valid password format",

	RegexPhone:     "The {field} value `{value}` is not a valid phone number",
	RegexPhoneLoos: "The {field} value `{value}` is not a valid phone number",
	RegexPostCode:  "The {field} value `{value}` is not a valid postcode format",

	RegexQQ:         "The {field} value `{value}` is not a valid QQ number",
	RegexResidentID: "The {field} value `{value}` is not a valid resident id number",
	RegexTelephone:  "The {field} value `{value}` is not a valid telephone number",
	RegexBankCard:   "The {field} value `{value}` is not a valid bank card number",

	JsonRuleName:    "The {field} value `{value}` is not a valid JSON string",
	IntegerRuleName: "The {field} value `{value}` is not an integer",
	FloatRuleName:   "The {field} value `{value}` is not of valid float type",
	BooleanRuleName: "The {field} value `{value}` field must be true or false",

	RegexRuleName:    "The {field} value `{value}` must be in regex of: {pattern}",
	NotRegexRuleName: "The {field} value `{value}` should not be in regex of: {pattern}",

	Date:        "The {field} value `{value}` is not a valid date",
	DataFormat:  "The {field} value `{value}` does not match the format: {pattern}",
	DateTime:    "The {field} value `{value}` is not a valid datetime",
	After:       "The {field} value `{value}` must be after field {field1} value `{value1}`",
	AfterEqual:  "The {field} value `{value}` must be after or equal to field {field1} value `{value1}`",
	Before:      "The {field} value `{value}` must be before field {field1} value `{value1}`",
	BeforeEqual: "The {field} value `{value}` must be before or equal to field {field1} value `{value1}`",
}

Functions

func BooleanFormat

func BooleanFormat(ctx context.Context, input RuleFuncInput) error

func DateRuleFunc

func DateRuleFunc(ctx context.Context, in RuleFuncInput) error

格式: date 说明:参数为常用日期类型,日期之间支持的连接符号-或/或., 也支持不带连接符号的8位长度日期,格式如: 2006-01-02, 2006/01/02, 2006.01.02, 20060102

func DateTimeRuleFunc

func DateTimeRuleFunc(ctx context.Context, in RuleFuncInput) error

格式: datetime 说明:参数为常用日期时间类型,其中日期之间支持的连接符号只支持-,格式如: 2006-01-02 12:00:00

func FloatFormat

func FloatFormat(ctx context.Context, input RuleFuncInput) error

func IntegerFormat

func IntegerFormat(ctx context.Context, input RuleFuncInput) error

func JsonFormat

func JsonFormat(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchBankCard

func RegexRuleMatchBankCard(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchDomain

func RegexRuleMatchDomain(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchEmail

func RegexRuleMatchEmail(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchIp

func RegexRuleMatchIp(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchIpv4

func RegexRuleMatchIpv4(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchIpv6

func RegexRuleMatchIpv6(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchMac

func RegexRuleMatchMac(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchPassport

func RegexRuleMatchPassport(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchPassword

func RegexRuleMatchPassword(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchPassword2

func RegexRuleMatchPassword2(ctx context.Context, input RuleFuncInput) error

格式: password2 说明:中等强度密码(在通用密码规则的基础上,要求密码必须包含大小写字母和数字)。

func RegexRuleMatchPassword3

func RegexRuleMatchPassword3(ctx context.Context, input RuleFuncInput) error

格式: password3 说明:强等强度密码(在通用密码规则的基础上,必须包含大小写字母、数字和特殊字符)。

func RegexRuleMatchPhone

func RegexRuleMatchPhone(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchPhoneLoose

func RegexRuleMatchPhoneLoose(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchPostCode

func RegexRuleMatchPostCode(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchQQ

func RegexRuleMatchQQ(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchResidentId

func RegexRuleMatchResidentId(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchTelephone

func RegexRuleMatchTelephone(ctx context.Context, input RuleFuncInput) error

func RegexRuleMatchUrl

func RegexRuleMatchUrl(ctx context.Context, input RuleFuncInput) error

func RequiredLengthFunc

func RequiredLengthFunc(ctx context.Context, input RuleFuncInput) error

func RequiredPtrFunc

func RequiredPtrFunc(ctx context.Context, input RuleFuncInput) error

func RequiredStringLengthFunc

func RequiredStringLengthFunc(ctx context.Context, input RuleFuncInput) error

Types

type BetweenRuleNumber

type BetweenRuleNumber[T Number] struct {
	Min T
	Max T
	// 当前字段的转换函数
	FieldConvertFunc func(from reflect.Value) T
}

func (*BetweenRuleNumber[T]) Run

func (b *BetweenRuleNumber[T]) Run(ctx context.Context, input RuleFuncInput) error

这里的input按照顺序就是min,max,其他规则同理 格式: between:min,max 说明:参数大小为min到max(支持整形和浮点类型参数)。

type CustomRuleInput

type CustomRuleInput struct {
	Value     reflect.Value
	Args      string
	RuleName  string
	FieldName string

	FieldType reflect.Type
	// 支持直接传递结构体指针,用来获取结构体中其他字段值
	StructPtr reflect.Value
}

type CustomValidRuleFunc

type CustomValidRuleFunc func(ctx context.Context, args *CustomRuleInput) error

type DateFormatRule

type DateFormatRule struct {
	Format string
}

func (*DateFormatRule) Run

格式: date-format:format 说明:判断日期是否为指定的日期/时间格式,format参数格式为gtime日期格式(可以包含日期及时间),格式说明参考章节:gtime模块 示例:date-format:Y-m-d H:i:s

type EqRule

type EqRule[T Number | string | bool] struct {
	// TODO FieldName待删除,直接在解析阶段替换为字段名
	FieldName string
	// 依赖字段的转换函数
	AssocFieldConvertFunc func(from reflect.Value) T
	// 当前字段的转换函数,数字类型转到float64,字符串不转
	FieldConvertFunc func(from reflect.Value) T
	// 关联字段索引
	AssocFieldIndex int
}

比较大小 T 的类型可以是 comparable 这样可以比较任意值类型

func (*EqRule[T]) EqBool added in v1.0.3

func (e *EqRule[T]) EqBool(ctx context.Context, input RuleFuncInput) error

格式: eq:field 说明:参数值必需与field字段参数的值相同。same规则的别名,功能同same规则。 版本:框架版本>=v2.2.0

func (*EqRule[T]) EqNumber

func (e *EqRule[T]) EqNumber(ctx context.Context, input RuleFuncInput) error

格式: eq:field 说明:参数值必需与field字段参数的值相同。same规则的别名,功能同same规则。 版本:框架版本>=v2.2.0

func (*EqRule[T]) EqString

func (e *EqRule[T]) EqString(ctx context.Context, input RuleFuncInput) error

格式: eq:field 说明:参数值必需与field字段参数的值相同。same规则的别名,功能同same规则。 版本:框架版本>=v2.2.0

type Float

type Float interface {
	~float32 | ~float64
}

type GtRuleNumber

type GtRuleNumber[T Number] struct {
	// TODO FieldName待删除,直接在解析阶段替换为字段名
	FieldName string
	// 依赖字段的转换函数
	AssocFieldConvertFunc func(from reflect.Value) T
	// 当前字段的转换函数,数字类型转到float64,字符串不转
	FieldConvertFunc func(from reflect.Value) T
	// 关联字段索引
	AssocFieldIndex int
}

func (*GtRuleNumber[T]) Run

func (g *GtRuleNumber[T]) Run(ctx context.Context, input RuleFuncInput) error

格式: gt:field 说明:参数值必需大于给定字段对应的值。 版本:框架版本>=v2.2.0

type GteRuleNumber

type GteRuleNumber[T Number] struct {
	// TODO FieldName待删除,直接在解析阶段替换为字段名
	FieldName string
	// 依赖字段的转换函数
	AssocFieldConvertFunc func(from reflect.Value) T
	// 当前字段的转换函数,数字类型转到float64,字符串不转
	FieldConvertFunc func(from reflect.Value) T
	// 关联字段索引
	AssocFieldIndex int
}

func (*GteRuleNumber[T]) Run

func (g *GteRuleNumber[T]) Run(ctx context.Context, input RuleFuncInput) error

格式: gte:field 说明:参数值必需大于或等于给定字段对应的值。 版本:框架版本>=v2.2.0

type Integer

type Integer interface {
	UnsignedNumber | SignedNumber
}

type LtRuleNumber

type LtRuleNumber[T Number] struct {
	// TODO FieldName待删除,直接在解析阶段替换为字段名
	FieldName string
	// 依赖字段的转换函数
	AssocFieldConvertFunc func(from reflect.Value) T
	// 当前字段的转换函数,数字类型转到float64,字符串不转
	FieldConvertFunc func(from reflect.Value) T
	// 关联字段索引
	AssocFieldIndex int
}

func (*LtRuleNumber[T]) Run

func (g *LtRuleNumber[T]) Run(ctx context.Context, input RuleFuncInput) error

格式: lt:field 说明:参数值必需小于给定字段对应的值。 版本:框架版本>=v2.2.0

type LteRuleNumber

type LteRuleNumber[T Number] struct {
	// TODO FieldName待删除,直接在解析阶段替换为字段名
	FieldName string
	// 依赖字段的转换函数
	AssocFieldConvertFunc func(from reflect.Value) T
	// 当前字段的转换函数,数字类型转到float64,字符串不转
	FieldConvertFunc func(from reflect.Value) T
	// 关联字段索引
	AssocFieldIndex int
}

func (*LteRuleNumber[T]) Run

func (g *LteRuleNumber[T]) Run(ctx context.Context, input RuleFuncInput) error

格式: lte:field 说明:参数值必需小于或等于给定字段对应的值。 版本:框架版本>=v2.2.0

type MaxRuleNumber

type MaxRuleNumber[T Number] struct {
	Max T
	// 当前字段的转换函数
	FieldConvertFunc func(from reflect.Value) T
}

int64 uint64 float64

func (*MaxRuleNumber[T]) Run

func (b *MaxRuleNumber[T]) Run(ctx context.Context, input RuleFuncInput) error

格式: max:max 说明:参数大小最大为max(支持整形和浮点类型参数)。

type MinRuleNumber

type MinRuleNumber[T Number] struct {
	Min T
	// 当前字段的转换函数
	FieldConvertFunc func(from reflect.Value) T
}

int64 uint64 float64

func (*MinRuleNumber[T]) Run

func (b *MinRuleNumber[T]) Run(ctx context.Context, input RuleFuncInput) error

格式: max:max 说明:参数大小最大为max(支持整形和浮点类型参数)。

type NotEqRule

type NotEqRule[T Number | string | bool] struct {
	// TODO FieldName待删除,直接在解析阶段替换为字段名
	FieldName string
	// 依赖字段的转换函数
	AssocFieldConvertFunc func(from reflect.Value) T
	// 当前字段的转换函数,数字类型转到float64,字符串不转
	FieldConvertFunc func(from reflect.Value) T
	// 关联字段索引
	AssocFieldIndex int
}

格式: not-eq:field 说明:参数值必需与field字段参数的值不相同。different规则的别名,功能同different规则。 版本:框架版本>=v2.2.0

func (*NotEqRule[T]) NotEqBool added in v1.0.3

func (e *NotEqRule[T]) NotEqBool(ctx context.Context, input RuleFuncInput) error

格式: same:field 说明:参数值必需与field字段参数的值相同。 示例:在用户注册时,提交密码Password和确认密码Password2必须相等(服务端校验)。

func (*NotEqRule[T]) NotEqNumber

func (e *NotEqRule[T]) NotEqNumber(ctx context.Context, input RuleFuncInput) error

格式: same:field 说明:参数值必需与field字段参数的值相同。 示例:在用户注册时,提交密码Password和确认密码Password2必须相等(服务端校验)。

func (*NotEqRule[T]) NotEqString

func (e *NotEqRule[T]) NotEqString(ctx context.Context, input RuleFuncInput) error

格式: same:field 说明:参数值必需与field字段参数的值相同。 示例:在用户注册时,提交密码Password和确认密码Password2必须相等(服务端校验)。

type Number

type Number interface {
	Integer | Float
}

type RangeRule

type RangeRule[T Number] struct {
	Values []T
	// 当前字段的转换函数
	FieldConvertFunc func(from reflect.Value) T
}

Format: not-in:value1,value2,... Format: in:value1,value2,...

func (*RangeRule[T]) In

func (r *RangeRule[T]) In(ctx context.Context, input RuleFuncInput) error

格式: in:value1,value2,... 说明:参数值应该在value1,value2,...中(字符串匹配)。

func (*RangeRule[T]) NotIn

func (r *RangeRule[T]) NotIn(ctx context.Context, input RuleFuncInput) error

格式: not-in:value1,value2,... 说明:参数值不应该在value1,value2,...中(字符串匹配)。

type RegexMatch

type RegexMatch struct {
	Pattern *regexp.Regexp
}

func (*RegexMatch) RegexRuleMatch

func (r *RegexMatch) RegexRuleMatch(ctx context.Context, input RuleFuncInput) error

func (*RegexMatch) RegexRuleNoMatch

func (r *RegexMatch) RegexRuleNoMatch(ctx context.Context, input RuleFuncInput) error

type RegisterCustomRuleFunc

type RegisterCustomRuleFunc struct {
	Args      string
	RuleName  string
	FieldName string

	FieldType reflect.Type
	Fn        CustomValidRuleFunc
}

func (*RegisterCustomRuleFunc) RunWithError

func (c *RegisterCustomRuleFunc) RunWithError(ctx context.Context, input RuleFuncInput) error

type RequiredFieldsRule

type RequiredFieldsRule struct {
	//required-without-all: field1,field2,...
	//required-without: field1,field2,...
	//required-with-all: field1,field2,...
	//required-with: field1,field2,...
	// 校验依赖字段是否有值的函数
	// map[依赖字段]=> 依赖字段验证是否有值的函数
	// AssocFieldValidFunc map[string]ValidFunc
	// 依赖的字段名字
	// 判定当前字段是否有值的函数
	IsEmpty ValidFunc
	//  直接存储依赖字段的索引,如果提前满足条件,可以跳过后面的,延迟求值
	AssocFields []RequiredWithRuleArg
}

func (*RequiredFieldsRule) RequiredWith

func (r *RequiredFieldsRule) RequiredWith(ctx context.Context, input RuleFuncInput) (err error)

RequiredWith 格式: required-with:field1,field2,... 说明:必需参数(只要有一个字段有值)。当前字段必须有值

func (*RequiredFieldsRule) RequiredWithAll

func (r *RequiredFieldsRule) RequiredWithAll(ctx context.Context, input RuleFuncInput) (err error)

RequiredWithAll 格式: required-with-all:field1,field2,... 说明:必须参数(全部字段都得有值)。当前字段必须有值 示例:当Id,Name,Gender,WifeName全部不为空时,HusbandName必须不为空。

func (*RequiredFieldsRule) RequiredWithout

func (r *RequiredFieldsRule) RequiredWithout(ctx context.Context, input RuleFuncInput) (err error)

RequiredWithout 格式: required-without:field1,field2,... 说明:必需参数(只要有一个字段为空)。当前字段必须有值 示例:当Id或WifeName为空时,HusbandName必须不为空

func (*RequiredFieldsRule) RequiredWithoutAll

func (r *RequiredFieldsRule) RequiredWithoutAll(ctx context.Context, input RuleFuncInput) (err error)

RequiredWithoutAll 格式: required-without-all:field1,field2,... 说明:必须参数(所有字段都为空时)。当前字段必须有值 示例:当Id和WifeName都为空时,HusbandName必须不为空。当前字段必须有值

type RequiredIfRule

type RequiredIfRule struct {
	// filed => value
	// 校验依赖字段是否有值的函数
	// map[依赖字段]=>  val
	//AssocFieldValues map[string]any
	// 判定当前字段是否有值的函数
	IsEmpty ValidFunc
	// 直接存储依赖字段的索引,如果提前满足条件,可以跳过后面的,延迟求值
	AssocFields []RequiredIfRuleArg
}

func (*RequiredIfRule) Run

func (r *RequiredIfRule) Run(ctx context.Context, input RuleFuncInput) error

Run 格式: required-if:field,value,... 说明:必需参数(当field=value时,当前字段必须有值)。多个字段以,号分隔。 当前字段类型: 符合required规则即可 field类型:基础类型(数字类型,布尔类型,字符串类型) value类型:必须是字面量类型,例如1 false hello,不能是变量

type RequiredIfRuleArg

type RequiredIfRuleArg struct {
	AssocFieldIndex int32
	IsString        bool
	Value           any
}

type RequiredUnlessRule

type RequiredUnlessRule struct {
	// filed => value
	// 校验依赖字段是否有值的函数
	// map[依赖字段]=> 依赖字段验证是否有值的函数
	//AssocFieldValues map[string]any
	// 判定当前字段是否有值的函数
	IsEmpty ValidFunc

	// 直接存储依赖字段的索引,如果提前满足条件,可以跳过后面的,延迟求值
	AssocFields []RequiredIfRuleArg
}

func (*RequiredUnlessRule) Run

Run 格式: required-unless:field,value,... 说明:必需参数(当field!=value时,当前字段必须有值)。多个字段以,号分隔。 当前字段类型: 符合required规则即可 field类型:基础类型(数字类型,布尔类型,字符串类型) value类型:必须是字面量类型,例如1 false hello,不能是变量

type RequiredWithRuleArg

type RequiredWithRuleArg struct {
	// 关联字段的索引
	AssocFieldIndex int
	// 关联字段的校验函数
	AssocFieldValidFunc ValidFunc
}

type RuleFuncInput

type RuleFuncInput struct {
	// this value
	Value reflect.Value

	Message string
	// 关联字段的值
	// AssocFieldValues map[string]any
	// 结构体指针,用来实现关联字段的验证
	StructPtr reflect.Value
}

type SignedNumber

type SignedNumber interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type StringLengthRule

type StringLengthRule struct {
	Min int
	Max int
}

func (*StringLengthRule) Run

func (s *StringLengthRule) Run(ctx context.Context, input RuleFuncInput) error

格式: length:min,max 说明:参数长度为min到max(长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位。

type StringMaxLengthRule

type StringMaxLengthRule struct {
	Max int
}

func (*StringMaxLengthRule) Run

格式: max-length:max 说明:参数长度最大为max(长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位。

type StringMinLengthRule

type StringMinLengthRule struct {
	Min int
}

func (*StringMinLengthRule) Run

格式: min-length:min 说明:参数长度最小为min(长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位。

type StringSizeRule

type StringSizeRule struct {
	Size int
}

func (*StringSizeRule) Run

func (s *StringSizeRule) Run(ctx context.Context, input RuleFuncInput) error

格式: size:size 说明:参数长度为 size (长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位。

type TimeRule

type TimeRule struct {
	FieldName string
	// 关联字段索引
	AssocFieldIndex int
}

func (*TimeRule) After

func (t *TimeRule) After(ctx context.Context, input RuleFuncInput) error

格式: after:field 说明:判断给定的日期/时间是否在指定字段的日期/时间之后。 版本:框架版本>=v2.2.0

func (*TimeRule) AfterEqual

func (t *TimeRule) AfterEqual(ctx context.Context, input RuleFuncInput) error

格式: after-equal:field 说明:判断给定的日期/时间是否在指定字段的日期/时间之后,或者与指定字段的日期/时间相等。 版本:框架版本>=v2.2.0

func (*TimeRule) Before

func (t *TimeRule) Before(ctx context.Context, input RuleFuncInput) error

格式: before:field 说明:判断给定的日期/时间是否在指定字段的日期/时间之前。 版本:框架版本>=v2.2.0

func (*TimeRule) BeforeEqual

func (t *TimeRule) BeforeEqual(ctx context.Context, input RuleFuncInput) error

格式: before-equal:field 说明:判断给定的日期/时间是否在指定字段的日期/时间之前,或者与指定字段的日期/时间相等。 版本:框架版本>=v2.2.0

type UnsignedNumber

type UnsignedNumber interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

type ValidFunc

type ValidFunc interface {
	Run(ctx context.Context, input RuleFuncInput) error
}

type ValidFuncImpl

type ValidFuncImpl func(ctx context.Context, input RuleFuncInput) error

func (ValidFuncImpl) Run

func (f ValidFuncImpl) Run(ctx context.Context, input RuleFuncInput) error

Jump to

Keyboard shortcuts

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