...
| Deck of Cards |
|---|
|
| Card |
|---|
| | Bloco de código |
|---|
| language | java |
|---|
| theme | Eclipse |
|---|
| linenumbers | true |
|---|
| public class ServiceInterface {
public void createUser(@Email String email, @NotNull String name);
} |
|
| Card |
|---|
| | Bloco de código |
|---|
| language | java |
|---|
| theme | Eclipse |
|---|
| linenumbers | true |
|---|
| @ValidateParams
public class ServiceImpl {
public void createUser(@Email String email, @NotNull String name){
//...
}
} |
|
|
Agrupamento de Validações
É possível agrupar validações em uma mesma validação personalizada, conforme exemplo abaixo, que faz várias validações em um único parâmetro.:
...
| Deck of Cards |
|---|
|
| Card |
|---|
| | Bloco de código |
|---|
| language | java |
|---|
| theme | Eclipse |
|---|
| title | @Plate |
|---|
| linenumbers | true |
|---|
| @NotNull
@NotEmpty
@NotBlank
@Pattern(regex="[A-Z]{3}\d{4}")
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
// Classe que irá fazer a validação no Detran
@Constraint(validatedBy = DetranValidator.class)
@ReportAsSingleViolation
public @interface Plate{
String message() default "{com.totvs.custom.validation.plate.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
|
|
| Card |
|---|
| | Bloco de código |
|---|
| language | java |
|---|
| theme | Eclipse |
|---|
| title | DetranValidator |
|---|
| linenumbers | true |
|---|
| public class DetranValidator implements ConstraintValidator<Plate, String> {
private CaseMode caseMode;
@Override
public void initialize(CheckCase constraintAnnotation) {
this.caseMode = constraintAnnotation.value();
}
@Override
public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
var validDetran = pesquisaDetran(); // logica de pesquisa no detran
if ( validDetran ) {
return true;
}
return false;
}
}
|
|
|