-
Notifications
You must be signed in to change notification settings - Fork 583
HV-1970 Add Korean specific RRN annotation #1338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
bafee67
HV-1970 Adding KorRRN annotation
ing9990 e4d3227
HV-1970 Adding KorRRN validator tests
ing9990 123f252
HV-1970 Refactoring to the Hibernate Code Style
ing9990 a672e04
HV-1970 Remove type field
ing9990 98a5164
HV-1970 Encoding Javadoc url
ing9990 ec01653
HV-1970 Adding RRN validation criteria and modifying test code
ing9990 883f3ef
HV-1970 Encoding javadoc url
ing9990 103eac2
HV-1970 Adding test cases and refactoring the RRNValidator
ing9990 00ddc91
HV-1970 fix error
ing9990 a3f45bc
HV-1970 Add attribute to optionally validate check-digit, adding and …
ing9990 f60784f
HV-1970 Remove private signature for Java8 version
ing9990 d0962dc
HV-1970 Change 'if' statement to 'switch' statement for Readability,…
ing9990 5f1337b
HV-1970 Change 'switch' statement to 'if' statement for Java 11
ing9990 f8e4c09
HV-1970 Change attribute name from BEFORE_OCTOBER_2020_ONLY to ALWAYS
ing9990 c625080
HV-1970 Adding test case for default value of validateCheckDigit()
ing9990 9566047
HV-1970 Fix typos
ing9990 7962786
HV-1970 Refactor: Moving validation methods to RRNValidationAlgorithm…
ing9990 b8951fb
HV-1970 Fix typo
ing9990 c589eb0
HV-1970 Adding asciidoc for KorRRN Annotation
ing9990 974c616
HV-1970 Modifying test case to pass the test
ing9990 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
engine/src/main/java/org/hibernate/validator/cfg/defs/kor/KorRRNDef.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.cfg.defs.kor; | ||
|
||
import org.hibernate.validator.cfg.ConstraintDef; | ||
import org.hibernate.validator.constraints.kor.KorRRN; | ||
|
||
public class KorRRNDef extends ConstraintDef<KorRRNDef, KorRRN> { | ||
|
||
public KorRRNDef() { | ||
super( KorRRN.class ); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
engine/src/main/java/org/hibernate/validator/cfg/defs/kor/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
|
||
/** | ||
* <p>Korean specific constraint definition classes for programmatic constraint definition API.</p> | ||
* <p>This package is part of the public Hibernate Validator API.</p> | ||
*/ | ||
package org.hibernate.validator.cfg.defs.kor; |
42 changes: 42 additions & 0 deletions
42
engine/src/main/java/org/hibernate/validator/constraints/kor/KorRRN.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.constraints.kor; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.CONSTRUCTOR; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.ElementType.TYPE_USE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Checks that the annotated character sequence is a valid Korean resident registration number. | ||
* | ||
* @author Taewoo Kim | ||
* @see <a href="https://ko.wikipedia.org/wiki/%EC%A3%BC%EB%AF%BC%EB%93%B1%EB%A1%9D%EB%B2%88%ED%98%B8">Korean resident registration number</a> | ||
*/ | ||
|
||
@Documented | ||
@Constraint(validatedBy = {}) | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
public @interface KorRRN { | ||
|
||
String message() default "{org.hibernate.validator.constraints.kor.KorRRN.message}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
60 changes: 60 additions & 0 deletions
60
...in/java/org/hibernate/validator/internal/constraintvalidators/hv/kor/KorRRNValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.internal.constraintvalidators.hv.kor; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
import org.hibernate.validator.constraints.kor.KorRRN; | ||
|
||
public class KorRRNValidator implements ConstraintValidator<KorRRN, CharSequence> { | ||
|
||
private static final Pattern REGEX = Pattern.compile( "\\d{6}-\\d{7}" ); | ||
|
||
private static final int CHECK_DIGIT_INDEX = 13; | ||
private static final int[] CHECK_DIGIT_ARRAY = { 2, 3, 4, 5, 6, 7, -1, 8, 9, 2, 3, 4, 5 }; | ||
|
||
@Override | ||
public boolean isValid(CharSequence rrnValue, ConstraintValidatorContext context) { | ||
if ( rrnValue == null ) { | ||
return true; | ||
} | ||
String rrn = rrnValue.toString(); | ||
|
||
if ( !isValidFormat( rrn ) ) { | ||
return false; | ||
} | ||
|
||
int month = Integer.parseInt( rrn.substring( 2, 4 ) ); | ||
int day = Integer.parseInt( rrn.substring( 4, 6 ) ); | ||
|
||
if ( day > 31 || ( day > 30 && ( month == 4 || month == 6 || month == 9 || month == 11 ) ) || ( day > 28 && month == 2 ) || rrn.length() != 14 ) { | ||
return false; | ||
} | ||
|
||
return validateCheckSum( rrn.toCharArray(), rrnValue.charAt( CHECK_DIGIT_INDEX ) ); | ||
} | ||
|
||
private static boolean isValidFormat(String rrn) { | ||
return REGEX.matcher( rrn ).matches(); | ||
} | ||
|
||
private boolean validateCheckSum(char[] rrnArray, char code) { | ||
int sum = 0; | ||
for ( int i = 0; i < CHECK_DIGIT_ARRAY.length; i++ ) { | ||
if ( i == 6 ) { | ||
continue; | ||
} | ||
sum += CHECK_DIGIT_ARRAY[i] * Character.getNumericValue( rrnArray[i] ); | ||
} | ||
|
||
return ( 11 - sum % 11 ) % 10 == Character.getNumericValue( code ); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...java/org/hibernate/validator/test/constraints/annotations/hv/kor/KorRRNValidatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.test.constraints.annotations.hv.kor; | ||
|
||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertNoViolations; | ||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertThat; | ||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf; | ||
|
||
import jakarta.validation.ConstraintViolation; | ||
|
||
import java.util.Set; | ||
|
||
import org.hibernate.validator.constraints.kor.KorRRN; | ||
import org.hibernate.validator.test.constraints.annotations.AbstractConstrainedTest; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
public class KorRRNValidatorTest extends AbstractConstrainedTest { | ||
|
||
@Test | ||
void testKorRRN() { | ||
final KorRegistrationCard korRegistrationCard = new KorRegistrationCard( "030205-3069010" ); | ||
final Set<ConstraintViolation<KorRegistrationCard>> violations = validator.validate( korRegistrationCard ); | ||
assertNoViolations( violations ); | ||
} | ||
|
||
@Test | ||
public void testKorRRNInvalid() { | ||
final KorRegistrationCard korRegistrationCard = new KorRegistrationCard( "010101-1063015" ); | ||
final Set<ConstraintViolation<KorRegistrationCard>> violations = validator.validate( korRegistrationCard ); | ||
assertThat( violations ) | ||
.containsOnlyViolations( violationOf( KorRRN.class ) | ||
.withMessage( "invalid Korean resident registration number (KorRRN)" ) ); | ||
} | ||
|
||
private static class KorRegistrationCard { | ||
|
||
@KorRRN | ||
private final String rrn; | ||
|
||
public KorRegistrationCard(String rrn) { | ||
this.rrn = rrn; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.