Skip to content

Commit d427198

Browse files
authored
Feature/currency codes (#811)
1 parent 42525d8 commit d427198

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Baked-in Validations
170170
| iso3166_1_alpha3 | Three-letter country code (ISO 3166-1 alpha-3) |
171171
| iso3166_1_alpha_numeric | Numeric country code (ISO 3166-1 numeric) |
172172
| iso3166_2 | Country subdivision code (ISO 3166-2) |
173+
| iso4217 | Currency code (ISO 4217) |
173174
| json | JSON |
174175
| jwt | JSON Web Token (JWT) |
175176
| latitude | Latitude |

baked_in.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ var (
192192
"iso3166_1_alpha3": isIso3166Alpha3,
193193
"iso3166_1_alpha_numeric": isIso3166AlphaNumeric,
194194
"iso3166_2": isIso31662,
195+
"iso4217": isIso4217,
196+
"iso4217_numeric": isIso4217Numeric,
195197
"bcp47_language_tag": isBCP47LanguageTag,
196198
"postcode_iso3166_alpha2": isPostcodeByIso3166Alpha2,
197199
"postcode_iso3166_alpha2_field": isPostcodeByIso3166Alpha2Field,
@@ -2365,6 +2367,28 @@ func isIso31662(fl FieldLevel) bool {
23652367
return iso3166_2[val]
23662368
}
23672369

2370+
// isIso4217 is the validation function for validating if the current field's value is a valid iso4217 currency code.
2371+
func isIso4217(fl FieldLevel) bool {
2372+
val := fl.Field().String()
2373+
return iso4217[val]
2374+
}
2375+
2376+
// isIso4217Numeric is the validation function for validating if the current field's value is a valid iso4217 numeric currency code.
2377+
func isIso4217Numeric(fl FieldLevel) bool {
2378+
field := fl.Field()
2379+
2380+
var code int
2381+
switch field.Kind() {
2382+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2383+
code = int(field.Int())
2384+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
2385+
code = int(field.Uint())
2386+
default:
2387+
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
2388+
}
2389+
return iso4217_numeric[code]
2390+
}
2391+
23682392
// isBCP47LanguageTag is the validation function for validating if the current field's value is a valid BCP 47 language tag, as parsed by language.Parse
23692393
func isBCP47LanguageTag(fl FieldLevel) bool {
23702394
field := fl.Field()

currency_codes.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package validator
2+
3+
var iso4217 = map[string]bool{
4+
"AFN": true, "EUR": true, "ALL": true, "DZD": true, "USD": true,
5+
"AOA": true, "XCD": true, "ARS": true, "AMD": true, "AWG": true,
6+
"AUD": true, "AZN": true, "BSD": true, "BHD": true, "BDT": true,
7+
"BBD": true, "BYN": true, "BZD": true, "XOF": true, "BMD": true,
8+
"INR": true, "BTN": true, "BOB": true, "BOV": true, "BAM": true,
9+
"BWP": true, "NOK": true, "BRL": true, "BND": true, "BGN": true,
10+
"BIF": true, "CVE": true, "KHR": true, "XAF": true, "CAD": true,
11+
"KYD": true, "CLP": true, "CLF": true, "CNY": true, "COP": true,
12+
"COU": true, "KMF": true, "CDF": true, "NZD": true, "CRC": true,
13+
"HRK": true, "CUP": true, "CUC": true, "ANG": true, "CZK": true,
14+
"DKK": true, "DJF": true, "DOP": true, "EGP": true, "SVC": true,
15+
"ERN": true, "SZL": true, "ETB": true, "FKP": true, "FJD": true,
16+
"XPF": true, "GMD": true, "GEL": true, "GHS": true, "GIP": true,
17+
"GTQ": true, "GBP": true, "GNF": true, "GYD": true, "HTG": true,
18+
"HNL": true, "HKD": true, "HUF": true, "ISK": true, "IDR": true,
19+
"XDR": true, "IRR": true, "IQD": true, "ILS": true, "JMD": true,
20+
"JPY": true, "JOD": true, "KZT": true, "KES": true, "KPW": true,
21+
"KRW": true, "KWD": true, "KGS": true, "LAK": true, "LBP": true,
22+
"LSL": true, "ZAR": true, "LRD": true, "LYD": true, "CHF": true,
23+
"MOP": true, "MKD": true, "MGA": true, "MWK": true, "MYR": true,
24+
"MVR": true, "MRU": true, "MUR": true, "XUA": true, "MXN": true,
25+
"MXV": true, "MDL": true, "MNT": true, "MAD": true, "MZN": true,
26+
"MMK": true, "NAD": true, "NPR": true, "NIO": true, "NGN": true,
27+
"OMR": true, "PKR": true, "PAB": true, "PGK": true, "PYG": true,
28+
"PEN": true, "PHP": true, "PLN": true, "QAR": true, "RON": true,
29+
"RUB": true, "RWF": true, "SHP": true, "WST": true, "STN": true,
30+
"SAR": true, "RSD": true, "SCR": true, "SLL": true, "SGD": true,
31+
"XSU": true, "SBD": true, "SOS": true, "SSP": true, "LKR": true,
32+
"SDG": true, "SRD": true, "SEK": true, "CHE": true, "CHW": true,
33+
"SYP": true, "TWD": true, "TJS": true, "TZS": true, "THB": true,
34+
"TOP": true, "TTD": true, "TND": true, "TRY": true, "TMT": true,
35+
"UGX": true, "UAH": true, "AED": true, "USN": true, "UYU": true,
36+
"UYI": true, "UYW": true, "UZS": true, "VUV": true, "VES": true,
37+
"VND": true, "YER": true, "ZMW": true, "ZWL": true, "XBA": true,
38+
"XBB": true, "XBC": true, "XBD": true, "XTS": true, "XXX": true,
39+
"XAU": true, "XPD": true, "XPT": true, "XAG": true,
40+
}
41+
42+
var iso4217_numeric = map[int]bool{
43+
8: true, 12: true, 32: true, 36: true, 44: true,
44+
48: true, 50: true, 51: true, 52: true, 60: true,
45+
64: true, 68: true, 72: true, 84: true, 90: true,
46+
96: true, 104: true, 108: true, 116: true, 124: true,
47+
132: true, 136: true, 144: true, 152: true, 156: true,
48+
170: true, 174: true, 188: true, 191: true, 192: true,
49+
203: true, 208: true, 214: true, 222: true, 230: true,
50+
232: true, 238: true, 242: true, 262: true, 270: true,
51+
292: true, 320: true, 324: true, 328: true, 332: true,
52+
340: true, 344: true, 348: true, 352: true, 356: true,
53+
360: true, 364: true, 368: true, 376: true, 388: true,
54+
392: true, 398: true, 400: true, 404: true, 408: true,
55+
410: true, 414: true, 417: true, 418: true, 422: true,
56+
426: true, 430: true, 434: true, 446: true, 454: true,
57+
458: true, 462: true, 480: true, 484: true, 496: true,
58+
498: true, 504: true, 512: true, 516: true, 524: true,
59+
532: true, 533: true, 548: true, 554: true, 558: true,
60+
566: true, 578: true, 586: true, 590: true, 598: true,
61+
600: true, 604: true, 608: true, 634: true, 643: true,
62+
646: true, 654: true, 682: true, 690: true, 694: true,
63+
702: true, 704: true, 706: true, 710: true, 728: true,
64+
748: true, 752: true, 756: true, 760: true, 764: true,
65+
776: true, 780: true, 784: true, 788: true, 800: true,
66+
807: true, 818: true, 826: true, 834: true, 840: true,
67+
858: true, 860: true, 882: true, 886: true, 901: true,
68+
927: true, 928: true, 929: true, 930: true, 931: true,
69+
932: true, 933: true, 934: true, 936: true, 938: true,
70+
940: true, 941: true, 943: true, 944: true, 946: true,
71+
947: true, 948: true, 949: true, 950: true, 951: true,
72+
952: true, 953: true, 955: true, 956: true, 957: true,
73+
958: true, 959: true, 960: true, 961: true, 962: true,
74+
963: true, 964: true, 965: true, 967: true, 968: true,
75+
969: true, 970: true, 971: true, 972: true, 973: true,
76+
975: true, 976: true, 977: true, 978: true, 979: true,
77+
980: true, 981: true, 984: true, 985: true, 986: true,
78+
990: true, 994: true, 997: true, 999: true,
79+
}

validator_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11083,6 +11083,62 @@ func TestIsIso3166AlphaNumericValidation(t *testing.T) {
1108311083
}, "Bad field type string")
1108411084
}
1108511085

11086+
func TestIsIso4217Validation(t *testing.T) {
11087+
tests := []struct {
11088+
value string `validate:"iso4217"`
11089+
expected bool
11090+
}{
11091+
{"TRY", true},
11092+
{"EUR", true},
11093+
{"USA", false},
11094+
}
11095+
11096+
validate := New()
11097+
11098+
for i, test := range tests {
11099+
11100+
errs := validate.Var(test.value, "iso4217")
11101+
11102+
if test.expected {
11103+
if !IsEqual(errs, nil) {
11104+
t.Fatalf("Index: %d iso4217 failed Error: %s", i, errs)
11105+
}
11106+
} else {
11107+
if IsEqual(errs, nil) {
11108+
t.Fatalf("Index: %d iso4217 failed Error: %s", i, errs)
11109+
}
11110+
}
11111+
}
11112+
}
11113+
11114+
func TestIsIso4217NumericValidation(t *testing.T) {
11115+
tests := []struct {
11116+
value int `validate:"iso4217_numeric"`
11117+
expected bool
11118+
}{
11119+
{8, true},
11120+
{12, true},
11121+
{13, false},
11122+
}
11123+
11124+
validate := New()
11125+
11126+
for i, test := range tests {
11127+
11128+
errs := validate.Var(test.value, "iso4217_numeric")
11129+
11130+
if test.expected {
11131+
if !IsEqual(errs, nil) {
11132+
t.Fatalf("Index: %d iso4217 failed Error: %s", i, errs)
11133+
}
11134+
} else {
11135+
if IsEqual(errs, nil) {
11136+
t.Fatalf("Index: %d iso4217 failed Error: %s", i, errs)
11137+
}
11138+
}
11139+
}
11140+
}
11141+
1108611142
func TestTimeZoneValidation(t *testing.T) {
1108711143
tests := []struct {
1108811144
value string `validate:"timezone"`

0 commit comments

Comments
 (0)