rule_date.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package valid
  2. import (
  3. "time"
  4. )
  5. var (
  6. // ErrDateInvalid is the error that returns in case of an invalid date.
  7. ErrDateInvalid = NewError("validation_date_invalid", "must be a valid date")
  8. // ErrDateOutOfRange is the error that returns in case of an invalid date.
  9. ErrDateOutOfRange = NewError("validation_date_out_of_range", "the date is out of range")
  10. )
  11. // DateRule is a validation rule that validates date/time string values.
  12. type DateRule struct {
  13. layout string
  14. min, max time.Time
  15. err, rangeErr Error
  16. }
  17. // Date returns a validation rule that checks if a string value is in a format that can be parsed into a date.
  18. // The format of the date should be specified as the layout parameter which accepts the same value as that for time.Parse.
  19. // For example,
  20. // validation.Date(time.ANSIC)
  21. // validation.Date("02 Jan 06 15:04 MST")
  22. // validation.Date("2006-01-02")
  23. //
  24. // By calling Min() and/or Max(), you can let the Date rule to check if a parsed date value is within
  25. // the specified date range.
  26. //
  27. // An empty value is considered valid. Use the Required rule to make sure a value is not empty.
  28. func Date(layout string) DateRule {
  29. return DateRule{
  30. layout: layout,
  31. err: ErrDateInvalid,
  32. rangeErr: ErrDateOutOfRange,
  33. }
  34. }
  35. // Error sets the error message that is used when the value being validated is not a valid date.
  36. func (r DateRule) Error(message string) DateRule {
  37. r.err = r.err.SetMessage(message)
  38. return r
  39. }
  40. // ErrorObject sets the error struct that is used when the value being validated is not a valid date..
  41. func (r DateRule) ErrorObject(err Error) DateRule {
  42. r.err = err
  43. return r
  44. }
  45. // RangeError sets the error message that is used when the value being validated is out of the specified Min/Max date range.
  46. func (r DateRule) RangeError(message string) DateRule {
  47. r.rangeErr = r.rangeErr.SetMessage(message)
  48. return r
  49. }
  50. // RangeErrorObject sets the error struct that is used when the value being validated is out of the specified Min/Max date range.
  51. func (r DateRule) RangeErrorObject(err Error) DateRule {
  52. r.rangeErr = err
  53. return r
  54. }
  55. // Min sets the minimum date range. A zero value means skipping the minimum range validation.
  56. func (r DateRule) Min(min time.Time) DateRule {
  57. r.min = min
  58. return r
  59. }
  60. // Max sets the maximum date range. A zero value means skipping the maximum range validation.
  61. func (r DateRule) Max(max time.Time) DateRule {
  62. r.max = max
  63. return r
  64. }
  65. // Validate checks if the given value is a valid date.
  66. func (r DateRule) Validate(value interface{}) error {
  67. value, isNil := Indirect(value)
  68. if isNil || IsEmpty(value) {
  69. return nil
  70. }
  71. str, err := EnsureString(value)
  72. if err != nil {
  73. return err
  74. }
  75. date, err := time.Parse(r.layout, str)
  76. if err != nil {
  77. return r.err
  78. }
  79. if !r.min.IsZero() && r.min.After(date) || !r.max.IsZero() && date.After(r.max) {
  80. return r.rangeErr
  81. }
  82. return nil
  83. }