Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BIC ISO format validator #758

Merged
merged 1 commit into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
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 @@ -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
}
7 changes: 7 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
}