-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.ConstraintSet
marrow16 edited this page Jan 21, 2023
·
3 revisions
Is a special constraint that contains other constraints
The contained constraints are checked sequentially but the overall set stops on the first failing constraint
set
Field | Type | Description |
---|---|---|
Constraints |
[]Constraint | is the slice of constraints within the set |
OneOf |
bool | when set to true, specifies that the constraint set should pass just one of the contained constraints (rather than all of them) |
Message |
string | is the violation message to be used if any of the constraints fail If the message is empty, the message from the first failing contained constraint is used |
Stop |
bool | when set to true, prevents further validation checks on the property if this constraint set fails |
Programmatic example...
package main
import (
"fmt"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
Properties: valix.Properties{
"foo": {
Type: valix.JsonString,
Constraints: valix.Constraints{
&valix.ConstraintSet{
Constraints: valix.Constraints{
&valix.StringUppercase{},
&valix.StringLowercase{},
},
OneOf: true,
Message: "Must be all uppercase or all lowercase",
},
},
},
},
}
ok, violations, _ := validator.ValidateString(`{"foo": "Mixed case"}`)
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": "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": "all lowercase"}`)
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:"&set{constraints:[&StringUppercase,&StringLowercase],one,msg:'Must be all uppercase or all lowercase'}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
ok, violations, _ := validator.ValidateString(`{"foo": "Mixed case"}`)
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": "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": "all lowercase"}`)
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)
}
}