Skip to content

constraint.FailingConstraint

marrow16 edited this page Jan 21, 2023 · 5 revisions

Prev Home Next

Constraint: FailingConstraint

Description

Is a utility constraint that always fails

V8n Tag Abbreviation

fail

Fields

Field Type Description
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, prevents further validation checks on the property if this constraint fails
StopAll bool when set to true, stops the entire validation

Examples

Programmatic example...
package main

import (
    "fmt"

    "github.com/marrow16/valix"
)

func main() {
    validator := &valix.Validator{
        Properties: valix.Properties{
            "foo": {
                Type:      valix.JsonAny,
                Mandatory: true,
                Order:     -1,
                Constraints: valix.Constraints{
                    &valix.FailingConstraint{
                        StopAll: true,
                        Message: "This always fails",
                    },
                },
            },
            "bar": {
                Type:      valix.JsonAny,
                Mandatory: true,
                Constraints: valix.Constraints{
                    &valix.FailingConstraint{
                        StopAll: true,
                        Message: "This always fails (but is never reached)",
                    },
                },
            },
        },
    }

    ok, violations, _ := validator.ValidateString(`{"foo": "aaa"}`)
    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": "aaa", "bar": "bbb"}`)
    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 interface{} `json:"foo" v8n:"order:-1, &fail{stopAll:true, msg:'This always fails!'}"`
    Bar interface{} `json:"bar" v8n:"&fail{msg:'This always fails (but is never reached)'}"`
}

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

func main() {
    ok, violations, _ := validator.ValidateString(`{"foo": "aaa"}`)
    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": "aaa", "bar": "bbb"}`)
    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