From 7ee038882a8bd26766fa529ace5124bf232cd5b4 Mon Sep 17 00:00:00 2001 From: Oleksii Kulikov Date: Fri, 23 Apr 2021 15:56:48 +0200 Subject: [PATCH] Add BIC ISO format validator --- baked_in.go | 12 ++++++++++++ doc.go | 7 +++++++ regexes.go | 2 ++ validator_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/baked_in.go b/baked_in.go index dd2570596..dfb85e3fb 100644 --- a/baked_in.go +++ b/baked_in.go @@ -190,6 +190,7 @@ var ( "iso3166_1_alpha3": isIso3166Alpha3, "iso3166_1_alpha_numeric": isIso3166AlphaNumeric, "bcp47_language_tag": isBCP47LanguageTag, + "bic": isIsoBicFormat, } ) @@ -2300,3 +2301,14 @@ func isBCP47LanguageTag(fl FieldLevel) bool { panic(fmt.Sprintf("Bad field type %T", field.Interface())) } + +// 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 +func isIsoBicFormat(fl FieldLevel) bool { + bicString := fl.Field().String() + + if !bicRegex.MatchString(bicString) { + return false + } + + return true +} diff --git a/doc.go b/doc.go index 207d9879a..eafad0db4 100644 --- a/doc.go +++ b/doc.go @@ -1228,6 +1228,13 @@ More information on https://pkg.go.dev/golang.org/x/text/language Usage: bcp47_language_tag +BIC (SWIFT code) + +This validates that a string value is a valid Business Identifier Code (SWIFT code), defined in ISO 9362. +More information on https://www.iso.org/standard/60390.html + + Usage: bic + TimeZone This validates that a string value is a valid time zone based on the time zone database present on the system. diff --git a/regexes.go b/regexes.go index b741f4e17..7b01e1325 100644 --- a/regexes.go +++ b/regexes.go @@ -49,6 +49,7 @@ const ( hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(>)|(<)|(")|(&)+[;]?` hTMLRegexString = `<[/]?([a-zA-Z]+).*?>` splitParamsRegexString = `'[^']*'|\S+` + bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$` ) var ( @@ -98,4 +99,5 @@ var ( hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString) hTMLRegex = regexp.MustCompile(hTMLRegexString) splitParamsRegex = regexp.MustCompile(splitParamsRegexString) + bicRegex = regexp.MustCompile(bicRegexString) ) diff --git a/validator_test.go b/validator_test.go index 1b2e39b94..4a72d4c5b 100644 --- a/validator_test.go +++ b/validator_test.go @@ -11201,3 +11201,44 @@ func TestBCP47LanguageTagValidation(t *testing.T) { _ = validate.Var(2, "bcp47_language_tag") }, "Bad field type int") } + +func TestBicIsoFormatValidation(t *testing.T) { + tests := []struct { + value string `validate:"bic"` + tag string + expected bool + }{ + {"SBICKEN1345", "bic", true}, + {"SBICKEN1", "bic", true}, + {"SBICKENY", "bic", true}, + {"SBICKEN1YYP", "bic", true}, + {"SBIC23NXXX", "bic", false}, + {"S23CKENXXXX", "bic", false}, + {"SBICKENXX", "bic", false}, + {"SBICKENXX9", "bic", false}, + {"SBICKEN13458", "bic", false}, + {"SBICKEN", "bic", false}, + } + + validate := New() + + for i, test := range tests { + + errs := validate.Var(test.value, test.tag) + + if test.expected { + if !IsEqual(errs, nil) { + t.Fatalf("Index: %d bic failed Error: %s", i, errs) + } + } else { + if IsEqual(errs, nil) { + t.Fatalf("Index: %d bic failed Error: %s", i, errs) + } else { + val := getError(errs, "", "") + if val.Tag() != "bic" { + t.Fatalf("Index: %d bic failed Error: %s", i, errs) + } + } + } + } +}