add all validator, simplify error messages

This commit is contained in:
Aneurin Barker Snook
2023-10-07 13:42:17 +01:00
parent c2efe16dc0
commit c74486d122
8 changed files with 98 additions and 19 deletions
+8 -9
View File
@@ -2,17 +2,19 @@ package validate
import (
"errors"
"fmt"
)
// Validation error.
var (
ErrTooFewChars = errors.New("too few characters")
ErrTooManyChars = errors.New("too many characters")
)
// MaxLength validates the length of a string as being less than or equal to a given maximum.
func MaxLength(l int) func(string) error {
return func(value string) error {
if len(value) > l {
if l != 1 {
return fmt.Errorf("Must not be longer than %d characters", l)
}
return errors.New("Must not be longer than 1 character")
return ErrTooManyChars
}
return nil
}
@@ -22,10 +24,7 @@ func MaxLength(l int) func(string) error {
func MinLength(l int) func(string) error {
return func(value string) error {
if len(value) < l {
if l != 1 {
return fmt.Errorf("Must not be shorter than %d characters", l)
}
return errors.New("Must not be shorter than 1 character")
return ErrTooFewChars
}
return nil
}