-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OGRN: Implement constraint validator
- Loading branch information
1 parent
7a651e7
commit 43210fd
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/name/valery1707/validator/russian/ogrn/Ogrn.java
This file contains 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,40 @@ | ||
package name.valery1707.validator.russian.ogrn; | ||
|
||
import javax.validation.Payload; | ||
|
||
public @interface Ogrn { | ||
/** | ||
* @return the error message template | ||
*/ | ||
String message() default "{name.valery1707.validator.russian.ogrn.Ogrn.message}"; | ||
|
||
/** | ||
* @return the check mode | ||
*/ | ||
CheckMode mode() default CheckMode.ANY; | ||
|
||
/** | ||
* @return the groups the constraint belongs to | ||
*/ | ||
Class<?>[] groups() default {}; | ||
|
||
/** | ||
* @return the payload associated to the constraint | ||
*/ | ||
Class<? extends Payload>[] payload() default {}; | ||
|
||
enum CheckMode { | ||
/** | ||
* Support any variant | ||
*/ | ||
ANY, | ||
/** | ||
* Support only juridical values | ||
*/ | ||
JURIDICAL, | ||
/** | ||
* Support only individual values | ||
*/ | ||
INDIVIDUAL; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/name/valery1707/validator/russian/ogrn/OgrnConstraintValidator.java
This file contains 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,29 @@ | ||
package name.valery1707.validator.russian.ogrn; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class OgrnConstraintValidator implements ConstraintValidator<Ogrn, CharSequence> { | ||
private Ogrn.CheckMode mode; | ||
|
||
@Override | ||
public void initialize(Ogrn constraintAnnotation) { | ||
mode = constraintAnnotation.mode(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(CharSequence value, ConstraintValidatorContext context) { | ||
return value == null || value.length() == 0 || (OgrnValidator.isValid(value).isValid() && isValidType(value, mode)); | ||
} | ||
|
||
private boolean isValidType(@Nonnull CharSequence value, @Nonnull Ogrn.CheckMode mode) { | ||
if (mode.equals(Ogrn.CheckMode.JURIDICAL)) { | ||
return value.length() == OgrnValidator.LEN_JURIDICAL; | ||
} else if (mode.equals(Ogrn.CheckMode.INDIVIDUAL)) { | ||
return value.length() == OgrnValidator.LEN_INDIVIDUAL; | ||
} else { | ||
return true; | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
name.valery1707.validator.russian.inn.Inn.message=INN must be correct | ||
name.valery1707.validator.russian.kpp.Kpp.message=KPP must be correct | ||
name.valery1707.validator.russian.ogrn.Ogrn.message=OGRN must be correct |
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
name.valery1707.validator.russian.inn.Inn.message=\u0418\u041D\u041D \u0434\u043E\u043B\u0436\u0435\u043D \u0431\u044B\u0442\u044C \u043A\u043E\u0440\u0440\u0435\u043A\u0442\u043D\u044B\u043C | ||
name.valery1707.validator.russian.kpp.Kpp.message=\u041A\u041F\u041F \u0434\u043E\u043B\u0436\u0435\u043D \u0431\u044B\u0442\u044C \u043A\u043E\u0440\u0440\u0435\u043A\u0442\u043D\u044B\u043C | ||
name.valery1707.validator.russian.ogrn.Ogrn.message=\u041E\u0413\u0420\u041D/\u041E\u0413\u0420\u041D\u0418\u041F \u0434\u043E\u043B\u0436\u0435\u043D \u0431\u044B\u0442\u044C \u043A\u043E\u0440\u0440\u0435\u043A\u0442\u043D\u044B\u043C |