forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test for a constraint validator injecting config properties
- Loading branch information
1 parent
4452e0f
commit f0fffb2
Showing
7 changed files
with
85 additions
and
0 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
31 changes: 31 additions & 0 deletions
31
...java/io/quarkus/it/hibernate/validator/injection/InjectedPropertyConstraintValidator.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,31 @@ | ||
package io.quarkus.it.hibernate.validator.injection; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
@ApplicationScoped | ||
public class InjectedPropertyConstraintValidator | ||
implements ConstraintValidator<InjectedPropertyConstraintValidatorConstraint, String> { | ||
|
||
@Inject | ||
ValidationConfigProperties config; | ||
private Pattern pattern; | ||
|
||
@Override | ||
public void initialize(InjectedPropertyConstraintValidatorConstraint constraintAnnotation) { | ||
pattern = Pattern.compile(config.pattern()); | ||
} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
return pattern.matcher(value).matches(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...arkus/it/hibernate/validator/injection/InjectedPropertyConstraintValidatorConstraint.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,21 @@ | ||
package io.quarkus.it.hibernate.validator.injection; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
@Target({ ElementType.TYPE_USE, ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = { InjectedPropertyConstraintValidator.class }) | ||
public @interface InjectedPropertyConstraintValidatorConstraint { | ||
|
||
String message() default "{InjectedPropertyConstraintValidatorConstraint.message}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
8 changes: 8 additions & 0 deletions
8
...src/main/java/io/quarkus/it/hibernate/validator/injection/ValidationConfigProperties.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,8 @@ | ||
package io.quarkus.it.hibernate.validator.injection; | ||
|
||
import io.smallrye.config.ConfigMapping; | ||
|
||
@ConfigMapping(prefix = "some.app.config.validation") | ||
public interface ValidationConfigProperties { | ||
String pattern(); | ||
} |
1 change: 1 addition & 0 deletions
1
integration-tests/hibernate-validator/src/main/resources/ValidationMessages.properties
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,3 +1,4 @@ | ||
MyCustomConstraint.message = invalid MyOtherBean | ||
InjectedConstraintValidatorConstraint.message = InjectedConstraintValidatorConstraint violation | ||
pattern.message=Value is not in line with the pattern | ||
InjectedPropertyConstraintValidatorConstraint.message = InjectedPropertyConstraintValidatorConstraint violation |
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