Skip to content

Commit

Permalink
Do not use regexp to validate country code format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Juraj Bubniak committed Dec 31, 2019
1 parent ed1e9a9 commit 05cd555
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions iban/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package iban

import (
"fmt"
"regexp"
"strconv"
"strings"
"unicode"

"github.com/jbub/banking/bban"
"github.com/jbub/banking/country"
Expand All @@ -27,11 +26,6 @@ const (
defaultCheckDigit = "00"
)

var (
// regexCountryCode holds Regexp for matching country codes.
regexCountryCode = regexp.MustCompile("^[A-Z]+$")
)

func validateMinLength(value string) error {
if len(value) < minIbanSize {
return ErrIbanTooShort
Expand All @@ -41,12 +35,8 @@ func validateMinLength(value string) error {

func validateCountryCode(value string) (string, error) {
code := extractCountryCode(value)
if code != strings.ToUpper(code) {
return "", ErrCountryCodeNotUpper
}

if !regexCountryCode.MatchString(code) {
return "", ErrCountryCodeNotAlpha
if err := validateCountryCodeFormat(code); err != nil {
return "", err
}

if !country.Exists(code) {
Expand All @@ -56,6 +46,18 @@ func validateCountryCode(value string) (string, error) {
return code, nil
}

func validateCountryCodeFormat(code string) error {
for _, r := range code {
if unicode.IsLower(r) {
return ErrCountryCodeNotUpper
}
if !unicode.IsLetter(r) {
return ErrCountryCodeNotAlpha
}
}
return nil
}

func validateCheckDigit(value string, code string) error {
digit := extractCheckDigit(value)
expected, err := calculateCheckDigit(value, code)
Expand Down

0 comments on commit 05cd555

Please sign in to comment.