Skip to content

constraint.StringLessThanOrEqualOther

marrow16 edited this page Jan 21, 2023 · 4 revisions

Prev Home Next

Constraint: StringLessThanOrEqualOther

Description

Check that a string value is less than or equal to another named property value

Note: this constraint is strict - if either property value is not a string then this constraint fails

V8n Tag Abbreviation

strlteo

Fields

Field Type Description
PropertyName string the property name of the other value to compare against
CaseInsensitive bool when set, the comparison is case-insensitive
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, Stop prevents further validation checks on the property if this constraint fails

Note: the PropertyName field can also be JSON dot notation path - where leading dots allow traversal up the object tree and names, separated by dots, allow traversal down the object tree.
A single dot at start is equivalent to no starting dot (i.e. a property name at the same level)

Examples

Programmatic example...
package main

import (
    "fmt"

    "github.com/marrow16/valix"
)

func main() {
    validator := &valix.Validator{
        Properties: valix.Properties{
            "start": {
                Type: valix.JsonString,
            },
            "end": {
                Type: valix.JsonString,
                Constraints: valix.Constraints{
                    &valix.StringLessThanOrEqualOther{
                        PropertyName:    "start",
                        CaseInsensitive: true,
                    },
                },
            },
        },
    }

    obj := `{"start": "a", "end": "b"}`
    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 = `{"start": "b", "end": "b"}`
    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 = `{"start": "Baa", "end": "b"}`
    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 {
    Start string `json:"start"`
    End   string `json:"end" v8n:"&strlteo{property:'start', insensitive:true}"`
}

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

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

    obj := `{"start": "a", "end": "b"}`
    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\n", i+1, v.Message, v.Property, v.Path)
    }

    obj = `{"start": "B", "end": "b"}`
    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\n", i+1, v.Message, v.Property, v.Path)
    }

    obj = `{"start": "Baa", "end": "b"}`
    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\n", i+1, v.Message, v.Property, v.Path)
    }
}

try on go-playground

With relative array value...
package main

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

    "github.com/marrow16/valix"
)

type StringsRequest struct {
    Strings []string `json:"strings" v8n:"type:array,&aof{Type:'string', Constraints:[&acond{When:'!first', Constraint:&strlteo{PropertyName:'[-1]',insensitive:true,Msg:'Must be less or equal to previous'}}]}"`
}

var stringsReqValidator = valix.MustCompileValidatorFor(StringsRequest{}, nil)

func main() {
    stringsRequest := &StringsRequest{}

    req := buildSimulatedHttpReq(`{"strings":["b","a","b"]}`)
    ok, violations, _ := stringsReqValidator.RequestValidateInto(req, stringsRequest)
    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(`{"strings":["baaaa","Baa","B","b"]}`)
    ok, violations, _ = stringsReqValidator.RequestValidateInto(req, stringsRequest)
    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(body string) *http.Request {
    req, _ := http.NewRequest("POST", "example.com/test", strings.NewReader(body))
    return req
}

try on go-playground

With path traversal...
package main

import (
    "fmt"

    "github.com/marrow16/valix"
)

type MyStruct struct {
    Foo struct {
        Bar string `json:"bar" v8n:"&strlteo{property:'..baz.qux',insensitive}"`
    } `json:"foo"`
    Baz struct {
        Qux string `json:"qux"`
    } `json:"baz"`
}

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

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

    ok, violations, _ := validator.ValidateStringInto(`{"foo": {"bar": "b"}, "baz": {"qux": "A"}}`, 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": {"bar": "A"}, "baz": {"qux": "b"}}`, 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