Documentation ¶
Index ¶
- func DeleteRule(rules ...string)
- func GetRegisteredRuleMap() map[string]RuleFunc
- func GetTags() []string
- func ParseTagValue(tag string) (field, rule, msg string)
- func RegisterRule(rule string, f RuleFunc)
- func RegisterRuleByMap(m map[string]RuleFunc)
- func Trim(str string, characterMask ...string) string
- func ValidateStruct(s interface{}) error
- type CustomMsg
- type Error
- type RuleFunc
- type RuleFuncInput
- type StrSet
- type Validator
- func (v *Validator) Assoc(assoc interface{}) *Validator
- func (v *Validator) Bail() *Validator
- func (v *Validator) Ci() *Validator
- func (v *Validator) Clone() *Validator
- func (v *Validator) Data(data interface{}) *Validator
- func (v *Validator) Foreach() *Validator
- func (v *Validator) Messages(messages interface{}) *Validator
- func (v *Validator) RuleFunc(rule string, f RuleFunc) *Validator
- func (v *Validator) RuleFuncMap(m map[string]RuleFunc) *Validator
- func (v *Validator) Rules(rules interface{}) *Validator
- func (v *Validator) Run(ctx context.Context) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeleteRule ¶
func DeleteRule(rules ...string)
DeleteRule deletes custom defined validation one or more rules and associated functions from global package.
func GetRegisteredRuleMap ¶
GetRegisteredRuleMap returns all the custom registered rules and associated functions.
func ParseTagValue ¶
ParseTagValue parses one sequence tag to field, rule and error message. The sequence tag is like: [alias@]rule[...#msg...]
func RegisterRule ¶
RegisterRule registers custom validation rule and function for package.
func RegisterRuleByMap ¶
RegisterRuleByMap registers custom validation rules using map for package.
func Trim ¶
Trim strips whitespace (or other characters) from the beginning and end of a string. The optional parameter `characterMask` specifies the additional stripped characters.
func ValidateStruct ¶
func ValidateStruct(s interface{}) error
Types ¶
type CustomMsg ¶
type CustomMsg = map[string]interface{}
CustomMsg is the custom error message type, like: map[field] => string|map[rule]string
type Error ¶
type Error interface { Code() gcode.Code Current() error Error() string FirstItem() (key string, messages map[string]error) FirstRule() (rule string, err error) FirstError() (err error) Items() (items []map[string]map[string]error) Map() map[string]error Maps() map[string]map[string]error String() string Strings() (errs []string) }
Error is the validation error for validation result.
type RuleFunc ¶
type RuleFunc func(ctx context.Context, in RuleFuncInput) error
RuleFunc is the custom function for data validation.
type RuleFuncInput ¶
type RuleFuncInput struct { // Rule specifies the validation rule string, like "required", "between:1,100", etc. Rule string // Message specifies the custom error message or configured i18n message for this rule. Message string // Field specifies the field for this rule to validate. Field string // ValueType specifies the type of the value, which might be nil. ValueType reflect.Type // Value specifies the value for this rule to validate. Value *any // Data specifies the `data` which is passed to the Validator. It might be a type of map/struct or a nil value. // You can ignore the parameter `Data` if you do not really need it in your custom validation rule. Data *any }
RuleFuncInput holds the input parameters that passed to custom rule function RuleFunc.
type StrSet ¶
type StrSet struct {
// contains filtered or unexported fields
}
func NewStrSet ¶
NewStrSet create and returns a new set, which contains un-repeated items. The parameter `safe` is used to specify whether using set in concurrent-safety, which is false in default.
func (*StrSet) AddIfNotExist ¶
AddIfNotExist checks whether item exists in the set, it adds the item to set and returns true if it does not exist in the set, or else it does nothing and returns false.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator is the validation manager for chaining operations.
func (*Validator) Assoc ¶
Assoc is a chaining operation function, which sets associated validation data for current operation. The optional parameter `assoc` is usually type of map, which specifies the parameter map used in union validation. Calling this function with `assoc` also sets `useAssocInsteadOfObjectAttributes` true
func (*Validator) Bail ¶
Bail sets the mark for stopping validation after the first validation error.
func (*Validator) Ci ¶
Ci sets the mark for Case-Insensitive for those rules that need value comparison.
func (*Validator) Clone ¶
Clone creates and returns a new Validator which is a shallow copy of current one.
func (*Validator) Data ¶
Data is a chaining operation function, which sets validation data for current operation.
func (*Validator) Foreach ¶
Foreach tells the next validation using current value as an array and validates each of its element. Note that this decorating rule takes effect just once for next validation rule, specially for single value validation.
func (*Validator) Messages ¶
Messages is a chaining operation function, which sets custom error messages for current operation. The parameter `messages` can be type of string/[]string/map[string]string. It supports sequence in error result if `rules` is type of []string.
func (*Validator) RuleFuncMap ¶
RuleFuncMap registers multiple custom rule functions to current Validator.