Skip to content

Commit

Permalink
Add BIC ISO format validator (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeexel committed May 6, 2021
1 parent b95d43b commit c206620
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
12 changes: 12 additions & 0 deletions baked_in.go
Expand Up @@ -190,6 +190,7 @@ var (
"iso3166_1_alpha3": isIso3166Alpha3,
"iso3166_1_alpha_numeric": isIso3166AlphaNumeric,
"bcp47_language_tag": isBCP47LanguageTag,
"bic": isIsoBicFormat,
}
)

Expand Down Expand Up @@ -2312,3 +2313,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
}
7 changes: 7 additions & 0 deletions doc.go
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions regexes.go
Expand Up @@ -49,6 +49,7 @@ const (
hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?`
hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
splitParamsRegexString = `'[^']*'|\S+`
bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
)

var (
Expand Down Expand Up @@ -98,4 +99,5 @@ var (
hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString)
hTMLRegex = regexp.MustCompile(hTMLRegexString)
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
bicRegex = regexp.MustCompile(bicRegexString)
)
41 changes: 41 additions & 0 deletions validator_test.go
Expand Up @@ -11223,3 +11223,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)
}
}
}
}
}

0 comments on commit c206620

Please sign in to comment.