length.go 777 B

1234567891011121314151617181920212223242526272829303132
  1. package validate
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. // MaxLength validates the length of a string as being less than or equal to a given maximum.
  7. func MaxLength(l int) func(string) error {
  8. return func(value string) error {
  9. if len(value) > l {
  10. if l != 1 {
  11. return fmt.Errorf("Must not be longer than %d characters", l)
  12. }
  13. return errors.New("Must not be longer than 1 character")
  14. }
  15. return nil
  16. }
  17. }
  18. // MinLength validates the length of a string as being greater than or equal to a given minimum.
  19. func MinLength(l int) func(string) error {
  20. return func(value string) error {
  21. if len(value) < l {
  22. if l != 1 {
  23. return fmt.Errorf("Must not be shorter than %d characters", l)
  24. }
  25. return errors.New("Must not be shorter than 1 character")
  26. }
  27. return nil
  28. }
  29. }