1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package valid
- import "regexp"
- var ErrMatchInvalid = NewError("validation_match_invalid", "must be in a valid format")
- func Match(re *regexp.Regexp) MatchRule {
- return MatchRule{
- re: re,
- err: ErrMatchInvalid,
- }
- }
- type MatchRule struct {
- re *regexp.Regexp
- err Error
- }
- func (r MatchRule) Validate(value interface{}) error {
- value, isNil := Indirect(value)
- if isNil {
- return nil
- }
- isString, str, isBytes, bs := StringOrBytes(value)
- if isString && (str == "" || r.re.MatchString(str)) {
- return nil
- } else if isBytes && (len(bs) == 0 || r.re.Match(bs)) {
- return nil
- }
- return r.err
- }
- func (r MatchRule) Error(message string) MatchRule {
- r.err = r.err.SetMessage(message)
- return r
- }
- func (r MatchRule) ErrorObject(err Error) MatchRule {
- r.err = err
- return r
- }
|