-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.SetConditionProperty
marrow16 edited this page Jan 21, 2023
·
4 revisions
Is a utility constraint that can be used to set a condition in the
ValidatorContext
from the value of a specified property within the object to which this constraint is attached
This constraint is normally only used in Validator.Constraints
Note: The property value can be of any type (inc. null) or, indeed, the property may be missing
cpty
Field | Type | Description |
---|---|---|
PropertyName |
string | is the name of the property to extract the condition value from |
Prefix |
string | is any prefix to be prepended to the condition token |
Mapping |
map[string]string | converts the token value to alternate values (if the value is not found in the map then the original value is used) |
NullToken |
string | is the condition token used if the value of the property specified is null/nil. If this field is not set and the property value is null at validation - then a condition token of "null" is used |
MissingToken |
string | is the condition token used if the property specified is missing. If this field is not set and the property is missing at validation - then a condition token of "missing" is used |
Format |
string | is an optional format string for dealing with non-string property values |
Programmatic example...
package main
import (
"fmt"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
UseNumber: true,
Properties: valix.Properties{
"foo": {
Type: valix.JsonObject,
ObjectValidator: &valix.Validator{
Constraints: valix.Constraints{
&valix.SetConditionProperty{
PropertyName: "bar",
Prefix: "bar_is_",
},
},
Properties: valix.Properties{
"bar": {
Type: valix.JsonString,
},
"baz": {
Type: valix.JsonString,
Constraints: valix.Constraints{
&valix.ConditionalConstraint{
When: []string{"bar_is_one"},
Constraint: &valix.StringUppercase{Message: "Baz must be all uppercase when bar is 'one'"},
},
},
},
},
},
},
},
}
ok, violations, _ := validator.ValidateString(`{"foo": {"bar": "one", "baz": "Not all uppercase"}}`)
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": {"bar": "two", "baz": "Not all uppercase"}}`)
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": {"bar": "one", "baz": "ALL UPPERCASE"}}`)
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 FooStruct `json:"foo" v8n:"obj.constraint:&cpty{propertyName:'bar', prefix:'bar_is_'}"`
}
type FooStruct struct {
Bar string `json:"bar"`
Baz string `json:"baz" v8n:"&[bar_is_one]strupper{\"Baz must be all uppercase when bar is 'one'\"}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
my := &MyStruct{}
ok, violations, _ := validator.ValidateStringInto(`{"foo": {"bar": "one", "baz": "Not all uppercase"}}`, 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": {"bar": "two", "baz": "Not all uppercase"}}`, 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": {"bar": "one", "baz": "ALL UPPERCASE"}}`, 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)
}
}