builtin

package
v0.0.0-...-ac9d817 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package builtin implements built-in validation rules.

Referred to Laravel validation: https://laravel.com/docs/master/validation#available-validation-rules

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(rule Rule)

Register registers builtin rule into manager.

Types

type Rule

type Rule interface {
	// Name returns the builtin name of the rule.
	Name() string

	// Message returns the default error message of the rule.
	Message() string

	// Run starts running the rule, it returns nil if successful, or else an error.
	Run(in RunInput) error
}

func GetRule

func GetRule(name string) Rule

GetRule retrieves and returns rule by `name`.

type RuleAfter

type RuleAfter struct{}

RuleAfter implements `after` rule: The datetime value should be after the value of field `field`.

Format: after:field

func (RuleAfter) Message

func (r RuleAfter) Message() string

func (RuleAfter) Name

func (r RuleAfter) Name() string

func (RuleAfter) Run

func (r RuleAfter) Run(in RunInput) error

type RuleAfterEqual

type RuleAfterEqual struct{}

RuleAfterEqual implements `after-equal` rule: The datetime value should be after or equal to the value of field `field`.

Format: after-equal:field

func (RuleAfterEqual) Message

func (r RuleAfterEqual) Message() string

func (RuleAfterEqual) Name

func (r RuleAfterEqual) Name() string

func (RuleAfterEqual) Run

func (r RuleAfterEqual) Run(in RunInput) error

type RuleArray

type RuleArray struct{}

RuleArray implements `array` rule: Value should be type of array.

Format: array

func (RuleArray) Message

func (r RuleArray) Message() string

func (RuleArray) Name

func (r RuleArray) Name() string

func (RuleArray) Run

func (r RuleArray) Run(in RunInput) error

type RuleBail

type RuleBail struct{}

RuleBail implements `bail` rule: Stop validating when this field's validation failed.

Format: bail

func (RuleBail) Message

func (r RuleBail) Message() string

func (RuleBail) Name

func (r RuleBail) Name() string

func (RuleBail) Run

func (r RuleBail) Run(in RunInput) error

type RuleBankCard

type RuleBankCard struct{}

RuleBankCard implements `bank-card` rule: Bank card number.

Format: bank-card

func (RuleBankCard) Message

func (r RuleBankCard) Message() string

func (RuleBankCard) Name

func (r RuleBankCard) Name() string

func (RuleBankCard) Run

func (r RuleBankCard) Run(in RunInput) error

type RuleBefore

type RuleBefore struct{}

RuleBefore implements `before` rule: The datetime value should be after the value of field `field`.

Format: before:field

func (RuleBefore) Message

func (r RuleBefore) Message() string

func (RuleBefore) Name

func (r RuleBefore) Name() string

func (RuleBefore) Run

func (r RuleBefore) Run(in RunInput) error

type RuleBeforeEqual

type RuleBeforeEqual struct{}

RuleBeforeEqual implements `before-equal` rule: The datetime value should be after or equal to the value of field `field`.

Format: before-equal:field

func (RuleBeforeEqual) Message

func (r RuleBeforeEqual) Message() string

func (RuleBeforeEqual) Name

func (r RuleBeforeEqual) Name() string

func (RuleBeforeEqual) Run

func (r RuleBeforeEqual) Run(in RunInput) error

type RuleBetween

type RuleBetween struct{}

RuleBetween implements `between` rule: Range between :min and :max. It supports both integer and float.

Format: between:min,max

func (RuleBetween) Message

func (r RuleBetween) Message() string

func (RuleBetween) Name

func (r RuleBetween) Name() string

func (RuleBetween) Run

func (r RuleBetween) Run(in RunInput) error

type RuleBoolean

type RuleBoolean struct{}

RuleBoolean implements `boolean` rule: Boolean(1,true,on,yes:true | 0,false,off,no,"":false)

Format: boolean

func (RuleBoolean) Message

func (r RuleBoolean) Message() string

func (RuleBoolean) Name

func (r RuleBoolean) Name() string

func (RuleBoolean) Run

func (r RuleBoolean) Run(in RunInput) error

type RuleCi

type RuleCi struct{}

RuleCi implements `ci` rule: Case-Insensitive configuration for those rules that need value comparison like: same, different, in, not-in, etc.

Format: ci

func (RuleCi) Message

func (r RuleCi) Message() string

func (RuleCi) Name

func (r RuleCi) Name() string

func (RuleCi) Run

func (r RuleCi) Run(in RunInput) error

type RuleDate

type RuleDate struct{}

RuleDate implements `date` rule: Standard date, like: 2006-01-02, 20060102, 2006.01.02.

Format: date

func (RuleDate) Message

func (r RuleDate) Message() string

func (RuleDate) Name

func (r RuleDate) Name() string

func (RuleDate) Run

func (r RuleDate) Run(in RunInput) error

type RuleDateFormat

type RuleDateFormat struct{}

RuleDateFormat implements `date-format` rule: Custom date format.

Format: date-format:format

func (RuleDateFormat) Message

func (r RuleDateFormat) Message() string

func (RuleDateFormat) Name

func (r RuleDateFormat) Name() string

func (RuleDateFormat) Run

func (r RuleDateFormat) Run(in RunInput) error

type RuleDatetime

type RuleDatetime struct{}

RuleDatetime implements `datetime` rule: Standard datetime, like: 2006-01-02 12:00:00.

Format: datetime

func (RuleDatetime) Message

func (r RuleDatetime) Message() string

func (RuleDatetime) Name

func (r RuleDatetime) Name() string

func (RuleDatetime) Run

func (r RuleDatetime) Run(in RunInput) error

type RuleDifferent

type RuleDifferent struct{}

RuleDifferent implements `different` rule: Value should be different from value of field.

Format: different:field

func (RuleDifferent) Message

func (r RuleDifferent) Message() string

func (RuleDifferent) Name

func (r RuleDifferent) Name() string

func (RuleDifferent) Run

func (r RuleDifferent) Run(in RunInput) error

type RuleDomain

type RuleDomain struct{}

RuleDomain implements `domain` rule: Domain.

Format: domain

func (RuleDomain) Message

func (r RuleDomain) Message() string

func (RuleDomain) Name

func (r RuleDomain) Name() string

func (RuleDomain) Run

func (r RuleDomain) Run(in RunInput) error

type RuleEmail

type RuleEmail struct{}

RuleEmail implements `email` rule: Email address.

Format: email

func (RuleEmail) Message

func (r RuleEmail) Message() string

func (RuleEmail) Name

func (r RuleEmail) Name() string

func (RuleEmail) Run

func (r RuleEmail) Run(in RunInput) error

type RuleEnums

type RuleEnums struct{}

RuleEnums implements `enums` rule: Value should be in enums of its constant type.

Format: enums

func (RuleEnums) Message

func (r RuleEnums) Message() string

func (RuleEnums) Name

func (r RuleEnums) Name() string

func (RuleEnums) Run

func (r RuleEnums) Run(in RunInput) error

type RuleEq

type RuleEq struct{}

RuleEq implements `eq` rule: Value should be the same as value of field.

This rule performs the same as rule `same`.

Format: eq:field

func (RuleEq) Message

func (r RuleEq) Message() string

func (RuleEq) Name

func (r RuleEq) Name() string

func (RuleEq) Run

func (r RuleEq) Run(in RunInput) error

type RuleFloat

type RuleFloat struct{}

RuleFloat implements `float` rule: Float. Note that an integer is actually a float number.

Format: float

func (RuleFloat) Message

func (r RuleFloat) Message() string

func (RuleFloat) Name

func (r RuleFloat) Name() string

func (RuleFloat) Run

func (r RuleFloat) Run(in RunInput) error

type RuleForeach

type RuleForeach struct{}

RuleForeach implements `foreach` rule: It tells the next validation using current value as an array and validates each of its element.

Format: foreach

func (RuleForeach) Message

func (r RuleForeach) Message() string

func (RuleForeach) Name

func (r RuleForeach) Name() string

func (RuleForeach) Run

func (r RuleForeach) Run(in RunInput) error

type RuleGT

type RuleGT struct{}

RuleGT implements `gt` rule: Greater than `field`. It supports both integer and float.

Format: gt:field

func (RuleGT) Message

func (r RuleGT) Message() string

func (RuleGT) Name

func (r RuleGT) Name() string

func (RuleGT) Run

func (r RuleGT) Run(in RunInput) error

type RuleGTE

type RuleGTE struct{}

RuleGTE implements `gte` rule: Greater than or equal to `field`. It supports both integer and float.

Format: gte:field

func (RuleGTE) Message

func (r RuleGTE) Message() string

func (RuleGTE) Name

func (r RuleGTE) Name() string

func (RuleGTE) Run

func (r RuleGTE) Run(in RunInput) error

type RuleIn

type RuleIn struct{}

RuleIn implements `in` rule: Value should be in: value1,value2,...

Format: in:value1,value2,...

func (RuleIn) Message

func (r RuleIn) Message() string

func (RuleIn) Name

func (r RuleIn) Name() string

func (RuleIn) Run

func (r RuleIn) Run(in RunInput) error

type RuleInteger

type RuleInteger struct{}

RuleInteger implements `integer` rule: Integer.

Format: integer

func (RuleInteger) Message

func (r RuleInteger) Message() string

func (RuleInteger) Name

func (r RuleInteger) Name() string

func (RuleInteger) Run

func (r RuleInteger) Run(in RunInput) error

type RuleIp

type RuleIp struct{}

RuleIp implements `ip` rule: IPv4/IPv6.

Format: ip

func (RuleIp) Message

func (r RuleIp) Message() string

func (RuleIp) Name

func (r RuleIp) Name() string

func (RuleIp) Run

func (r RuleIp) Run(in RunInput) error

type RuleIpv4

type RuleIpv4 struct{}

RuleIpv4 implements `ipv4` rule: IPv4.

Format: ipv4

func (RuleIpv4) Message

func (r RuleIpv4) Message() string

func (RuleIpv4) Name

func (r RuleIpv4) Name() string

func (RuleIpv4) Run

func (r RuleIpv4) Run(in RunInput) error

type RuleIpv6

type RuleIpv6 struct{}

RuleIpv6 implements `ipv6` rule: IPv6.

Format: ipv6

func (RuleIpv6) Message

func (r RuleIpv6) Message() string

func (RuleIpv6) Name

func (r RuleIpv6) Name() string

func (RuleIpv6) Run

func (r RuleIpv6) Run(in RunInput) error

type RuleJson

type RuleJson struct{}

RuleJson implements `json` rule: JSON.

Format: json

func (RuleJson) Message

func (r RuleJson) Message() string

func (RuleJson) Name

func (r RuleJson) Name() string

func (RuleJson) Run

func (r RuleJson) Run(in RunInput) error

type RuleLT

type RuleLT struct{}

RuleLT implements `lt` rule: Lesser than `field`. It supports both integer and float.

Format: lt:field

func (RuleLT) Message

func (r RuleLT) Message() string

func (RuleLT) Name

func (r RuleLT) Name() string

func (RuleLT) Run

func (r RuleLT) Run(in RunInput) error

type RuleLTE

type RuleLTE struct{}

RuleLTE implements `lte` rule: Lesser than or equal to `field`. It supports both integer and float.

Format: lte:field

func (RuleLTE) Message

func (r RuleLTE) Message() string

func (RuleLTE) Name

func (r RuleLTE) Name() string

func (RuleLTE) Run

func (r RuleLTE) Run(in RunInput) error

type RuleLength

type RuleLength struct{}

RuleLength implements `length` rule: Length between :min and :max. The length is calculated using unicode string, which means one chinese character or letter both has the length of 1.

Format: length:min,max

func (RuleLength) Message

func (r RuleLength) Message() string

func (RuleLength) Name

func (r RuleLength) Name() string

func (RuleLength) Run

func (r RuleLength) Run(in RunInput) error

type RuleMac

type RuleMac struct{}

RuleMac implements `mac` rule: MAC.

Format: mac

func (RuleMac) Message

func (r RuleMac) Message() string

func (RuleMac) Name

func (r RuleMac) Name() string

func (RuleMac) Run

func (r RuleMac) Run(in RunInput) error

type RuleMax

type RuleMax struct{}

RuleMax implements `max` rule: Equal or lesser than :max. It supports both integer and float.

Format: max:max

func (RuleMax) Message

func (r RuleMax) Message() string

func (RuleMax) Name

func (r RuleMax) Name() string

func (RuleMax) Run

func (r RuleMax) Run(in RunInput) error

type RuleMaxLength

type RuleMaxLength struct{}

RuleMaxLength implements `max-length` rule: Length is equal or lesser than :max. The length is calculated using unicode string, which means one chinese character or letter both has the length of 1.

Format: max-length:max

func (RuleMaxLength) Message

func (r RuleMaxLength) Message() string

func (RuleMaxLength) Name

func (r RuleMaxLength) Name() string

func (RuleMaxLength) Run

func (r RuleMaxLength) Run(in RunInput) error

type RuleMin

type RuleMin struct{}

RuleMin implements `min` rule: Equal or greater than :min. It supports both integer and float.

Format: min:min

func (RuleMin) Message

func (r RuleMin) Message() string

func (RuleMin) Name

func (r RuleMin) Name() string

func (RuleMin) Run

func (r RuleMin) Run(in RunInput) error

type RuleMinLength

type RuleMinLength struct{}

RuleMinLength implements `min-length` rule: Length is equal or greater than :min. The length is calculated using unicode string, which means one chinese character or letter both has the length of 1.

Format: min-length:min

func (RuleMinLength) Message

func (r RuleMinLength) Message() string

func (RuleMinLength) Name

func (r RuleMinLength) Name() string

func (RuleMinLength) Run

func (r RuleMinLength) Run(in RunInput) error

type RuleNotEq

type RuleNotEq struct{}

RuleNotEq implements `not-eq` rule: Value should be different from value of field.

Format: not-eq:field

func (RuleNotEq) Message

func (r RuleNotEq) Message() string

func (RuleNotEq) Name

func (r RuleNotEq) Name() string

func (RuleNotEq) Run

func (r RuleNotEq) Run(in RunInput) error

type RuleNotIn

type RuleNotIn struct{}

RuleNotIn implements `not-in` rule: Value should not be in: value1,value2,...

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

func (RuleNotIn) Message

func (r RuleNotIn) Message() string

func (RuleNotIn) Name

func (r RuleNotIn) Name() string

func (RuleNotIn) Run

func (r RuleNotIn) Run(in RunInput) error

type RuleNotRegex

type RuleNotRegex struct{}

RuleNotRegex implements `not-regex` rule: Value should not match custom regular expression pattern.

Format: not-regex:pattern

func (RuleNotRegex) Message

func (r RuleNotRegex) Message() string

func (RuleNotRegex) Name

func (r RuleNotRegex) Name() string

func (RuleNotRegex) Run

func (r RuleNotRegex) Run(in RunInput) error

type RulePassport

type RulePassport struct{}

RulePassport implements `passport` rule: Universal passport format rule: Starting with letter, containing only numbers or underscores, length between 6 and 18

Format: passport

func (RulePassport) Message

func (r RulePassport) Message() string

func (RulePassport) Name

func (r RulePassport) Name() string

func (RulePassport) Run

func (r RulePassport) Run(in RunInput) error

type RulePassword

type RulePassword struct{}

RulePassword implements `password` rule: Universal password format rule1: Containing any visible chars, length between 6 and 18.

Format: password

func (RulePassword) Message

func (r RulePassword) Message() string

func (RulePassword) Name

func (r RulePassword) Name() string

func (RulePassword) Run

func (r RulePassword) Run(in RunInput) error

type RulePassword2

type RulePassword2 struct{}

RulePassword2 implements `password2` rule: Universal password format rule2: Must meet password rule1, must contain lower and upper letters and numbers.

Format: password2

func (RulePassword2) Message

func (r RulePassword2) Message() string

func (RulePassword2) Name

func (r RulePassword2) Name() string

func (RulePassword2) Run

func (r RulePassword2) Run(in RunInput) error

type RulePassword3

type RulePassword3 struct{}

RulePassword3 implements `password3` rule: Universal password format rule3: Must meet password rule1, must contain lower and upper letters, numbers and special chars.

Format: password3

func (RulePassword3) Message

func (r RulePassword3) Message() string

func (RulePassword3) Name

func (r RulePassword3) Name() string

func (RulePassword3) Run

func (r RulePassword3) Run(in RunInput) error

type RulePhone

type RulePhone struct{}

RulePhone implements `phone` rule:

  1. China Mobile: 134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188, 178(4G), 147(Net); 172

  2. China Unicom: 130, 131, 132, 155, 156, 185, 186 ,176(4G), 145(Net), 175

  3. China Telecom: 133, 153, 180, 181, 189, 177(4G)

  4. Satelite: 1349

  5. Virtual: 170, 173

  6. 2018: 16x, 19x

Format: phone

func (RulePhone) Message

func (r RulePhone) Message() string

func (RulePhone) Name

func (r RulePhone) Name() string

func (RulePhone) Run

func (r RulePhone) Run(in RunInput) error

type RulePhoneLoose

type RulePhoneLoose struct{}

RulePhoneLoose implements `phone-loose` rule: Loose mobile phone number verification(宽松的手机号验证) As long as the 11 digits numbers beginning with 13, 14, 15, 16, 17, 18, 19 can pass the verification (只要满足 13、14、15、16、17、18、19开头的11位数字都可以通过验证).

Format: phone-loose

func (RulePhoneLoose) Message

func (r RulePhoneLoose) Message() string

func (RulePhoneLoose) Name

func (r RulePhoneLoose) Name() string

func (RulePhoneLoose) Run

func (r RulePhoneLoose) Run(in RunInput) error

type RulePostcode

type RulePostcode struct{}

RulePostcode implements `postcode` rule: Postcode number.

Format: postcode

func (RulePostcode) Message

func (r RulePostcode) Message() string

func (RulePostcode) Name

func (r RulePostcode) Name() string

func (RulePostcode) Run

func (r RulePostcode) Run(in RunInput) error

type RuleQQ

type RuleQQ struct{}

RuleQQ implements `qq` rule: Tencent QQ number.

Format: qq

func (RuleQQ) Message

func (r RuleQQ) Message() string

func (RuleQQ) Name

func (r RuleQQ) Name() string

func (RuleQQ) Run

func (r RuleQQ) Run(in RunInput) error

type RuleRegex

type RuleRegex struct{}

RuleRegex implements `regex` rule: Value should match custom regular expression pattern.

Format: regex:pattern

func (RuleRegex) Message

func (r RuleRegex) Message() string

func (RuleRegex) Name

func (r RuleRegex) Name() string

func (RuleRegex) Run

func (r RuleRegex) Run(in RunInput) error

type RuleRequired

type RuleRequired struct{}

RuleRequired implements `required` rule. Format: required

func (RuleRequired) Message

func (r RuleRequired) Message() string

func (RuleRequired) Name

func (r RuleRequired) Name() string

func (RuleRequired) Run

func (r RuleRequired) Run(in RunInput) error

type RuleRequiredIf

type RuleRequiredIf struct{}

RuleRequiredIf implements `required-if` rule: Required unless all given field and its value are equal.

Format: required-if:field,value,... Example: required-if: id,1,age,18

func (RuleRequiredIf) Message

func (r RuleRequiredIf) Message() string

func (RuleRequiredIf) Name

func (r RuleRequiredIf) Name() string

func (RuleRequiredIf) Run

func (r RuleRequiredIf) Run(in RunInput) error

type RuleRequiredUnless

type RuleRequiredUnless struct{}

RuleRequiredUnless implements `required-unless` rule: Required unless all given field and its value are not equal.

Format: required-unless:field,value,... Example: required-unless:id,1,age,18

func (RuleRequiredUnless) Message

func (r RuleRequiredUnless) Message() string

func (RuleRequiredUnless) Name

func (r RuleRequiredUnless) Name() string

func (RuleRequiredUnless) Run

func (r RuleRequiredUnless) Run(in RunInput) error

type RuleRequiredWith

type RuleRequiredWith struct{}

RuleRequiredWith implements `required-with` rule: Required if any of given fields are not empty.

Format: required-with:field1,field2,... Example: required-with:id,name

func (RuleRequiredWith) Message

func (r RuleRequiredWith) Message() string

func (RuleRequiredWith) Name

func (r RuleRequiredWith) Name() string

func (RuleRequiredWith) Run

func (r RuleRequiredWith) Run(in RunInput) error

type RuleRequiredWithAll

type RuleRequiredWithAll struct{}

RuleRequiredWithAll implements `required-with-all` rule: Required if all given fields are not empty.

Format: required-with-all:field1,field2,... Example: required-with-all:id,name

func (RuleRequiredWithAll) Message

func (r RuleRequiredWithAll) Message() string

func (RuleRequiredWithAll) Name

func (r RuleRequiredWithAll) Name() string

func (RuleRequiredWithAll) Run

type RuleRequiredWithout

type RuleRequiredWithout struct{}

RuleRequiredWithout implements `required-without` rule: Required if any of given fields are empty.

Format: required-without:field1,field2,... Example: required-without:id,name

func (RuleRequiredWithout) Message

func (r RuleRequiredWithout) Message() string

func (RuleRequiredWithout) Name

func (r RuleRequiredWithout) Name() string

func (RuleRequiredWithout) Run

type RuleRequiredWithoutAll

type RuleRequiredWithoutAll struct{}

RuleRequiredWithoutAll implements `required-without-all` rule: Required if all given fields are empty.

Format: required-without-all:field1,field2,... Example: required-without-all:id,name

func (RuleRequiredWithoutAll) Message

func (r RuleRequiredWithoutAll) Message() string

func (RuleRequiredWithoutAll) Name

func (r RuleRequiredWithoutAll) Name() string

func (RuleRequiredWithoutAll) Run

type RuleResidentId

type RuleResidentId struct{}

RuleResidentId implements `resident-id` rule: Resident id number.

Format: resident-id

func (RuleResidentId) Message

func (r RuleResidentId) Message() string

func (RuleResidentId) Name

func (r RuleResidentId) Name() string

func (RuleResidentId) Run

func (r RuleResidentId) Run(in RunInput) error

type RuleSame

type RuleSame struct{}

RuleSame implements `same` rule: Value should be the same as value of field.

Format: same:field

func (RuleSame) Message

func (r RuleSame) Message() string

func (RuleSame) Name

func (r RuleSame) Name() string

func (RuleSame) Run

func (r RuleSame) Run(in RunInput) error

type RuleSize

type RuleSize struct{}

RuleSize implements `size` rule: Length must be :size. The length is calculated using unicode string, which means one chinese character or letter both has the length of 1.

Format: size:size

func (RuleSize) Message

func (r RuleSize) Message() string

func (RuleSize) Name

func (r RuleSize) Name() string

func (RuleSize) Run

func (r RuleSize) Run(in RunInput) error

type RuleTelephone

type RuleTelephone struct{}

RuleTelephone implements `telephone` rule: "XXXX-XXXXXXX" "XXXX-XXXXXXXX" "XXX-XXXXXXX" "XXX-XXXXXXXX" "XXXXXXX" "XXXXXXXX"

Format: telephone

func (RuleTelephone) Message

func (r RuleTelephone) Message() string

func (RuleTelephone) Name

func (r RuleTelephone) Name() string

func (RuleTelephone) Run

func (r RuleTelephone) Run(in RunInput) error

type RuleUrl

type RuleUrl struct{}

RuleUrl implements `url` rule: URL.

Format: url

func (RuleUrl) Message

func (r RuleUrl) Message() string

func (RuleUrl) Name

func (r RuleUrl) Name() string

func (RuleUrl) Run

func (r RuleUrl) Run(in RunInput) error

type RunInput

type RunInput struct {
	RuleKey     string       // RuleKey is like the "max" in rule "max: 6"
	RulePattern string       // RulePattern is like "6" in rule:"max:6"
	Field       string       // The field name of Value.
	ValueType   reflect.Type // ValueType specifies the type of the value, which might be nil.
	Value       *gvar.Var    // Value specifies the value for this rule to validate.
	Data        *gvar.Var    // Data specifies the `data` which is passed to the Validator.
	Message     string       // Message specifies the custom error message or configured i18n message for this rule.
	Option      RunOption    // Option provides extra configuration for validation rule.
}

type RunOption

type RunOption struct {
	CaseInsensitive bool // CaseInsensitive indicates that it does Case-Insensitive comparison in string.
}

Jump to

Keyboard shortcuts

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