-
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.
Add generic custom constraint using SpEL expression
- Loading branch information
Mihael Cacko
committed
Oct 2, 2023
1 parent
56b87e5
commit 4544d65
Showing
12 changed files
with
280 additions
and
2 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
71 changes: 71 additions & 0 deletions
71
...validation-api/src/main/java/net/croz/nrich/validation/api/constraint/SpelExpression.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,71 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package net.croz.nrich.validation.api.constraint; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.CONSTRUCTOR; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.ElementType.TYPE_USE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
|
||
/** | ||
* The annotated element is validated against a provided SpEL expression | ||
*/ | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@Repeatable(SpelExpression.List.class) | ||
@Documented | ||
@Constraint(validatedBy = {}) | ||
public @interface SpelExpression { | ||
|
||
String message() default "{nrich.constraint.spelExpression.invalid.message}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
/** | ||
* SpEL expression that is evaluated | ||
* | ||
* @return SpEL expression | ||
*/ | ||
String value(); | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
/** | ||
* Defines several {@link SpelExpression} annotations on the same element. | ||
* | ||
* @see SpelExpression | ||
*/ | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@interface List { | ||
|
||
SpelExpression[] value(); | ||
} | ||
} |
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
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
64 changes: 64 additions & 0 deletions
64
...src/main/java/net/croz/nrich/validation/constraint/validator/SpelExpressionValidator.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,64 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package net.croz.nrich.validation.constraint.validator; | ||
|
||
import net.croz.nrich.validation.api.constraint.SpelExpression; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.expression.BeanFactoryResolver; | ||
import org.springframework.expression.Expression; | ||
import org.springframework.expression.ExpressionParser; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
import org.springframework.expression.spel.support.StandardEvaluationContext; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
public class SpelExpressionValidator implements ConstraintValidator<SpelExpression, Object> { | ||
|
||
private String spelExpression; | ||
|
||
private ExpressionParser expressionParser; | ||
|
||
private StandardEvaluationContext evaluationContext; | ||
|
||
private ApplicationContext applicationContext; | ||
|
||
public SpelExpressionValidator(ApplicationContext applicationContext) { | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
@Override | ||
public void initialize(SpelExpression constraintAnnotation) { | ||
spelExpression = constraintAnnotation.value(); | ||
expressionParser = new SpelExpressionParser(); | ||
evaluationContext = new StandardEvaluationContext(); | ||
evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext)); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Object value, ConstraintValidatorContext context) { | ||
// will be validated by other constraints | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
Expression expression = expressionParser.parseExpression(spelExpression); | ||
|
||
return expression.getValue(evaluationContext, value, Boolean.class); | ||
} | ||
} |
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
...on/src/test/java/net/croz/nrich/validation/constraint/stub/SpelExpressionTestRequest.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 @@ | ||
/* | ||
* Copyright 2020-2023 CROZ d.o.o, the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package net.croz.nrich.validation.constraint.stub; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import net.croz.nrich.validation.api.constraint.SpelExpression; | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
public class SpelExpressionTestRequest { | ||
|
||
@SpelExpression("@spelValidationTestService.validateUuid(#this)") | ||
private final String uuid; | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...on/src/test/java/net/croz/nrich/validation/constraint/stub/SpelValidationTestService.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,14 @@ | ||
package net.croz.nrich.validation.constraint.stub; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class SpelValidationTestService { | ||
|
||
private static final Pattern PATTERN = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"); | ||
|
||
public boolean validateUuid(String uuid) { | ||
Matcher matcher = PATTERN.matcher(uuid); | ||
return matcher.find(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...test/java/net/croz/nrich/validation/constraint/validator/SpelExpressionValidatorTest.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,56 @@ | ||
package net.croz.nrich.validation.constraint.validator; | ||
|
||
import net.croz.nrich.validation.ValidationTestConfiguration; | ||
import net.croz.nrich.validation.constraint.stub.SpelExpressionTestRequest; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.Validator; | ||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringJUnitConfig(ValidationTestConfiguration.class) | ||
public class SpelExpressionValidatorTest { | ||
|
||
@Autowired | ||
private Validator validator; | ||
|
||
@Test | ||
void shouldNotReportErrorForNullValue() { | ||
// given | ||
SpelExpressionTestRequest request = new SpelExpressionTestRequest(null); | ||
|
||
// when | ||
Set<ConstraintViolation<SpelExpressionTestRequest>> constraintViolationList = validator.validate(request); | ||
|
||
// then | ||
assertThat(constraintViolationList).isEmpty(); | ||
} | ||
|
||
@Test | ||
void shouldNotReportErrorWhenValueIsValid() { | ||
// given | ||
SpelExpressionTestRequest request = new SpelExpressionTestRequest("4adf9bf9-2656-468b-880a-706ff704e6b4"); | ||
|
||
// when | ||
Set<ConstraintViolation<SpelExpressionTestRequest>> constraintViolationList = validator.validate(request); | ||
|
||
// then | ||
assertThat(constraintViolationList).isEmpty(); | ||
} | ||
|
||
@Test | ||
void shouldReportErrorWhenValueIsNotValid() { | ||
// given | ||
SpelExpressionTestRequest request = new SpelExpressionTestRequest("4adf9bf9-2656-xxxx-xxxx-706ff704e6b4"); | ||
|
||
// when | ||
Set<ConstraintViolation<SpelExpressionTestRequest>> constraintViolationList = validator.validate(request); | ||
|
||
// then | ||
assertThat(constraintViolationList).isNotEmpty(); | ||
} | ||
} |