Esempio n. 1
0
// Validate validates iban code.
func Validate(value string) error {
	err := validateMinLength(value)
	if err != nil {
		return err
	}

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

	structure, ok := country.GetBbanStructure(code)
	if !ok {
		return ErrCountryCodeNotPresent
	}

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

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

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

	return nil
}
Esempio n. 2
0
func (s *ValidateTestSuite) TestValidateBbanStructure() {
	for _, cs := range validCases {
		structure, ok := country.GetBbanStructure(cs.CountryCode)
		s.True(ok, "IBAN = %s", cs.Iban)
		err := validateBbanStructure(cs.Iban, structure)
		s.NoError(err, "IBAN = %s", cs.Iban)
	}
}
Esempio n. 3
0
func (s *ValidateTestSuite) TestValidateBbanLengthInvalid() {
	for _, cs := range invalidBbanCases {
		structure, ok := country.GetBbanStructure(cs.CountryCode)
		s.True(ok, "IBAN = %s", cs.Iban)
		err := validateBbanLength(cs.Iban, structure)
		s.Error(err, "IBAN = %s", cs.Iban)
		s.Equal(cs.Error, ErrInvalidBbanLength, "IBAN = %s", cs.Iban)
	}
}
Esempio n. 4
0
func extractBbanPart(value string, entryType bban.EntryType) string {
	bban := extractBban(value)
	code := extractCountryCode(value)
	structure, _ := country.GetBbanStructure(code)
	offset := 0

	for _, part := range structure.Parts() {
		value := bban[offset : offset+part.Length]
		if part.EntryType == entryType {
			return value
		}
		offset += part.Length
	}

	return ""
}