Skip to content

constraint.SetConditionIf

marrow16 edited this page Jan 21, 2023 · 3 revisions

Prev Home Next

Constraint: SetConditionIf

Description

Is a special constraint that wraps another constraint and sets a condition based on whether that wrapped constraint is ok or fails

Note: the wrapped constraint cannot add any violations and cannot stop the validation (i.e. it is called 'silently')

V8n Tag Abbreviation

cif

Fields

Field Type Description
Constraint Constraint is the wrapped constraint to be checked
If this is nil, the SetOk condition is always set
Note: the wrapped constraint cannot add any violations and cannot stop the validation (i.e. it is called 'silently')
SetOk string is the condition to set if the wrapped constraint is ok
Note: if this is an empty string - no condition is set
SetFail string is the condition to set if the wrapped constraint fails
Note: if this is an empty string - no condition is set
Parent bool by default, conditions are set on the current property or object - but specifying true for this field means the condition is set on the parent object too
Global bool setting this field to true means the condition is set for the entire validator context

Examples

Using SetConditionIf followed by a conditional constraint...

The following examples show using SetConditionIf to determine if a value is all uppercase followed by a conditional constraint that is only checked when the value is not all uppercase...

package main

import (
    "fmt"
    "regexp"

    "github.com/marrow16/valix"
)

func main() {
    validator := &valix.Validator{
        Properties: valix.Properties{
            "foo": {
                Type: valix.JsonString,
                Constraints: valix.Constraints{
                    &valix.SetConditionIf{
                        Constraint: &valix.StringUppercase{},
                        SetFail:    "NOT_ALL_UPPER",
                    },
                    &valix.ConditionalConstraint{
                        When: []string{"NOT_ALL_UPPER"},
                        Constraint: &valix.StringPattern{
                            Regexp:  *regexp.MustCompile("^[A-Z]"),
                            Message: "Must start with uppercase letter when not all uppercase",
                        },
                    },
                },
            },
        },
    }

    obj := `{"foo": "this is not all UPPERCASE and doesn't start with a an uppercase letter"}`
    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": "This is not all UPPERCASE but does start with a an uppercase letter"}`
    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": "0 ALL UPPERCASE BUT STARTS WITH DIGIT IS OK"}`
    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": "0 Not ALL UPPERCASE BUT STARTS WITH DIGIT IS OK"}`
    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:"&cif{con:&strupper, fail:'NOT_ALL_UPPER'},&[NOT_ALL_UPPER]strpatt{Regexp:'^[A-Z]',Msg:'Must start with uppercase letter when not all uppercase'}"`
}

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

func main() {
    obj := `{"foo": "this is not all UPPERCASE and doesn't start with a an uppercase letter"}`
    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": "This is not all UPPERCASE but does start with a an uppercase letter"}`
    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": "0 ALL UPPERCASE BUT STARTS WITH DIGIT IS OK"}`
    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": "0 Not ALL UPPERCASE BUT STARTS WITH DIGIT IS OK"}`
    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