-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.DatetimeToleranceToNow
marrow16 edited this page Jan 21, 2023
·
5 revisions
Check that a date/time (as an ISO string) value meets a tolerance against the current time
Note: this constraint is strict - if the property value is not a valid ISO datetime then this constraint fails
dttolnow
| Field | Type | Description |
|---|---|---|
Duration |
int64 | the tolerance duration amount - which can be positive, negative or zero For negative values, this is the maximum duration into the past For positive values, this is the maximum duration into the future Note: If the value is zero then the behaviour is assumed to be "same" - but is then dependent on the unit specified. For example, if the Duration is zero and the Unit is specified as "year" then this constraint |
Unit |
string | is the string token specifying the unit in which the Duration is measured This can be "millennium", "century", "decade", "year", "month", "week", "day", "hour", "min", "sec", "milli" (millisecond), "micro" (microsecond) or "nano" (nanosecond)Note: if this is empty, then "day" is assumed. If the token is invalid - this constraint fails! |
MinCheck |
bool | when set to true, specifies that the tolerance is a minimum check (rather than the default maximum check) |
ExcTime |
bool | when set to true, excludes the time when comparing Note: This also excludes the effect of any timezone offsets specified in either of the compared values |
IgnoreNull |
bool | when set to true, IgnoreNull makes the constraint less strict by ignoring null values |
Message |
string | the violation message to be used if the constraint fails. If empty, the default message is used |
Stop |
bool | when set to true, Stop prevents further validation checks on the property if this constraint fails |
Programmatic example...
package main
import (
"fmt"
"time"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
Properties: valix.Properties{
"bookingDate": {
Type: valix.JsonDatetime,
Constraints: valix.Constraints{
&valix.ConstraintSet{
Constraints: valix.Constraints{
&valix.DatetimeDayOfWeek{
Days: "06",
},
&valix.DatetimeToleranceToNow{
Duration: 2,
Unit: "week",
ExcTime: true,
},
},
Message: "We only accept bookings for the next two weekends",
},
},
},
},
}
now := time.Now()
for i := 0; i < 20; i++ {
ok, violations, _ := validator.ValidateString(fmt.Sprintf(`{"bookingDate": "%s"}`, now.Format(time.RFC3339)))
fmt.Printf("Booking date \"%s\" ok? %v\n", now.Format("Monday, 02-Jan-06"), ok)
if !ok {
fmt.Printf("\tReason: %s\n", violations[0].Message)
}
now = now.Add(24 * time.Hour)
}
}Struct v8n tag example...
package main
import (
"fmt"
"time"
"github.com/marrow16/valix"
)
type MyStruct struct {
BookingDate *time.Time `json:"bookingDate" v8n:"&set{constraints:[&dtdow{'06'}, &dttolnow{dur:2, unit:'week', excTime:true}], msg:'We only accept bookings for the next two weekends'}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
now := time.Now()
for i := 0; i < 20; i++ {
ok, violations, _ := validator.ValidateString(fmt.Sprintf(`{"bookingDate": "%s"}`, now.Format(time.RFC3339)))
fmt.Printf("Booking date \"%s\" ok? %v\n", now.Format("Monday, 02-Jan-06"), ok)
if !ok {
fmt.Printf("\tReason: %s\n", violations[0].Message)
}
now = now.Add(24 * time.Hour)
}
}