-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.DatetimeToleranceToOther
marrow16 edited this page Jan 21, 2023
·
6 revisions
Check that a date/time (as an ISO string) value meets a tolerance against the value of another named property value
Note: this constraint is strict - if the property value is not a valid ISO datetime then this constraint fails
dttolother
| Field | Type | Description |
|---|---|---|
PropertyName |
string | the property name of the other value to compare against Note: the PropertyName can also be JSON dot notation path - where leading dots allow traversal up the object tree and names, separated by dots, allow traversal down the object tree. A single dot at start is equivalent to no starting dot (i.e. a property name at the same level) |
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"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
Properties: valix.Properties{
"holidayStartDate": {
Type: valix.JsonDatetime,
Constraints: valix.Constraints{
&valix.DatetimeLessThanOther{
PropertyName: "holidayEndDate",
ExcTime: true,
Stop: true,
},
&valix.DatetimeToleranceToOther{
PropertyName: "holidayEndDate",
Duration: 7,
Unit: "day",
MinCheck: true,
ExcTime: true,
Message: "Sorry, we don't do holidays less than a week long",
},
},
},
"holidayEndDate": {
Type: valix.JsonDatetime,
},
},
}
ok, violations, _ := validator.ValidateString(`{"holidayStartDate": "2022-09-28T12:00:00Z", "holidayEndDate": "2022-09-28T12:00:00Z"}`)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
ok, violations, _ = validator.ValidateString(`{"holidayStartDate": "2022-09-28T12:00:00Z", "holidayEndDate": "2022-09-29T12:00:00Z"}`)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
ok, violations, _ = validator.ValidateString(`{"holidayStartDate": "2022-09-28T12:00:00Z", "holidayEndDate": "2022-10-06T12:00:00Z"}`)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
}Struct v8n tag example...
package main
import (
"fmt"
"time"
"github.com/marrow16/valix"
)
type MyStruct struct {
HolidayStartDate *time.Time `json:"holidayStartDate" v8n:"&dtlto{prop:'holidayEndDate', excTime:true, stop:true},&dttolother{prop:'holidayEndDate', dur:7, unit:'day', min:true, excTime:true, msg:\"Sorry, we don't do holidays less than a week long\"}"`
HolidayEndDate *time.Time `json:"holidayEndDate"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
ok, violations, _ := validator.ValidateString(`{"holidayStartDate": "2022-09-28T12:00:00Z", "holidayEndDate": "2022-09-28T12:00:00Z"}`)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
ok, violations, _ = validator.ValidateString(`{"holidayStartDate": "2022-09-28T12:00:00Z", "holidayEndDate": "2022-09-29T12:00:00Z"}`)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
ok, violations, _ = validator.ValidateString(`{"holidayStartDate": "2022-09-28T12:00:00Z", "holidayEndDate": "2022-10-06T12:00:00Z"}`)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
}