size.go 644 B

12345678910111213141516171819202122232425262728293031
  1. package validate
  2. import (
  3. "errors"
  4. )
  5. // Validation error.
  6. var (
  7. ErrTooFewItems = errors.New("too few items")
  8. ErrTooManyItems = errors.New("too many items")
  9. )
  10. // MaxSize validates the length of a slice as being less than or equal to a given maximum.
  11. func MaxSize[T any](l int) func([]T) error {
  12. return func(value []T) error {
  13. if len(value) > l {
  14. return ErrTooManyItems
  15. }
  16. return nil
  17. }
  18. }
  19. // MinSize validates the length of a slice as being greater than or equal to a given minimum.
  20. func MinSize[T any](l int) func([]T) error {
  21. return func(value []T) error {
  22. if len(value) < l {
  23. return ErrTooFewChars
  24. }
  25. return nil
  26. }
  27. }