-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.NotEqualsOther
marrow16 edited this page Jan 21, 2023
·
6 revisions
Check that a property value not equals the value of another named property
neqo
| Field | Type | Description |
|---|---|---|
PropertyName |
string | the property name of the other value to compare against Note: the PropertyName can also be JSON dot notation path - where leading dots allow traversal up the object tree and names, separated by dots, allow traversal down the object tree. A single dot at start is equivalent to no starting dot (i.e. a property name at the same level) |
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 |
Strict |
bool | when set to true, fails if the other property is not present (even though the not equals would technically be ok) |
Programmatic example...
package main
import (
"fmt"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
Properties: valix.Properties{
"foo": {
Type: valix.JsonAny,
Mandatory: true,
Constraints: valix.Constraints{
&valix.NotEqualsOther{
PropertyName: "other",
},
},
},
"other": {
Type: valix.JsonAny,
Mandatory: true,
},
},
}
ok, violations, _ := validator.ValidateString(`{"foo": "aaa", "other": "bbb"}`)
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": "aaa", "other": "aaa"}`)
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": "aaa", "other": {"someSubProperty": "aaa"}}}`)
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": {"someSubProperty": "bbb"}, "other": {"someSubProperty": "aaa"}}}`)
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": {"someSubProperty": "aaa"}, "other": {"someSubProperty": "aaa"}}}`)
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:"&neqo{PropertyName:'other'}"`
Other interface{} `json:"other"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
ok, violations, _ := validator.ValidateString(`{"foo": "aaa", "other": "bbb"}`)
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": "aaa", "other": "aaa"}`)
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": "aaa", "other": {"someSubProperty": "aaa"}}}`)
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": {"someSubProperty": "bbb"}, "other": {"someSubProperty": "aaa"}}}`)
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": {"someSubProperty": "aaa"}, "other": {"someSubProperty": "aaa"}}}`)
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)
}
}