Skip to content

constraint.SetConditionFrom

marrow16 edited this page Jan 21, 2023 · 5 revisions

Prev Home Next

Constraint: SetConditionFrom

Description

Is a utility constraint that can be used to set a condition in the ValidatorContext from the value of the property (to which this constraint is added)

V8n Tag Abbreviation

cfrom

Fields

Field Type Description
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
Prefix string is any prefix to be prepended to the condition token
Mapping map[string]string converts the string value to alternate values (if the value is not found in the map then the original value is used
NullToken string is the condition token used if the value of the property is null/nil. If this field is not set and the property value is null at validation - then a condition token of "null" is used
Format string is an optional format string for dealing with non-string property values

Examples

Programmatic example...
package main

import (
    "fmt"

    "github.com/marrow16/valix"
)

func main() {
    validator := &valix.Validator{
        Properties: valix.Properties{
            "foo": {
                Type:  valix.JsonString,
                Order: -1, // make sure foo is validated before bar - so the condition is set
                Constraints: valix.Constraints{
                    &valix.StringValidToken{
                        Tokens: []string{"positive", "negative", "any"},
                        Stop:   true,
                    },
                    &valix.SetConditionFrom{
                        Parent: true,
                        Prefix: "foo_is_",
                    },
                },
            },
            "bar": {
                Type: valix.JsonNumber,
                Constraints: valix.Constraints{
                    &valix.ConditionalConstraint{
                        When:       []string{"foo_is_positive"},
                        Constraint: &valix.Positive{Message: "Bar must be positive when foo is set to \"positive\""},
                    },
                    &valix.ConditionalConstraint{
                        When:       []string{"foo_is_negative"},
                        Constraint: &valix.Negative{Message: "Bar must be negative when foo is set to \"negative\""},
                    },
                },
            },
        },
    }

    ok, violations, _ := validator.ValidateString(`{"foo": "positive", "bar": -1}`)
    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": "negative", "bar": 1}`)
    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": "positive", "bar": 1}`)
    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": "negative", "bar": -1}`)
    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:"order:-1, &strtoken{tokens:[positive,negative,any], stop:true}, &cfrom{parent:true, prefix:'foo_is_'}"`
    Bar int    `json:"bar" v8n:"&[foo_is_positive]pos{'Bar must be positive when foo is set to \"positive\"'}, &[foo_is_negative]neg{'Bar must be negative when foo is set to \"negative\"'}"`
}

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

func main() {
    my := &MyStruct{}

    ok, violations, _ := validator.ValidateStringInto(`{"foo": "positive", "bar": -1}`, my)
    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.ValidateStringInto(`{"foo": "negative", "bar": 1}`, my)
    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.ValidateStringInto(`{"foo": "positive", "bar": 1}`, my)
    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.ValidateStringInto(`{"foo": "negative", "bar": -1}`, my)
    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