Skip to content

Commit 49e1053

Browse files
committed
feat: separate 2014 and 2022 bic implementations, update README to reflect changes
1 parent a74c100 commit 49e1053

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ validate := validator.New(validator.WithRequiredStructEnabled())
164164
| base64 | Base64 String |
165165
| base64url | Base64URL String |
166166
| base64rawurl | Base64RawURL String |
167-
| bic | Business Identifier Code (ISO 9362) |
167+
| bic_iso_9362_2014 | Business Identifier Code (ISO 9362:2014) |
168+
| bic | Business Identifier Code (ISO 9362:2022) |
168169
| bcp47_language_tag | Language tag (BCP 47) |
169170
| btc_addr | Bitcoin Address |
170171
| btc_addr_bech32 | Bitcoin Bech32 Address (segwit) |

baked_in.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ var (
237237
"bcp47_language_tag": isBCP47LanguageTag,
238238
"postcode_iso3166_alpha2": isPostcodeByIso3166Alpha2,
239239
"postcode_iso3166_alpha2_field": isPostcodeByIso3166Alpha2Field,
240-
"bic": isIsoBicFormat,
240+
"bic_iso_9362_2014": isIsoBic2014Format,
241+
"bic": isIsoBic2022Format,
241242
"semver": isSemverFormat,
242243
"dns_rfc1035_label": isDnsRFC1035LabelFormat,
243244
"credit_card": isCreditCard,
@@ -2943,11 +2944,18 @@ func isBCP47LanguageTag(fl FieldLevel) bool {
29432944
panic(fmt.Sprintf("Bad field type %s", field.Type()))
29442945
}
29452946

2946-
// 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
2947-
func isIsoBicFormat(fl FieldLevel) bool {
2947+
// isIsoBic2014Format is the validation function for validating if the current field's value is a valid Business Identifier Code (SWIFT code), defined in ISO 9362 2014
2948+
func isIsoBic2014Format(fl FieldLevel) bool {
29482949
bicString := fl.Field().String()
29492950

2950-
return bicRegex().MatchString(bicString)
2951+
return bic2014Regex().MatchString(bicString)
2952+
}
2953+
2954+
// isIsoBic2022Format is the validation function for validating if the current field's value is a valid Business Identifier Code (SWIFT code), defined in ISO 9362 2022
2955+
func isIsoBic2022Format(fl FieldLevel) bool {
2956+
bicString := fl.Field().String()
2957+
2958+
return bic2022Regex().MatchString(bicString)
29512959
}
29522960

29532961
// isSemverFormat is the validation function for validating if the current field's value is a valid semver version, defined in Semantic Versioning 2.0.0

regexes.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ const (
6868
hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
6969
jWTRegexString = "^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]*$"
7070
splitParamsRegexString = `'[^']*'|\S+`
71-
bicRegexString = `^[A-Z0-9]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$`
71+
bic2014RegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
72+
bic2022RegexString = `^[A-Z0-9]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$`
7273
semverRegexString = `^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$` // numbered capture groups https://semver.org/
7374
dnsRegexStringRFC1035Label = "^[a-z]([-a-z0-9]*[a-z0-9])?$"
7475
cveRegexString = `^CVE-(1999|2\d{3})-(0[^0]\d{2}|0\d[^0]\d{1}|0\d{2}[^0]|[1-9]{1}\d{3,})$` // CVE Format Id https://cve.mitre.org/cve/identifiers/syntaxchange.html
@@ -153,7 +154,8 @@ var (
153154
hTMLRegex = lazyRegexCompile(hTMLRegexString)
154155
jWTRegex = lazyRegexCompile(jWTRegexString)
155156
splitParamsRegex = lazyRegexCompile(splitParamsRegexString)
156-
bicRegex = lazyRegexCompile(bicRegexString)
157+
bic2014Regex = lazyRegexCompile(bic2014RegexString)
158+
bic2022Regex = lazyRegexCompile(bic2022RegexString)
157159
semverRegex = lazyRegexCompile(semverRegexString)
158160
dnsRegexRFC1035Label = lazyRegexCompile(dnsRegexStringRFC1035Label)
159161
cveRegex = lazyRegexCompile(cveRegexString)

validator_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13202,7 +13202,47 @@ func TestBCP47LanguageTagValidation(t *testing.T) {
1320213202
}, "Bad field type int")
1320313203
}
1320413204

13205-
func TestBicIsoFormatValidation(t *testing.T) {
13205+
func TestBicIso2014FormatValidation(t *testing.T) {
13206+
tests := []struct {
13207+
value string `validate:"bic_iso_9362_2014"`
13208+
tag string
13209+
expected bool
13210+
}{
13211+
{"SBICKEN1345", "bic_iso_9362_2014", true},
13212+
{"SBICKEN1", "bic_iso_9362_2014", true},
13213+
{"SBICKENY", "bic_iso_9362_2014", true},
13214+
{"SBICKEN1YYP", "bic_iso_9362_2014", true},
13215+
{"SBIC23NXXX", "bic_iso_9362_2014", false},
13216+
{"S23CKENXXXX", "bic_iso_9362_2014", false},
13217+
{"SBICKENXX", "bic_iso_9362_2014", false},
13218+
{"SBICKENXX9", "bic_iso_9362_2014", false},
13219+
{"SBICKEN13458", "bic_iso_9362_2014", false},
13220+
{"SBICKEN", "bic_iso_9362_2014", false},
13221+
}
13222+
13223+
validate := New()
13224+
13225+
for i, test := range tests {
13226+
errs := validate.Var(test.value, test.tag)
13227+
13228+
if test.expected {
13229+
if !IsEqual(errs, nil) {
13230+
t.Fatalf("Index: %d bic_iso_9362_2014 failed Error: %s", i, errs)
13231+
}
13232+
} else {
13233+
if IsEqual(errs, nil) {
13234+
t.Fatalf("Index: %d bic_iso_9362_2014 failed Error: %s", i, errs)
13235+
} else {
13236+
val := getError(errs, "", "")
13237+
if val.Tag() != "bic_iso_9362_2014" {
13238+
t.Fatalf("Index: %d bic_iso_9362_2014 failed Error: %s", i, errs)
13239+
}
13240+
}
13241+
}
13242+
}
13243+
}
13244+
13245+
func TestBicIso2022FormatValidation(t *testing.T) {
1320613246
tests := []struct {
1320713247
value string `validate:"bic"`
1320813248
tag string

0 commit comments

Comments
 (0)