Skip to content

constraint.StringValidCountryCode

marrow16 edited this page Jan 21, 2023 · 4 revisions

Prev Home Next

Constraint: StringValidCountryCode

Description

V8n Tag Abbreviation

strcountry (and iso3166-2,iso3166-1,iso3166,iso3166-1-numeric)

Fields

Field Type Description
Allow3166_2 bool if set, allows ISO3166-2 codes with subdivision (i.e. "cc-sd" country code - subdivision)
Allow3166_2_Obsoletes bool if set, allows obsolete ISO3166-2 codes
AllowUserAssigned bool if set, allows ISO3166-1 user assigned codes
Allow3166_1_ExceptionallyReserved bool if set, allows ISO3166-1 exceptionally reserved codes
Allow3166_1_IndeterminatelyReserved bool if set, allows ISO3166-1 indeterminately reserved codes
Allow3166_1_TransitionallyReserved bool if set, allows ISO3166-1 transitionally reserved codes
Allow3166_1_Deleted bool if set, allows ISO3166-1 deleted codes
Allow3166_1_Numeric bool if set, allows ISO3166-1 numeric codes
NumericOnly bool overrides all other flags (with the exception of AllowUserAssigned) and allows only ISO-3166-1 numeric codes
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: Without setting any of the Allow... fields, this constraint checks basic ISO3166-1 codes

Examples

Programmatic example...
package main

import (
    "fmt"

    "github.com/marrow16/valix"
)

func main() {
    validator := &valix.Validator{
        UseNumber: true,
        Properties: valix.Properties{
            "country": {
                Type: valix.JsonString,
                Constraints: valix.Constraints{
                    &valix.StringValidCountryCode{},
                },
            },
        },
    }

    ok, violations, _ := validator.ValidateString(`{"country": "XX"}`)
    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(`{"country": "GB"}`)
    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 {
    Country string `json:"country" v8n:"&strcountry{}"`
}

var validator = valix.MustCompileValidatorFor(MyStruct{})

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

    ok, violations, _ := validator.ValidateStringInto(`{"country": "XX"}`, 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(`{"country": "GB"}`, 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