Skip to content

constraint.FailWhen

marrow16 edited this page Jan 21, 2023 · 5 revisions

Prev Home Next

Constraint: FailWhen

Description

V8n Tag Abbreviation

failw

Fields

Field Type Description
Conditions []string the conditions under which to fail
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"
    "net/http"
    "strings"

    "github.com/marrow16/valix"
)

func main() {
    validator := &valix.Validator{
        Properties: valix.Properties{
            "foo": {
                Type: valix.JsonAny,
                Constraints: valix.Constraints{
                    &valix.FailWhen{
                        Conditions: []string{"METHOD_POST"},
                        Message:    "We do not like people POSTing foos",
                    },
                },
            },
        },
    }

    req := buildSimulatedHttpReq("POST", `{"foo": "aaa"}`)
    ok, violations, _ := validator.RequestValidate(req)
    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)
    }

    req = buildSimulatedHttpReq("PUT", `{"foo": "aaa"}`)
    ok, violations, _ = validator.RequestValidate(req)
    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)
    }
}

func buildSimulatedHttpReq(method string, body string) *http.Request {
    req, _ := http.NewRequest(method, "example.com/test", strings.NewReader(body))
    return req
}

try on go-playground

Struct v8n tag example...
package main

import (
    "fmt"
    "net/http"
    "strings"

    "github.com/marrow16/valix"
)

type MyStruct struct {
    Foo interface{} `json:"foo" v8n:"&failw{conditions:['METHOD_POST'], msg:'We do not like people POSTing foos'}"`
}

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

func main() {
    req := buildSimulatedHttpReq("POST", `{"foo": "aaa"}`)
    ok, violations, _ := validator.RequestValidate(req)
    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)
    }

    req = buildSimulatedHttpReq("PUT", `{"foo": "aaa"}`)
    ok, violations, _ = validator.RequestValidate(req)
    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)
    }
}

func buildSimulatedHttpReq(method string, body string) *http.Request {
    req, _ := http.NewRequest(method, "example.com/test", strings.NewReader(body))
    return req
}

try on go-playground

Clone this wiki locally