-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.StringValidTimezone
marrow16 edited this page Jan 21, 2023
·
6 revisions
Check that a string value is a valid timezone
strtz
| Field | Type | Description |
|---|---|---|
LocationOnly |
bool | allows location only |
OffsetOnly |
bool | allows offset only |
AllowNumeric |
bool | if set, allows offset to be a numeric value |
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: If both LocationOnly and OffsetOnly are set to true - this constraint will always fail!
Programmatic example...
package main
import (
"fmt"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
UseNumber: true,
Properties: valix.Properties{
"foo": {
Type: valix.JsonString,
Constraints: valix.Constraints{
&valix.StringValidTimezone{
LocationOnly: true,
},
},
},
},
}
ok, violations, _ := validator.ValidateString(`{"foo": "not a timezone"}`)
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(`{"foo": "+00:00"}`)
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(`{"foo": "Europe/London"}`)
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"
"github.com/marrow16/valix"
)
type MyStruct struct {
Foo string `json:"foo" v8n:"&strtz{LocationOnly}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{})
func main() {
my := &MyStruct{}
ok, violations, _ := validator.ValidateStringInto(`{"foo": "not a timezone"}`, my)
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.ValidateStringInto(`{"foo": "+00:00"}`, my)
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.ValidateStringInto(`{"foo": "Europe/London"}`, my)
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)
}
}