add Required, Prefix, Suffix, Contains, Match validators
Broadens the built-in set with the string/presence checks that came up most often when reaching for this package: - Required[T comparable] non-zero value - Prefix / Suffix / Contains strings.HasPrefix/HasSuffix/Contains - Match(*regexp.Regexp) arbitrary pattern All follow the existing shape: a constructor returning func(T) error, a sentinel error matched with errors.Is, table-driven tests and an Example. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user