-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.FailWith
marrow16 edited this page Jan 21, 2023
·
3 revisions
Is a utility constraint that fails when specified others property expression evaluates to true
failwith
| Field | Type | Description |
|---|---|---|
Others |
OthersExpr | is the others expression to be evaluated to determine whether the constraint should fail |
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, prevents further validation checks on the property if this constraint fails |
StopAll |
bool | when set to true, stops the entire validation |
Programmatic example...
package main
import (
"fmt"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
IgnoreUnknownProperties: true,
Properties: valix.Properties{
"foo": {
Type: valix.JsonAny,
Constraints: valix.Constraints{
&valix.FailWith{
Others: valix.MustParseExpression("(bar && baz) || (baz && qux)"),
Message: "Not with (bar and baz) or (baz and qux)",
},
},
},
},
}
ok, violations, _ := validator.ValidateString(`{"foo": true, "bar": "here", "baz": "here"}`)
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": true, "baz": "here", "qux": "here"}`)
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": true, "bar": "here", "qux": "here"}`)
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 interface{} `json:"foo" v8n:"&failwith{Others:'(bar && baz) || (baz && qux)', Message:'Not with (bar and baz) or (baz and qux)'}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, &valix.ValidatorForOptions{IgnoreUnknownProperties: true})
func main() {
ok, violations, _ := validator.ValidateString(`{"foo": true, "bar": "here", "baz": "here"}`)
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": true, "baz": "here", "qux": "here"}`)
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": true, "bar": "here", "qux": "here"}`)
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)
}
}