21 lines
401 B
Go
21 lines
401 B
Go
package validate
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"regexp"
|
||
|
|
)
|
||
|
|
|
||
|
|
var (
|
||
|
|
ErrNoMatch = NewError("invalid format")
|
||
|
|
)
|
||
|
|
|
||
|
|
// Match validates that a string matches a regular expression. Compile the
|
||
|
|
// expression once with regexp.MustCompile and reuse the returned validator.
|
||
|
|
func Match(re *regexp.Regexp) func(string) error {
|
||
|
|
return func(value string) error {
|
||
|
|
if !re.MatchString(value) {
|
||
|
|
return ErrNoMatch
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
}
|