add all validator, simplify error messages
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user