-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.DatetimeDayOfWeek
marrow16 edited this page Jan 21, 2023
·
4 revisions
Check that a date (represented as string or time.Time) is an allowed day of the week
dtdow
| Field | Type | Description |
|---|---|---|
Days |
string | is the allowed days (of the week) expressed as a string of allowed week day numbers (in any order) Where 0 = Sunday, e.g. "06" (or "60") allows Sunday or Saturdayor to allow only 'working days' of the week - "12345"
|
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{
"preferredDeliveryDate": {
Type: valix.JsonDatetime,
Constraints: valix.Constraints{
&valix.DatetimeDayOfWeek{
Days: "12345",
Message: "Sorry, we can only deliver Monday to Friday",
},
},
},
},
}
obj := `{"preferredDeliveryDate": "2022-09-25T10:00:00"}`
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 = `{"preferredDeliveryDate": "2022-09-26T10:00:00"}`
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 {
PreferredDeliveryDate *time.Time `json:"preferredDeliveryDate" v8n:"&dtdow{Days:\"12345\", Message: \"Sorry, we can only deliver Monday to Friday\"}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
obj := `{"preferredDeliveryDate": "2022-09-25T10:00:00"}`
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 = `{"preferredDeliveryDate": "2022-09-26T10:00:00"}`
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)
}
}