Files

27 lines
668 B
Go
Raw Permalink Normal View History

2023-10-13 21:16:08 +01:00
package validate
var (
2023-11-19 20:42:48 +00:00
ErrMustHaveMoreItems = NewError("must have at least %d items")
ErrMustHaveFewerItems = NewError("must have no more than %d items")
2023-10-13 21:16:08 +01:00
)
// 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 {
2024-07-18 06:53:20 +01:00
return ErrMustHaveFewerItems.With(l)
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 {
2024-07-18 06:53:20 +01:00
return ErrMustHaveMoreItems.With(l)
2023-10-13 21:16:08 +01:00
}
return nil
}
}