Skip to content

Commit c206620

Browse files
authored
Add BIC ISO format validator (#758)
1 parent b95d43b commit c206620

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

baked_in.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ var (
190190
"iso3166_1_alpha3": isIso3166Alpha3,
191191
"iso3166_1_alpha_numeric": isIso3166AlphaNumeric,
192192
"bcp47_language_tag": isBCP47LanguageTag,
193+
"bic": isIsoBicFormat,
193194
}
194195
)
195196

@@ -2312,3 +2313,14 @@ func isBCP47LanguageTag(fl FieldLevel) bool {
23122313

23132314
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
23142315
}
2316+
2317+
// isIsoBicFormat is the validation function for validating if the current field's value is a valid Business Identifier Code (SWIFT code), defined in ISO 9362
2318+
func isIsoBicFormat(fl FieldLevel) bool {
2319+
bicString := fl.Field().String()
2320+
2321+
if !bicRegex.MatchString(bicString) {
2322+
return false
2323+
}
2324+
2325+
return true
2326+
}

doc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,13 @@ More information on https://pkg.go.dev/golang.org/x/text/language
12281228
12291229
Usage: bcp47_language_tag
12301230
1231+
BIC (SWIFT code)
1232+
1233+
This validates that a string value is a valid Business Identifier Code (SWIFT code), defined in ISO 9362.
1234+
More information on https://www.iso.org/standard/60390.html
1235+
1236+
Usage: bic
1237+
12311238
TimeZone
12321239
12331240
This validates that a string value is a valid time zone based on the time zone database present on the system.

regexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const (
4949
hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?`
5050
hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
5151
splitParamsRegexString = `'[^']*'|\S+`
52+
bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
5253
)
5354

5455
var (
@@ -98,4 +99,5 @@ var (
9899
hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString)
99100
hTMLRegex = regexp.MustCompile(hTMLRegexString)
100101
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
102+
bicRegex = regexp.MustCompile(bicRegexString)
101103
)

validator_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11223,3 +11223,44 @@ func TestBCP47LanguageTagValidation(t *testing.T) {
1122311223
_ = validate.Var(2, "bcp47_language_tag")
1122411224
}, "Bad field type int")
1122511225
}
11226+
11227+
func TestBicIsoFormatValidation(t *testing.T) {
11228+
tests := []struct {
11229+
value string `validate:"bic"`
11230+
tag string
11231+
expected bool
11232+
}{
11233+
{"SBICKEN1345", "bic", true},
11234+
{"SBICKEN1", "bic", true},
11235+
{"SBICKENY", "bic", true},
11236+
{"SBICKEN1YYP", "bic", true},
11237+
{"SBIC23NXXX", "bic", false},
11238+
{"S23CKENXXXX", "bic", false},
11239+
{"SBICKENXX", "bic", false},
11240+
{"SBICKENXX9", "bic", false},
11241+
{"SBICKEN13458", "bic", false},
11242+
{"SBICKEN", "bic", false},
11243+
}
11244+
11245+
validate := New()
11246+
11247+
for i, test := range tests {
11248+
11249+
errs := validate.Var(test.value, test.tag)
11250+
11251+
if test.expected {
11252+
if !IsEqual(errs, nil) {
11253+
t.Fatalf("Index: %d bic failed Error: %s", i, errs)
11254+
}
11255+
} else {
11256+
if IsEqual(errs, nil) {
11257+
t.Fatalf("Index: %d bic failed Error: %s", i, errs)
11258+
} else {
11259+
val := getError(errs, "", "")
11260+
if val.Tag() != "bic" {
11261+
t.Fatalf("Index: %d bic failed Error: %s", i, errs)
11262+
}
11263+
}
11264+
}
11265+
}
11266+
}

0 commit comments

Comments
 (0)