Skip to content

Commit eb3e3d7

Browse files
committed
HV-2117 NIP validation incorrectly accepts values ending in 0
1 parent 09f631e commit eb3e3d7

File tree

4 files changed

+55
-51
lines changed

4 files changed

+55
-51
lines changed

engine/src/main/java/org/hibernate/validator/internal/constraintvalidators/hv/pl/NIPValidator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,13 @@ public void initialize(NIP constraintAnnotation) {
3232
protected int[] getWeights(List<Integer> digits) {
3333
return WEIGHTS_NIP;
3434
}
35+
36+
@Override
37+
protected boolean checkTwoDigitModuloResult(char checkDigit) {
38+
// From https://pl.wikipedia.org/wiki/Numer_identyfikacji_podatkowej:
39+
// > NIP jest tak generowany, aby nigdy w wyniku tego dzielenia, jako reszta, nie uzyskać liczby 10
40+
//
41+
// which means that the way NIP is generated the checksum can never be 10, so if we got here, we've got an invalid NIP:
42+
return false;
43+
}
3544
}

engine/src/main/java/org/hibernate/validator/internal/constraintvalidators/hv/pl/PolishNumberValidator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.hibernate.validator.internal.util.ModUtil;
1515

1616
/**
17-
* A base class validator for different Polish identification numbers. They differs in the lengths and weights used to calculate the mod sum.
17+
* A base class validator for different Polish identification numbers. They differ in the lengths and weights used to calculate the mod sum.
1818
* In order to implement one you need to provide a method that gives an array of weights
1919
* and {@link ConstraintValidator#initialize(Annotation)} methods.
2020
*
@@ -38,11 +38,15 @@ public boolean isCheckDigitValid(List<Integer> digits, char checkDigit) {
3838
switch ( modResult ) {
3939
case 10:
4040
case 11:
41-
return checkDigit == '0';
41+
return checkTwoDigitModuloResult( checkDigit );
4242
default:
4343
return Character.isDigit( checkDigit ) && modResult == extractDigit( checkDigit );
4444
}
4545
}
4646

47+
protected boolean checkTwoDigitModuloResult(char checkDigit) {
48+
return checkDigit == '0';
49+
}
50+
4751
protected abstract int[] getWeights(List<Integer> digits);
4852
}

engine/src/test/java/org/hibernate/validator/test/constraints/annotations/hv/pl/NIPValidatorTest.java

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.hibernate.validator.constraints.pl.NIP;
1212
import org.hibernate.validator.test.constraints.annotations.AbstractConstrainedTest;
1313

14+
import org.testng.annotations.DataProvider;
1415
import org.testng.annotations.Test;
1516

1617
/**
@@ -40,62 +41,51 @@ public void testIncorrectLength() {
4041
);
4142
}
4243

43-
@Test
44-
public void testCorrectNipNumber() {
45-
assertNoViolations( validator.validate( new Person( "5931423811" ) ) );
46-
assertNoViolations( validator.validate( new Person( "2596048500" ) ) );
47-
assertNoViolations( validator.validate( new Person( "4163450312" ) ) );
48-
assertNoViolations( validator.validate( new Person( "1786052059" ) ) );
49-
assertNoViolations( validator.validate( new Person( "6660057854" ) ) );
50-
assertNoViolations( validator.validate( new Person( "4219220786" ) ) );
51-
assertNoViolations( validator.validate( new Person( "3497264632" ) ) );
52-
44+
@Test(dataProvider = "validNips")
45+
public void testCorrectNipNumber(String nip) {
46+
assertNoViolations( validator.validate( new Person( nip ) ) );
5347
}
5448

55-
@Test
56-
public void testIncorrectNipNumber() {
57-
assertThat( validator.validate( new Person( "123-456-78-14" ) ) )
58-
.containsOnlyViolations(
59-
violationOf( NIP.class ).withProperty( "nip" )
60-
);
61-
assertThat( validator.validate( new Person( "123-45-67-812" ) ) )
62-
.containsOnlyViolations(
63-
violationOf( NIP.class ).withProperty( "nip" )
64-
);
65-
assertThat( validator.validate( new Person( "123-456-32-12" ) ) )
66-
.containsOnlyViolations(
67-
violationOf( NIP.class ).withProperty( "nip" )
68-
);
69-
assertThat( validator.validate( new Person( "5931423812" ) ) )
70-
.containsOnlyViolations(
71-
violationOf( NIP.class ).withProperty( "nip" )
72-
);
73-
assertThat( validator.validate( new Person( "2596048505" ) ) )
74-
.containsOnlyViolations(
75-
violationOf( NIP.class ).withProperty( "nip" )
76-
);
77-
assertThat( validator.validate( new Person( "4163450311" ) ) )
78-
.containsOnlyViolations(
79-
violationOf( NIP.class ).withProperty( "nip" )
80-
);
81-
assertThat( validator.validate( new Person( "1786052053" ) ) )
82-
.containsOnlyViolations(
83-
violationOf( NIP.class ).withProperty( "nip" )
84-
);
85-
assertThat( validator.validate( new Person( "6660057852" ) ) )
86-
.containsOnlyViolations(
87-
violationOf( NIP.class ).withProperty( "nip" )
88-
);
89-
assertThat( validator.validate( new Person( "4219220785" ) ) )
90-
.containsOnlyViolations(
91-
violationOf( NIP.class ).withProperty( "nip" )
92-
);
93-
assertThat( validator.validate( new Person( "3497264639" ) ) )
49+
@Test(dataProvider = "invalidNips")
50+
public void testIncorrectNipNumber(String nip) {
51+
assertThat( validator.validate( new Person( nip ) ) )
9452
.containsOnlyViolations(
9553
violationOf( NIP.class ).withProperty( "nip" )
9654
);
9755
}
9856

57+
@DataProvider(name = "validNips")
58+
private static Object[][] validNips() {
59+
return new Object[][] {
60+
{ "5931423811" },
61+
{ "2596048500" },
62+
{ "4163450312" },
63+
{ "1786052059" },
64+
{ "6660057854" },
65+
{ "4219220786" },
66+
{ "3497264632" }
67+
};
68+
}
69+
70+
@DataProvider(name = "invalidNips")
71+
private static Object[][] invalidNips() {
72+
return new Object[][] {
73+
{ "123-456-78-14" },
74+
{ "123-45-67-812" },
75+
{ "123-456-32-12" },
76+
{ "5931423812" },
77+
{ "2596048505" },
78+
{ "4163450311" },
79+
{ "1786052053" },
80+
{ "6660057852" },
81+
{ "4219220785" },
82+
{ "3497264639" },
83+
{ "4062321040" },
84+
{ "7985097620" },
85+
{ "8808817210" }
86+
};
87+
}
88+
9989
public static class Person {
10090

10191
@NIP

engine/src/test/java/org/hibernate/validator/test/constraints/annotations/hv/pl/REGONValidatorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void testCorrectRegon9Number() {
5959
assertNoViolations( validator.validate( new Company( "737024234" ) ) );
6060
assertNoViolations( validator.validate( new Company( "074635672" ) ) );
6161
assertNoViolations( validator.validate( new Company( "593908869" ) ) );
62+
assertNoViolations( validator.validate( new Company( "501827370" ) ) );
6263
}
6364

6465
@Test

0 commit comments

Comments
 (0)