Skip to content

Commit

Permalink
Refactor iban country code validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Juraj Bubniak committed Jan 3, 2020
1 parent a838242 commit 53903ca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
10 changes: 5 additions & 5 deletions iban/iban.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func Validate(value string) error {
return err
}

code, err := validateCountryCode(value)
if err != nil {
code := extractCountryCode(value)
if err := validateCountryCode(code); err != nil {
return err
}

Expand All @@ -99,15 +99,15 @@ func Validate(value string) error {
return ErrCountryCodeNotPresent
}

if err = validateBbanLength(value, structure); err != nil {
if err := validateBbanLength(value, structure); err != nil {
return err
}

if err = validateBbanStructure(value, structure); err != nil {
if err := validateBbanStructure(value, structure); err != nil {
return err
}

if err = validateCheckDigit(value, code); err != nil {
if err := validateCheckDigit(value, code); err != nil {
return err
}

Expand Down
13 changes: 11 additions & 2 deletions iban/iban_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ func TestValidateDigit(t *testing.T) {
func TestValidateCountryCode(t *testing.T) {
for _, cs := range validCases {
t.Run(cs.iban, func(t *testing.T) {
code, err := validateCountryCode(cs.iban)
code := extractCountryCode(cs.iban)
err := validateCountryCode(code)
require.NoError(t, err)
require.Equal(t, cs.countryCode, code)
})
}
}
Expand Down Expand Up @@ -396,3 +396,12 @@ func TestMustParseInvalid(t *testing.T) {
})
}
}

func BenchmarkValidate(b *testing.B) {
b.ReportAllocs()
b.SetBytes(2)

for i := 0; i < b.N; i++ {
_ = Validate("AL47212110090000000235698741")
}
}
15 changes: 1 addition & 14 deletions iban/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,7 @@ func validateMinLength(value string) error {
return nil
}

func validateCountryCode(value string) (string, error) {
code := extractCountryCode(value)
if err := validateCountryCodeFormat(code); err != nil {
return "", err
}

if !country.Exists(code) {
return "", ErrCountryCodeNotPresent
}

return code, nil
}

func validateCountryCodeFormat(code string) error {
func validateCountryCode(code string) error {
for _, r := range code {
if unicode.IsLower(r) {
return ErrCountryCodeNotUpper
Expand Down

0 comments on commit 53903ca

Please sign in to comment.