Skip to content

constraint.StringPresetPattern

marrow16 edited this page Jan 21, 2023 · 4 revisions

Prev Home Next

Constraint: StringPresetPattern

Description

Check that a string matches a given preset pattern

Note: By default, this constraint is non-strict - if the value being checked is not a string, this constraint does not fail (unless the Strict field is set)

V8n Tag Abbreviation

strpreset

Fields

Field Type Description
Preset string the preset token (which must exist in the presets registry)
Note: If the specified preset token does not exist - the constraint fails!
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
Strict bool when set to true, fails if the value being checked is not a correct type

Preset tokens

token message / description post check?
alpha Value must be only alphabet characters (A-Z, a-z)
alphaNumeric Value must be only alphanumeric characters (A-Z, a-z, 0-9)
barcode Value must be a valid barcode
Checks against EAN, ISBN, ISSN, UPC regexps and verifies using check digit
base64 Value must be a valid base64 encoded string
base64URL Value must be a valid base64 URL encoded string
card Value must be a valid card number
cmyk Value must be a valid cmyk() color string
(components as 0.162 or 16.2%)
cmyk300 Value must be a valid cmyk() color string (maximum 300%)
Post-check ensures components do not exceed 300%
(components as 0.162 or 16.2%)
EAN Value must be a valid EAN code
(EAN-8, 13, 14, 18 or 99)
EAN8 Value must be a valid EAN-8 code
EAN13 Value must be a valid EAN-13 code
DUN14 Value must be a valid DUN-14 code
EAN14 Value must be a valid EAN-14 code
EAN18 Value must be a valid EAN-18 code
EAN99 Value must be a valid EAN-99 code
e164 Value must be a valid E.164 code
hexadecimal Value must be a valid hexadecimal string
hsl Value must be a valid hsl() color string
hsla Value must be a valid hsla() color string
htmlColor Value must be a valid HTML color string
integer Value must be a valid integer string (characters 0-9)
ISBN Value must be a valid ISBN (10 or 13 digit)
ISBN10 Value must be a valid ISBN (10 digit only)
ISBN13 Value must be a valid ISBN (13 digit only)
ISSN Value must be a valid ISSN (8 or 13 digit)
ISSN13 Value must be a valid ISSN (13 digit only)
ISSN8 Value must be a valid ISSN (8 digit only)
numeric Value must be a valid number string
numeric+e Value must be a valid number string
also allows scientific notation - e.g. "1.2e34"
numeric+x Value must be a valid number string
also allows scientific notation - e.g. "1.2e34", or "Inf" or "NaN"
publication Value must be a valid ISBN or ISSN
rgb Value must be a valid rgb() color string
rgba Value must be a valid rgba() color string
rgb-icc Value must be a valid rgb-icc() color string
Post-check ensures components are correct
ULID Value must be a valid ULID
UPC Value must be a valid UPC code (UPC-A or UPC-E)
UPC-A Value must be a valid UPC-A code
UPC-E Value must be a valid UPC-E code
uuid Value must be a valid UUID (lowercase hex chars only)
UUID Value must be a valid UUID (upper or lower hex chars)
uuid1 Value must be a valid UUID (Version 1) (lowercase hex chars only)
UUID1 Value must be a valid UUID (Version 1) (upper or lower hex chars)
uuid2 Value must be a valid UUID (Version 2) (lowercase hex chars only)
UUID2 Value must be a valid UUID (Version 2) (upper or lower hex chars)
uuid3 Value must be a valid UUID (Version 3) (lowercase hex chars only)
UUID3 Value must be a valid UUID (Version 3) (upper or lower hex chars)
uuid4 Value must be a valid UUID (Version 4) (lowercase hex chars only)
UUID4 Value must be a valid UUID (Version 4) (upper or lower hex chars)
uuid5 Value must be a valid UUID (Version 5) (lowercase hex chars only)
UUID5 Value must be a valid UUID (Version 5) (upper or lower hex chars)
Note: Post check indicates whether value is further checked after regexp match - for example, check digits on card numbers & barcodes

The preset tokens can also be used as constraints within v8n tags - for example...

type MyStruct struct {
    Foo `json:"foo" v8n:"&strpreset{preset:'publication'}"`
}

can be expressed as...

type MyStruct struct {
    Foo `json:"foo" v8n:"&publication"`
}

Examples

Programmatic example...
package main

import (
    "fmt"
    "github.com/marrow16/valix"
)

func main() {
    validator := &valix.Validator{
        UseNumber: true,
        Properties: valix.Properties{
            "favouriteBook": {
                Type: valix.JsonString,
                Constraints: valix.Constraints{
                    &valix.StringPresetPattern{
                        Preset: valix.PresetPublication,
                    },
                },
            },
        },
    }

    ok, violations, _ := validator.ValidateString(`{"favouriteBook": "9780201733869"}`)
    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(`{"favouriteBook": "9770036873129"}`)
    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(`{"favouriteBook": "11448759"}`)
    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(`{"favouriteBook": "9780201733860"}`)
    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(`{"favouriteBook": "9770036873121"}`)
    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(`{"favouriteBook": "1144875X"}`)
    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 {
    FavouriteBook string `json:"favouriteBook" v8n:"&strpreset{preset:'publication'}"`
}

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

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

    ok, violations, _ := validator.ValidateStringInto(`{"favouriteBook": "9780201733869"}`, 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(`{"favouriteBook": "9770036873129"}`, 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(`{"favouriteBook": "11448759"}`, 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(`{"favouriteBook": "9780201733860"}`, 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(`{"favouriteBook": "9770036873121"}`, 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(`{"favouriteBook": "1144875X"}`, 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


Registering your own preset...
package main

import (
    "fmt"
    "regexp"

    "github.com/marrow16/valix"
)

type MyStruct struct {
    Hash string `json:"hash" v8n:"&sha256"`
}

var validator *valix.Validator

func init() {
    valix.RegisterPresetPattern("sha256", regexp.MustCompile("^[0-9a-f]{64}$"), "Must be a valid SHA384", nil, true)
    validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
}

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

    ok, violations, _ := validator.ValidateStringInto(`{"hash": "6f5902ac237024bdd0c176cb93063dc46f5902ac237024bdd0c176cb93063dc"}`, 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(`{"hash": "6f5902ac237024bdd0c176cb93063dc46f5902ac237024bdd0c176cb93063dc4"}`, 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