-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.DatetimeLessThanOrEqualOther
marrow16 edited this page Jan 21, 2023
·
5 revisions
Check that a date/time (as an ISO string) value is less than or equal to another named property value
Note: this constraint is strict - if either of the compared values is not a valid ISO datetime then this constraint fails
dtlteo
| Field | Type | Description |
|---|---|---|
PropertyName |
string | the property name of the other value to compare against |
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 |
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 |
Note: the PropertyName field 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)
Programmatic example...
package main
import (
"fmt"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
Properties: valix.Properties{
"startDate": {
Type: valix.JsonDatetime,
Constraints: valix.Constraints{
&valix.DatetimeLessThanOrEqualOther{
PropertyName: "endDate",
ExcTime: true,
},
},
},
"endDate": {
Type: valix.JsonDatetime,
},
},
}
obj := `{"startDate": "2022-09-09", "endDate": "2022-09-08"}`
ok, violations, _ := validator.ValidateString(obj)
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)
}
obj = `{"startDate": "2022-09-08", "endDate": "2022-09-09"}`
ok, violations, _ = validator.ValidateString(obj)
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)
}
obj = `{"startDate": "2022-09-09", "endDate": "2022-09-09"}`
ok, violations, _ = validator.ValidateString(obj)
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 {
StartDate *time.Time `json:"startDate" v8n:"&dtlteo{PropertyName:'endDate'}"`
EndDate *time.Time `json:"endDate"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
obj := `{"startDate": "2022-09-09", "endDate": "2022-09-08"}`
ok, violations, _ := validator.ValidateString(obj)
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)
}
obj = `{"startDate": "2022-09-08", "endDate": "2022-09-09"}`
ok, violations, _ = validator.ValidateString(obj)
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)
}
obj = `{"startDate": "2022-09-09", "endDate": "2022-09-09"}`
ok, violations, _ = validator.ValidateString(obj)
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)
}
}With relative array value...
package main
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/marrow16/valix"
)
type DatesRequest struct {
Dates []*time.Time `json:"dates" v8n:"type:array,&aof{Type:'datetime', Constraints:[&acond{When:'!last', Constraint:&dtlteo{PropertyName:'[+1]',Msg:'Must be less than next'}}]}"`
}
var datesReqValidator = valix.MustCompileValidatorFor(DatesRequest{}, nil)
func main() {
req := buildSimulatedHttpReq(`{"dates":["2022-09-02T12:00:00Z", "2022-09-01T12:00:00Z"]}`)
dates := &DatesRequest{}
ok, violations, _ := datesReqValidator.RequestValidateInto(req, dates)
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)
}
req = buildSimulatedHttpReq(`{"dates":["2022-09-01T12:00:00Z", "2022-09-02T12:00:00Z"]}`)
ok, violations, _ = datesReqValidator.RequestValidateInto(req, dates)
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)
}
}
func buildSimulatedHttpReq(body string) *http.Request {
req, _ := http.NewRequest("POST", "example.com/test", strings.NewReader(body))
return req
}