size.go 668 B

1234567891011121314151617181920212223242526
  1. package validate
  2. var (
  3. ErrMustHaveMoreItems = NewError("must have at least %d items")
  4. ErrMustHaveFewerItems = NewError("must have no more than %d items")
  5. )
  6. // MaxSize validates the length of a slice as being less than or equal to a given maximum.
  7. func MaxSize[T any](l int) func([]T) error {
  8. return func(value []T) error {
  9. if len(value) > l {
  10. return ErrMustHaveFewerItems.With(l)
  11. }
  12. return nil
  13. }
  14. }
  15. // MinSize validates the length of a slice as being greater than or equal to a given minimum.
  16. func MinSize[T any](l int) func([]T) error {
  17. return func(value []T) error {
  18. if len(value) < l {
  19. return ErrMustHaveMoreItems.With(l)
  20. }
  21. return nil
  22. }
  23. }