Skip to content

constraint.ArrayDistinctProperty

marrow16 edited this page Jan 21, 2023 · 3 revisions

Prev Home Next

Constraint: ArrayDistinctProperty

Description

Check each object element in an array has a specified property that is distinct

This differs from ArrayUnique, which checks for unique items in the array, whereas ArrayDistinctProperty checks objects within an array to ensure that a specific property is unique

V8n Tag Abbreviation

adistinctp

Fields

Field Type Description
PropertyName string the name of the property (in each array element object) to check for distinct (uniqueness)
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

Examples

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.ArrayDistinctProperty{
                        PropertyName: "bar",
                        IgnoreCase:   true,
                    },
                },
            },
        },
    }

    obj := `{
        "foo": [{"bar": "one"}, {"bar": "One"}, {"bar": "two"}]
    }`
    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\n", i+1, v.Message, v.Property, v.Path)
    }

    obj = `{
        "foo": [{"bar": "one"}, {"bar": "two"}, {"bar": "three"}]
    }`
    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\n", i+1, v.Message, v.Property, v.Path)
    }
}

try on go-playground

Struct v8n tag example...
package main

import (
    "fmt"

    "github.com/marrow16/valix"
)

type MyStruct struct {
    Foo []string `json:"foo" v8n:"&adistinctp{PropertyName:'bar', IgnoreCase:true,}"`
}

var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)

func main() {
    obj := `{
        "foo": [{"bar": "one"}, {"bar": "One"}, {"bar": "two"}]
    }`
    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\n", i+1, v.Message, v.Property, v.Path)
    }

    obj = `{
        "foo": [{"bar": "one"}, {"bar": "two"}, {"bar": "three"}]
    }`
    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\n", i+1, v.Message, v.Property, v.Path)
    }
}

try on go-playground

Clone this wiki locally