Skip to content

constraint.StringValidCurrencyCode

marrow16 edited this page Jan 21, 2023 · 6 revisions

Prev Home Next

Constraint: StringValidCurrencyCode

Description

Check that a string is a valid ISO-4217 currency code

V8n Tag Abbreviation

strccy (and iso4217,iso4217-numeric,iso4217-alpha)

Fields

Field Type Description
AllowNumeric bool when set to true, allows numeric currency codes
AllowHistorical bool when set to true, allows historical currency codes
AllowUnofficial bool when set to true, allows unofficial currency codes
AllowCrypto bool when set to true, allows crypto currency codes
AllowTestCode bool when set to true, allows test currency codes (i.e. "XTS" or numeric "963")
AllowNoCode bool when set to true, allows no code (i.e. "XXX" or numeric "999")
NumericOnly bool set to true to only allow ISO-4217 numeric currency 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

Examples

Programmatic example...
package main

import (
    "fmt"

    "github.com/marrow16/valix"
)

func main() {
    validator := &valix.Validator{
        UseNumber: true,
        Properties: valix.Properties{
            "currency": {
                Type: valix.JsonAny,
                Constraints: valix.Constraints{
                    &valix.StringValidCurrencyCode{
                        AllowNumeric: true,
                    },
                },
            },
        },
    }

    ok, violations, _ := validator.ValidateString(`{"currency": "XYZ"}`)
    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(`{"currency": 123}`)
    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(`{"currency": "GBP"}`)
    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(`{"currency": 826}`)
    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 {
    Currency interface{} `json:"currency" v8n:"&iso4217"`
}

var validator = valix.MustCompileValidatorFor(MyStruct{})

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

    ok, violations, _ := validator.ValidateStringInto(`{"currency": "XYZ"}`, 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(`{"currency": 123}`, 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(`{"currency": "GBP"}`, 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(`{"currency": 826}`, 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