Skip to content

Commit

Permalink
Merge pull request #300 from slawekjaranowski/VALIDATOR-497
Browse files Browse the repository at this point in the history
[VALIDATOR-497] IBANValidator: add method validate with validation status
  • Loading branch information
garydgregory authored Dec 27, 2024
2 parents d4655c5 + 07602d3 commit 02f543d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Johannes Weberhofer">InetAddressValidator does not need its instance variable, so uses a touch less memory.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Pick up maven-antrun-plugin version from parent POM org.apache:apache.</action>
<!-- ADD -->
<action type="add" issue="VALIDATOR-497" dev="sjaranowski" due-to="Slawomir Jaranowski">IBANValidator: add method validate with validation status</action>
<action type="add" dev="sebb">DomainValidatorTest: added Maven profile to simplfy execution.</action>
<action type="add" dev="sebb">IBANValidator: add Honduras from v99.</action>
<action type="add" dev="sjaranowski" due-to="Slawomir Jaranowski">Improve IBANValidatorTest by using registry from swift.</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,31 @@ public boolean hasValidator(final String code) {
* @return {@code true} if the value is valid
*/
public boolean isValid(final String code) {
return validate(code) == IBANValidatorStatus.VALID;
}

/**
* Validate an IBAN Code
*
* @param code The value validation is being performed on
* @return {@link IBANValidatorStatus} for validation
* @since 1.10.0
*/
public IBANValidatorStatus validate(String code) {
final Validator formatValidator = getValidator(code);
if (formatValidator == null || code.length() != formatValidator.ibanLength || !formatValidator.regexValidator.isValid(code)) {
return false;
if (formatValidator == null) {
return IBANValidatorStatus.UNKNOWN_COUNTRY;
}

if (code.length() != formatValidator.ibanLength) {
return IBANValidatorStatus.INVALID_LENGTH;
}
return IBANCheckDigit.IBAN_CHECK_DIGIT.isValid(code);

if (!formatValidator.regexValidator.isValid(code)) {
return IBANValidatorStatus.INVALID_PATTERN;
}

return IBANCheckDigit.IBAN_CHECK_DIGIT.isValid(code) ? IBANValidatorStatus.VALID : IBANValidatorStatus.INVALID_CHECKSUM;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.validator.routines;

/**
* Statuses of IBAN validation.
*
* @since 1.10.0
*/
public enum IBANValidatorStatus {
/**
* IBAN is valid
*/
VALID,

/**
* IBAN validator for given country is not registered
*/
UNKNOWN_COUNTRY,

/**
* Length for given IBAN is wrong
*/
INVALID_LENGTH,

/**
* Pattern for given IBAN is not match
*/
INVALID_PATTERN,

/**
* Checksum for given IBAN is invalid
*/
INVALID_CHECKSUM
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
Expand Down Expand Up @@ -479,4 +480,20 @@ public void validatorShouldExistWithProperConfiguration(final String countryName
}
assertTrue(allPatterns.isEmpty(), "Unrecognized patterns: " + allPatterns + " for" + countryInfo);
}

public static Stream<Arguments> validateIbanStatuses() {
return Stream.of(
Arguments.of("XX", IBANValidatorStatus.UNKNOWN_COUNTRY),
Arguments.of("AD0101", IBANValidatorStatus.INVALID_LENGTH),
Arguments.of("AD12XX012030200359100100", IBANValidatorStatus.INVALID_PATTERN),
Arguments.of("AD9900012030200359100100", IBANValidatorStatus.INVALID_CHECKSUM),
Arguments.of("AD1200012030200359100100", IBANValidatorStatus.VALID)
);
}

@ParameterizedTest
@MethodSource
public void validateIbanStatuses(String iban, IBANValidatorStatus expectedStatus) {
assertEquals(expectedStatus, IBANValidator.getInstance().validate(iban));
}
}

0 comments on commit 02f543d

Please sign in to comment.