Skip to content

Commit

Permalink
OGRN: Implement constraint validator
Browse files Browse the repository at this point in the history
  • Loading branch information
valery1707 committed Jul 24, 2017
1 parent 7a651e7 commit 43210fd
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/main/java/name/valery1707/validator/russian/ogrn/Ogrn.java
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;
}
}
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;
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/ValidationMessages.properties
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
1 change: 1 addition & 0 deletions src/main/resources/ValidationMessages_ru_RU.properties
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

0 comments on commit 43210fd

Please sign in to comment.