-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.ArrayUnique
marrow16 edited this page Jan 21, 2023
·
5 revisions
Check each element in an array value is unique
aunique
Field | Type | Description |
---|---|---|
IgnoreNulls |
bool | whether to ignore null items in the array |
IgnoreCase |
bool | whether uniqueness is case in-insensitive (for string elements) |
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 |
Programmatic example...
package main
import (
"fmt"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
Properties: valix.Properties{
"foo": {
Type: valix.JsonArray,
Constraints: valix.Constraints{
&valix.ArrayUnique{
IgnoreNulls: true,
IgnoreCase: true,
},
},
},
},
}
obj := `{
"foo": ["same", "Same", {"bar": "same", "baz": 1}, {"bar": "same", "baz": 1.0}]
}`
ok, violations, _ := validator.ValidateString(obj)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s", 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:"&aunique{IgnoreNulls:true, IgnoreCase:true,}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
obj := `{
"foo": ["same", "Same", {"bar": "same", "baz": 1}, {"bar": "same", "baz": 1.0}]
}`
ok, violations, _ := validator.ValidateString(obj)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s", i+1, v.Message, v.Property, v.Path)
}
}