@@ -93,96 +93,6 @@ func Before(t any) ValidatorFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// FutureDate validates that a time is in the future.
|
||||
func FutureDate() ValidatorFunc {
|
||||
return func(value any) *ValidationError {
|
||||
actualValue, isNil := dereferenceValue(value)
|
||||
if isNil {
|
||||
return nil
|
||||
}
|
||||
|
||||
timeVal, ok := actualValue.(time.Time)
|
||||
if !ok {
|
||||
return newValidationError(ErrorCodeInvalidFormat, "value must be a time.Time")
|
||||
}
|
||||
|
||||
if !timeVal.After(time.Now()) {
|
||||
return newValidationError(ErrorCodeOutOfRange, "must be a future date")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// PastDate validates that a time is in the past.
|
||||
func PastDate() ValidatorFunc {
|
||||
return func(value any) *ValidationError {
|
||||
actualValue, isNil := dereferenceValue(value)
|
||||
if isNil {
|
||||
return nil
|
||||
}
|
||||
|
||||
timeVal, ok := actualValue.(time.Time)
|
||||
if !ok {
|
||||
return newValidationError(ErrorCodeInvalidFormat, "value must be a time.Time")
|
||||
}
|
||||
|
||||
if !timeVal.Before(time.Now()) {
|
||||
return newValidationError(ErrorCodeOutOfRange, "must be a past date")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MinDuration validates that a duration is at least the specified minimum value.
|
||||
func MinDuration(min time.Duration) ValidatorFunc {
|
||||
return func(value any) *ValidationError {
|
||||
actualValue, isNil := dereferenceValue(value)
|
||||
if isNil {
|
||||
return nil
|
||||
}
|
||||
|
||||
duration, ok := actualValue.(time.Duration)
|
||||
if !ok {
|
||||
return newValidationError(ErrorCodeInvalidFormat, "value must be a time.Duration")
|
||||
}
|
||||
|
||||
if duration < min {
|
||||
return newValidationError(
|
||||
ErrorCodeOutOfRange,
|
||||
fmt.Sprintf("must be at least %s", min),
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MaxDuration validates that a duration does not exceed the specified maximum value.
|
||||
func MaxDuration(max time.Duration) ValidatorFunc {
|
||||
return func(value any) *ValidationError {
|
||||
actualValue, isNil := dereferenceValue(value)
|
||||
if isNil {
|
||||
return nil
|
||||
}
|
||||
|
||||
duration, ok := actualValue.(time.Duration)
|
||||
if !ok {
|
||||
return newValidationError(ErrorCodeInvalidFormat, "value must be a time.Duration")
|
||||
}
|
||||
|
||||
if duration > max {
|
||||
return newValidationError(
|
||||
ErrorCodeOutOfRange,
|
||||
fmt.Sprintf("must be at most %s", max),
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// RangeDuration validates that a duration is within the specified range (inclusive).
|
||||
func RangeDuration(min, max time.Duration) ValidatorFunc {
|
||||
return func(value any) *ValidationError {
|
||||
|
||||
Reference in New Issue
Block a user