-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed to Java 17; Creating a custom validation
- Loading branch information
Showing
8 changed files
with
190 additions
and
43 deletions.
There are no files selected for viewing
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
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
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
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
24 changes: 24 additions & 0 deletions
24
src/main/java/io/github/arrudalabs/mizudo/validation/SuportaValidacao.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,24 @@ | ||
package io.github.arrudalabs.mizudo.validation; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import javax.validation.groups.Default; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.util.function.Predicate; | ||
|
||
@Constraint(validatedBy = SuportaValidacaoValidator.class) | ||
@Target({ElementType.TYPE,ElementType.FIELD,ElementType.PARAMETER, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SuportaValidacao { | ||
String message(); | ||
|
||
Class<? extends Predicate> classeValidadora(); | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
Class<? extends Default>[] groups() default {}; | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/io/github/arrudalabs/mizudo/validation/SuportaValidacaoValidator.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,33 @@ | ||
package io.github.arrudalabs.mizudo.validation; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import java.lang.reflect.InvocationTargetException; | ||
|
||
@ApplicationScoped | ||
public class SuportaValidacaoValidator implements ConstraintValidator<SuportaValidacao, Object> { | ||
|
||
private SuportaValidacao constraintAnnotation; | ||
|
||
@Override | ||
public void initialize(SuportaValidacao constraintAnnotation) { | ||
ConstraintValidator.super.initialize(constraintAnnotation); | ||
this.constraintAnnotation = constraintAnnotation; | ||
} | ||
|
||
@Override | ||
public boolean isValid(Object value, ConstraintValidatorContext context) { | ||
try { | ||
return this.constraintAnnotation | ||
.classeValidadora() | ||
.getConstructor(new Class[0]) | ||
.newInstance(new Object[0]).test(value); | ||
} catch (IllegalAccessException | ||
| NoSuchMethodException | ||
| InstantiationException | ||
| InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
src/test/java/io/github/arrudalabs/mizudo/validation/SuportaValidacaoValidatorTest.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,44 @@ | ||
package io.github.arrudalabs.mizudo.validation; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.inject.Inject; | ||
import javax.validation.Validator; | ||
import javax.validation.constraints.NotBlank; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@QuarkusTest | ||
class SuportaValidacaoValidatorTest { | ||
|
||
@Inject | ||
Validator validator; | ||
|
||
@Test | ||
void test() { | ||
assertFalse(validator.validate(new Pojo(null,"ewrw")).isEmpty()); | ||
} | ||
|
||
|
||
@SuportaValidacao( | ||
message = "Pojo invalido - atributos diferentes", | ||
classeValidadora = Pojo.PojoValidator.class | ||
) | ||
static record Pojo(@NotBlank String atributo1, | ||
@NotBlank String atributo2) { | ||
|
||
public static class PojoValidator implements Predicate<Pojo> { | ||
|
||
@Override | ||
public boolean test(Pojo o) { | ||
return Objects.nonNull(o) | ||
&& Objects.equals(o.atributo1, o.atributo2); | ||
} | ||
} | ||
|
||
} | ||
} |