-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.StringValidCurrencyCode
marrow16 edited this page Jan 21, 2023
·
6 revisions
Check that a string is a valid ISO-4217 currency code
strccy (and iso4217,iso4217-numeric,iso4217-alpha)
| 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 |
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)
}
}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)
}
}