-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.DatetimeTolerance
marrow16 edited this page Jan 21, 2023
·
5 revisions
Check that a date/time (as an ISO string) value meets a tolerance against a specified value
Note: this constraint is strict - if the property value is not a valid ISO datetime then this constraint fails
dttol
Field | Type | Description |
---|---|---|
Value |
string | the value to compare against (a string representation of date or datetime in ISO format) |
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{
"bookingDate": {
Type: valix.JsonDatetime,
Constraints: valix.Constraints{
&valix.DatetimeTolerance{
Value: "2022-09-20",
Unit: "day",
Duration: 7,
ExcTime: true,
},
},
},
},
}
ok, violations, _ := validator.ValidateString(`{"bookingDate": "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(`{"bookingDate": "2022-09-27T12: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 {
BookingDate *time.Time `json:"bookingDate" v8n:"&dttol{Value:'2022-09-20T12:00:00', Unit:'hour', Duration: 4}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
ok, violations, _ := validator.ValidateString(`{"bookingDate": "2022-09-20T17: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(`{"bookingDate": "2022-09-20T16: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)
}
}