diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d7800d264..1a75d8920 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -71,6 +71,7 @@ The type attribute can be add,update,fix,remove. InetAddressValidator does not need its instance variable, so uses a touch less memory. Pick up maven-antrun-plugin version from parent POM org.apache:apache. + IBANValidator: add method validate with validation status DomainValidatorTest: added Maven profile to simplfy execution. IBANValidator: add Honduras from v99. Improve IBANValidatorTest by using registry from swift. diff --git a/src/main/java/org/apache/commons/validator/routines/IBANValidator.java b/src/main/java/org/apache/commons/validator/routines/IBANValidator.java index 3d6d4b8b6..f76636b2d 100644 --- a/src/main/java/org/apache/commons/validator/routines/IBANValidator.java +++ b/src/main/java/org/apache/commons/validator/routines/IBANValidator.java @@ -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; } /** diff --git a/src/main/java/org/apache/commons/validator/routines/IBANValidatorStatus.java b/src/main/java/org/apache/commons/validator/routines/IBANValidatorStatus.java new file mode 100644 index 000000000..037f6e457 --- /dev/null +++ b/src/main/java/org/apache/commons/validator/routines/IBANValidatorStatus.java @@ -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 +} diff --git a/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java index 8b848fac7..a748a0739 100644 --- a/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/IBANValidatorTest.java @@ -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; @@ -479,4 +480,20 @@ public void validatorShouldExistWithProperConfiguration(final String countryName } assertTrue(allPatterns.isEmpty(), "Unrecognized patterns: " + allPatterns + " for" + countryInfo); } + + public static Stream 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)); + } }