-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bau: Move AllowedString constraint to pay-java-commons (#936)
Moving from pay-connector as this validator would be useful for other java projects.
- Loading branch information
1 parent
89cdb51
commit 609ff79
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
model/src/main/java/uk/gov/service/payments/commons/api/validation/AllowedStrings.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 uk.gov.service.payments.commons.api.validation; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({ FIELD }) | ||
@Retention(RUNTIME) | ||
@Constraint(validatedBy = AllowedStringsValidator.class) | ||
@Documented | ||
public @interface AllowedStrings { | ||
|
||
String message(); | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
|
||
String[] allowed(); | ||
|
||
@Target({ FIELD }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@interface List { | ||
|
||
AllowedStrings[] value(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...src/main/java/uk/gov/service/payments/commons/api/validation/AllowedStringsValidator.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 uk.gov.service.payments.commons.api.validation; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import java.util.Set; | ||
|
||
public class AllowedStringsValidator implements ConstraintValidator<AllowedStrings, String> { | ||
|
||
private Set<String> allowedStrings; | ||
|
||
@Override | ||
public void initialize(AllowedStrings parameters) { | ||
allowedStrings = Set.of(parameters.allowed()); | ||
} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if (value == null) return false; | ||
return allowedStrings.contains(value); | ||
} | ||
} |