2023-10-13 21:16:08 +01:00
|
|
|
package validate
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Validation error.
|
|
|
|
|
var (
|
|
|
|
|
ErrTooFewItems = errors.New("too few items")
|
|
|
|
|
ErrTooManyItems = errors.New("too many items")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// MaxSize validates the length of a slice as being less than or equal to a given maximum.
|
|
|
|
|
func MaxSize[T any](l int) func([]T) error {
|
|
|
|
|
return func(value []T) error {
|
|
|
|
|
if len(value) > l {
|
2023-10-14 14:00:22 +01:00
|
|
|
return ErrTooManyItems
|
2023-10-13 21:16:08 +01:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MinSize validates the length of a slice as being greater than or equal to a given minimum.
|
|
|
|
|
func MinSize[T any](l int) func([]T) error {
|
|
|
|
|
return func(value []T) error {
|
|
|
|
|
if len(value) < l {
|
2023-10-16 17:43:05 +01:00
|
|
|
return ErrTooFewItems
|
2023-10-13 21:16:08 +01:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|