2023-10-07 10:43:21 +01:00
|
|
|
package validate
|
|
|
|
|
|
2023-10-07 13:42:17 +01:00
|
|
|
// Validation error.
|
|
|
|
|
var (
|
2023-11-19 20:42:48 +00:00
|
|
|
ErrMustBeLonger = NewError("must be at least %d characters")
|
|
|
|
|
ErrMustBeShorter = NewError("must be no more than %d characters")
|
2023-10-07 10:43:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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 {
|
2023-11-19 20:42:48 +00:00
|
|
|
return ErrMustBeShorter.With(l)
|
2023-10-07 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MinLength validates the length of a string as being greater than or equal to a given minimum.
|
|
|
|
|
func MinLength(l int) func(string) error {
|
|
|
|
|
return func(value string) error {
|
|
|
|
|
if len(value) < l {
|
2023-11-19 20:42:48 +00:00
|
|
|
return ErrMustBeLonger.With(l)
|
2023-10-07 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|