-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.DatetimePast
marrow16 edited this page Jan 21, 2023
·
6 revisions
Check that a datetime/date (represented as string or time.Time) is in the past
dtpast
| Field | Type | Description |
|---|---|---|
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 |
Programmatic example...
package main
import (
"fmt"
"time"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
Properties: valix.Properties{
"foo": {
Type: valix.JsonDatetime,
Constraints: valix.Constraints{
&valix.DatetimePast{
ExcTime: true,
},
},
},
},
}
obj := "{\"foo\": \"" + time.Now().Format(time.RFC3339) + "\"}"
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 = "{\"foo\": \"" + time.Now().Add(0-(time.Hour*24)).Format(time.RFC3339) + "\"}"
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 {
Foo *time.Time `json:"foo" v8n:"&dtpast{ExcTime: true}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
obj := "{\"foo\": \"" + time.Now().Format(time.RFC3339) + "\"}"
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 = "{\"foo\": \"" + time.Now().Add(0-(time.Hour*24)).Format(time.RFC3339) + "\"}"
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)
}
}