Skip to content

constraint.ArrayConditionalConstraint

marrow16 edited this page Jan 21, 2023 · 5 revisions

Prev Home Next

Constraint: ArrayConditionalConstraint

Description

A special constraint that wraps another constraint - but the wrapped constraint is only checked when the specified when condition is met

V8n Tag Abbreviation

acond

Fields

Field Type Description
When string special token denoting the array condition on which the wrapped constraint is to be checked (see below)
Ancestry unit ancestry depth at which to obtain the current array index information
Constraint Constraint the wrapped constraint
  • "first" - when the array item is the first
  • "!first" - when the array item is not the first
  • "last" - when the array item is the last
  • "!last" - when the array item is not the last
  • "%n" - when the modulus n of the array index is zero
  • ">n" - when the array index is greater than n
  • "<n" - when the array index is less than n
  • "n" - when the array index is n

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.ArrayOf{
                        Type: "string",
                        Constraints: valix.Constraints{
                            &valix.ArrayConditionalConstraint{
                                When: "!first",
                                // when not the first item - make sure the string is greater than the previous array element...
                                Constraint: &valix.StringGreaterThanOther{
                                    PropertyName: "[-1]",
                                    Message:      "Must be greater than previous array element",
                                },
                            },
                        },
                    },
                },
            },
        },
    }
    obj := `{
        "foo": ["f", "b", "c"]
    }`

    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)
    }
}

try on go-playground

Struct v8n tag example...
package main

import (
    "fmt"
    "github.com/marrow16/valix"
)

type MyStruct struct {
    Foo []string `json:"foo" v8n:"&aof{Type:'string', Constraints:[&acond{When:'!first', Constraint:&strgto{PropertyName:'[-1]',Message:'Must be greater than previous array element'}}]}"`
}

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

func main() {
    obj := `{
        "foo": ["f", "b", "c"]
    }`

    my := &MyStruct{}
    ok, violations, _ := validator.ValidateStringInto(obj, my)
    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)
    }
}

try on go-playground

Clone this wiki locally