Skip to content
Browse files

HV-906 Exposing ignoreNonDigitCharacters attribute of Luhncheck via …

…@OverridesAttribute
  • Loading branch information...
1 parent 01463c5 commit fcd76679e09063511c6563150ae6c779fd0ec52a @hferentschik hferentschik committed with gunnarmorling Jul 5, 2014
View
9 documentation/src/main/asciidoc/ch02.asciidoc
@@ -598,25 +598,26 @@ level constraint.
|Use
|Hibernate metadata impact
-|@CreditCardNumber
+|@CreditCardNumber(ignoreNonDigitCharacters=)
|CharSequence
|Checks that the annotated character sequence passes the
Luhn checksum test. Note, this validation aims to check for user
mistakes, not credit card validity! See also
- link:$$http://www.merriampark.com/anatomycc.htm$$[Anatomy of Credit Card Numbers].
+ http://www.merriampark.com/anatomycc.htm[Anatomy of Credit Card Numbers]. ignoreNonDigitCharacters
+ allows to ignore non digit characters. The default is +false+.
|None
|@EAN
|CharSequence
|Checks that the annotated character sequence is a valid
-link:$$http://en.wikipedia.org/wiki/International_Article_Number_%28EAN%29$$[EAN] barcode.
+link:$$http://en.wikipedia.org/wiki/International_Article_Number_%28EAN%29$$[EAN] barcode.
type determines the type of barcode. The default is EAN-13.
|None
|@Email
|CharSequence
|Checks whether the specified character sequence is a valid email address. The optional parameters
-regexp and flags allow to specify an additional regular expression (including regular expression
+regexp and flags allow to specify an additional regular expression (including regular expression
flags) which the email must match.
|None
View
8 engine/src/main/java/org/hibernate/validator/constraints/CreditCardNumber.java
@@ -20,6 +20,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
+import javax.validation.OverridesAttribute;
import javax.validation.Payload;
import javax.validation.ReportAsSingleViolation;
@@ -52,6 +53,13 @@
Class<? extends Payload>[] payload() default { };
/**
+ * @return Whether non-digit characters in the validated input should be ignored ({@code true}) or result in a
+ * validation error ({@code false}). Default is {@code false}
+ */
+ @OverridesAttribute(constraint = LuhnCheck.class, name = "ignoreNonDigitCharacters")
+ boolean ignoreNonDigitCharacters() default false;
+
+ /**
* Defines several {@code @CreditCardNumber} annotations on the same element.
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
View
7 ...ain/java/org/hibernate/validator/internal/constraintvalidators/hv/LuhnCheckValidator.java
@@ -55,9 +55,12 @@ public void initialize(LuhnCheck constraintAnnotation) {
@Override
public boolean isCheckDigitValid(List<Integer> digits, char checkDigit) {
int modResult = ModUtil.calculateLuhnMod10Check( digits );
- int checkValue = extractDigit( checkDigit );
+ if ( !Character.isDigit( checkDigit ) ) {
+ return false;
+ }
+
+ int checkValue = extractDigit( checkDigit );
return checkValue == modResult;
}
-
}
View
7 ...in/java/org/hibernate/validator/internal/constraintvalidators/hv/Mod10CheckValidator.java
@@ -79,9 +79,12 @@ public void initialize(Mod10Check constraintAnnotation) {
@Override
public boolean isCheckDigitValid(List<Integer> digits, char checkDigit) {
int modResult = ModUtil.calculateMod10Check( digits, this.multiplier, this.weight );
- int checkValue = extractDigit( checkDigit );
+ if ( !Character.isDigit( checkDigit ) ) {
+ return false;
+ }
+
+ int checkValue = extractDigit( checkDigit );
return checkValue == modResult;
}
-
}
View
34 ...ernate/validator/test/internal/constraintvalidators/hv/CreditCardNumberValidatorTest.java
@@ -90,10 +90,40 @@ public void testNullValue() throws Exception {
assertNumberOfViolations( constraintViolations, 0 );
}
+ @Test
+ @TestForIssue(jiraKey = "HV-906")
+ public void testCharactersAreNotAllowedTest() {
+ CreditCard card = new CreditCard();
+ card.setCreditCardNumber( "text not numbers" );
+ Set<ConstraintViolation<CreditCard>> constraintViolations = validator.validate( card );
+ assertNumberOfViolations( constraintViolations, 1 );
+ }
+
+ @Test
+ @TestForIssue(jiraKey = "HV-906")
+ public void testValidCreditCardNumberWithSpaces() throws Exception {
+ CreditCard card = new CreditCard();
+ card.setCreditCardNumberWithNonDigits( "5412 3456 7890 125" );
+ Set<ConstraintViolation<CreditCard>> constraintViolations = validator.validate( card );
+ assertNumberOfViolations( constraintViolations, 0 );
+ }
+
+ @Test
+ @TestForIssue(jiraKey = "HV-906")
+ public void testValidCreditCardNumberWithDashes() throws Exception {
+ CreditCard card = new CreditCard();
+ card.setCreditCardNumberWithNonDigits( "5412-3456-7890-125" );
+ Set<ConstraintViolation<CreditCard>> constraintViolations = validator.validate( card );
+ assertNumberOfViolations( constraintViolations, 0 );
+ }
+
public static class CreditCard {
@CreditCardNumber
String creditCardNumber;
+ @CreditCardNumber(ignoreNonDigitCharacters = true)
+ String creditCardNumberWithNonDigits;
+
@CreditCardNumber
CharSequence creditCardNumberAsCharSequence;
@@ -104,5 +134,9 @@ public void setCreditCardNumber(String creditCardNumber) {
public void setCreditCardNumberAsCharSequence(CharSequence creditCardNumberAsCharSequence) {
this.creditCardNumberAsCharSequence = creditCardNumberAsCharSequence;
}
+
+ public void setCreditCardNumberWithNonDigits(String creditCardNumberWithNonDigits) {
+ this.creditCardNumberWithNonDigits = creditCardNumberWithNonDigits;
+ }
}
}

0 comments on commit fcd7667

Please sign in to comment.
Something went wrong with that request. Please try again.