Skip to content

Commit b751d80

Browse files
hoyeonyymarko-bekhta
authored andcommitted
HV-1923: Fix CPF constraint to reject incorrect formats
1 parent a6f0ac3 commit b751d80

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

engine/src/main/java/org/hibernate/validator/constraints/br/CPF.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @author Victor Rezende dos Santos
3232
*/
3333
@Pattern.List({
34-
@Pattern(regexp = "([0-9]{3}[.]?[0-9]{3}[.]?[0-9]{3}-[0-9]{2})|([0-9]{11})"),
34+
@Pattern(regexp = "([0-9]{3}\\.[0-9]{3}\\.[0-9]{3}-[0-9]{2})|([0-9]{11})"),
3535
// XXX.XXX.XXX-XX where X is always the same digit are not a valid CPFs, but all of them passes the mod check. Needs to be singled out each one via regexp
3636
@Pattern(regexp = "^(?:(?!000\\.?000\\.?000-?00).)*$"),
3737
@Pattern(regexp = "^(?:(?!111\\.?111\\.?111-?11).)*$"),

engine/src/main/java/org/hibernate/validator/internal/constraintvalidators/hv/br/CPFValidator.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,10 @@
1818
*/
1919
public class CPFValidator implements ConstraintValidator<CPF, CharSequence> {
2020
private static final Pattern DIGITS_ONLY = Pattern.compile( "\\d+" );
21-
private static final Pattern SINGLE_DASH_SEPARATOR = Pattern.compile( "\\d+-\\d\\d" );
2221

2322
private final Mod11CheckValidator withSeparatorMod11Validator1 = new Mod11CheckValidator();
2423
private final Mod11CheckValidator withSeparatorMod11Validator2 = new Mod11CheckValidator();
2524

26-
private final Mod11CheckValidator withDashOnlySeparatorMod11Validator1 = new Mod11CheckValidator();
27-
private final Mod11CheckValidator withDashOnlySeparatorMod11Validator2 = new Mod11CheckValidator();
28-
2925
private final Mod11CheckValidator withoutSeparatorMod11Validator1 = new Mod11CheckValidator();
3026
private final Mod11CheckValidator withoutSeparatorMod11Validator2 = new Mod11CheckValidator();
3127

@@ -42,14 +38,6 @@ public void initialize(CPF constraintAnnotation) {
4238
0, 12, 13, true, Integer.MAX_VALUE, '0', '0', Mod11Check.ProcessingDirection.RIGHT_TO_LEFT
4339
);
4440

45-
// validates CPF strings with separator, eg 134241313-00
46-
withDashOnlySeparatorMod11Validator1.initialize(
47-
0, 8, 10, true, Integer.MAX_VALUE, '0', '0', Mod11Check.ProcessingDirection.RIGHT_TO_LEFT
48-
);
49-
withDashOnlySeparatorMod11Validator2.initialize(
50-
0, 10, 11, true, Integer.MAX_VALUE, '0', '0', Mod11Check.ProcessingDirection.RIGHT_TO_LEFT
51-
);
52-
5341
// validates CPF strings without separator, eg 13424131300
5442
// checksums as described above, except there are no separator characters
5543
withoutSeparatorMod11Validator1.initialize(
@@ -70,14 +58,9 @@ public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
7058
return withoutSeparatorMod11Validator1.isValid( value, context )
7159
&& withoutSeparatorMod11Validator2.isValid( value, context );
7260
}
73-
else if ( SINGLE_DASH_SEPARATOR.matcher( value ).matches() ) {
74-
return withDashOnlySeparatorMod11Validator1.isValid( value, context )
75-
&& withDashOnlySeparatorMod11Validator2.isValid( value, context );
76-
}
7761
else {
7862
return withSeparatorMod11Validator1.isValid( value, context )
7963
&& withSeparatorMod11Validator2.isValid( value, context );
80-
8164
}
8265
}
8366
}

engine/src/test/java/org/hibernate/validator/test/constraints/annotations/hv/br/CPFValidatorTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,27 @@ public void valid_cpf_without_separator_validates() {
9999
}
100100

101101
@Test
102-
@TestForIssue(jiraKey = "HV-979")
103-
public void correct_cpf_with_dash_only_separator_validates() {
102+
@TestForIssue(jiraKey = "HV-1923")
103+
public void incorrect_cpf_with_dash_only_separator_is_invalid() {
104104
Set<ConstraintViolation<Person>> violations = validator.validate( new Person( "134241313-00" ) );
105-
assertNoViolations( violations );
105+
assertThat( violations ).containsOnlyViolations(
106+
violationOf( CPF.class ).withProperty( "cpf" )
107+
);
108+
}
109+
110+
@Test
111+
@TestForIssue(jiraKey = "HV-1923")
112+
public void incorrect_cpf_with_partial_dots_is_invalid() {
113+
// Test various incorrect formats with partial dots
114+
Set<ConstraintViolation<Person>> violations = validator.validate( new Person( "134.241313-00" ) );
115+
assertThat( violations ).containsOnlyViolations(
116+
violationOf( CPF.class ).withProperty( "cpf" )
117+
);
118+
119+
violations = validator.validate( new Person( "134241.313-00" ) );
120+
assertThat( violations ).containsOnlyViolations(
121+
violationOf( CPF.class ).withProperty( "cpf" )
122+
);
106123
}
107124

108125
@Test

0 commit comments

Comments
 (0)