Documentation ¶
Index ¶
- func IsStructZero(sIface interface{}) bool
- func IsValidationError(err error) bool
- type Error
- type Errors
- type MapError
- type Rule
- type TemplateMap
- type Validate
- func (v *Validate) DoRules(data interface{}, rules []Rule) (Errors, error)
- func (v *Validate) DoRulesAndToMapError(data interface{}, rules []Rule) (MapError, error)
- func (v *Validate) DoRulesAndToMapErrorWithTagName(data interface{}, rules []Rule, tagName string) (MapError, error)
- func (v *Validate) DoRulesToProtoError(data interface{}, rules []Rule) *proto.Error
- func (v *Validate) DoRulesToStruct(data interface{}, rules []Rule, toStruct interface{})
- func (v *Validate) DoRulesToStructAndSetNil(data interface{}, rules []Rule, toStruct interface{})
- func (v *Validate) DoRulesWithTagName(data interface{}, rules []Rule, tagName string) (verrs Errors, err error)
- func (v *Validate) IsVar(field interface{}, tag string) bool
- func (v *Validate) RegisterInclusionValidationParam(param string, validSlice interface{}) error
- func (v *Validate) RegisterRegexpValidation(tag string, regexpString string) error
- func (v *Validate) RegisterTemplateMap(templateMap TemplateMap) error
- func (v *Validate) RegisterValidation(tag string, fn func(validator.FieldLevel) bool) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsStructZero ¶
func IsStructZero(sIface interface{}) bool
Return true when all fields is zero. sIface must be pointer to struct.
func IsValidationError ¶
Types ¶
type MapError ¶
func VErrorsToMap ¶
func VErrorsToMap(verrs Errors, templateMap TemplateMap) (MapError, error)
templateMap is a map that mean [tag]template, templateMap will be parsed by go template, you can use ".Tag" and ".Param" variable in the template.
For example, validation tag is "max=100", then tag is "max", param is "100", if template is "is too large, maximum is {{.Param}}", then it will be parsed to "is too large, maximum is 100".
If templateMap is nil, it will use defaultTemplateMap to parse. If not found the tag in the templateMap, it will use defaultTemplateMap to parse for this tag, it mean templateMap will merge to defaultTemplateMap, so you don't worry about missing some tags of the templateMap.
If parse template failed, it will return error.
type TemplateMap ¶
type Validate ¶
func (*Validate) DoRules ¶
data should be a struct or a pointer to struct.
if return (nil, nil), it mean no validation error.
If it return (nil, error), you must to solve it. Possible errors: * Invalid Rule.Tag * Invalid Rule.Field * data is not a struct or a pointer to struct
Some custom tags: * zipcode_jp * simple_email
func (*Validate) DoRulesAndToMapError ¶
Same as DoRules, run DoRules and VErrorsToMap with custom template. You can use RegisterTemplateMap func to register custom template.
func (*Validate) DoRulesAndToMapErrorWithTagName ¶
func (*Validate) DoRulesToProtoError ¶
If no any error, return nil.
func (*Validate) DoRulesToStruct ¶
TODO toStruct can be pointer to struct. toStruct must be pointer to pointer to struct, and fields must contains all rule.Field and type must be []string.
func (*Validate) DoRulesToStructAndSetNil ¶
toStruct must be pointer to pointer to struct, and fields must contains all rule.Field and type must be []string. If no validation errors, then set toStruct to nil.
func (*Validate) DoRulesWithTagName ¶
func (*Validate) IsVar ¶
It's a proxy function for validate.Var from github.com/go-playground/validator. But it return bool type.
If has some invalid input, it will return false.
func (*Validate) RegisterInclusionValidationParam ¶
RegisterInclusionValidationParam register a param for inclusion validation. validSlice are all valid values for param of the inclusion validation, and it must be slice type.
For example, if you register a "gender" param, then you can use `validate:"inclusion=gender"` validation tag for the struct.
If you use a unregistered inclusion param, then this field of the struct validation always failed.
If you register the same param multiple times, the front will be covered. If param is empty, it will return error.
func (*Validate) RegisterRegexpValidation ¶
RegisterRegexpValidation adds a regexp validation with the given tag and regexpString
NOTES: - if the key already exists, the previous validation function will be replaced. - this method is not thread-safe it is intended that these all be registered prior to any validation
func (*Validate) RegisterTemplateMap ¶
func (v *Validate) RegisterTemplateMap(templateMap TemplateMap) error
func (*Validate) RegisterValidation ¶
RegisterValidation adds a validation with the given tag.
NOTES: - if the key already exists, the previous validation function will be replaced. - this method is not thread-safe it is intended that these all be registered prior to any validation
TODO if we use this function, we must import github.com/go-playground/validator, this is not good, because we hope only import this package. To find a better way.