-
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.
- Loading branch information
Mihael Cacko
committed
Sep 29, 2023
1 parent
56b87e5
commit 5da861c
Showing
7 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
nrich-validation-api/src/main/java/net/croz/nrich/validation/api/constraint/Generic.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(Generic.List.class) | ||
@Documented | ||
@Constraint(validatedBy = {}) | ||
public @interface Generic { | ||
|
||
String message() default "{nrich.constraint.generic.invalid.message}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
/** | ||
* SpEL expression that is evaluated | ||
* | ||
* @return SpEL expression | ||
*/ | ||
String value(); | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
/** | ||
* Defines several {@link Generic} annotations on the same element. | ||
* | ||
* @see Generic | ||
*/ | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@interface List { | ||
|
||
Generic[] 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
51 changes: 51 additions & 0 deletions
51
...dation/src/main/java/net/croz/nrich/validation/constraint/validator/GenericValidator.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,51 @@ | ||
/* | ||
* 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.Generic; | ||
import org.springframework.expression.Expression; | ||
import org.springframework.expression.ExpressionParser; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
public class GenericValidator implements ConstraintValidator<Generic, Object> { | ||
|
||
private String spelExpression; | ||
|
||
private ExpressionParser expressionParser; | ||
|
||
@Override | ||
public void initialize(Generic constraintAnnotation) { | ||
spelExpression = constraintAnnotation.value(); | ||
expressionParser = new SpelExpressionParser(); | ||
} | ||
|
||
@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(value, Boolean.class); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...tion/src/test/java/net/croz/nrich/validation/constraint/stub/GenericValidTestRequest.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.Generic; | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
public class GenericValidTestRequest { | ||
|
||
@Generic(value = "#this matches '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'") | ||
private final String uuid; | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...on/src/test/java/net/croz/nrich/validation/constraint/validator/GenericValidatorTest.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.GenericValidTestRequest; | ||
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 GenericValidatorTest { | ||
|
||
@Autowired | ||
private Validator validator; | ||
|
||
@Test | ||
void shouldNotReportErrorForNullValue() { | ||
// given | ||
GenericValidTestRequest request = new GenericValidTestRequest(null); | ||
|
||
// when | ||
Set<ConstraintViolation<GenericValidTestRequest>> constraintViolationList = validator.validate(request); | ||
|
||
// then | ||
assertThat(constraintViolationList).isEmpty(); | ||
} | ||
|
||
@Test | ||
void shouldNotReportErrorWhenValueIsValid() { | ||
// given | ||
GenericValidTestRequest request = new GenericValidTestRequest("4adf9bf9-2656-468b-880a-706ff704e6b4"); | ||
|
||
// when | ||
Set<ConstraintViolation<GenericValidTestRequest>> constraintViolationList = validator.validate(request); | ||
|
||
// then | ||
assertThat(constraintViolationList).isEmpty(); | ||
} | ||
|
||
@Test | ||
void shouldReportErrorWhenValueIsNotValid() { | ||
// given | ||
GenericValidTestRequest request = new GenericValidTestRequest("4adf9bf9-2656-xxxx-xxxx-706ff704e6b4"); | ||
|
||
// when | ||
Set<ConstraintViolation<GenericValidTestRequest>> constraintViolationList = validator.validate(request); | ||
|
||
// then | ||
assertThat(constraintViolationList).isNotEmpty(); | ||
} | ||
} |