-
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.
Merge pull request #248 from xenit-eu/allowed-values-validator
Add AllowedValues constraint annotation [ACC-1466]
- Loading branch information
Showing
18 changed files
with
312 additions
and
33 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
42 changes: 42 additions & 0 deletions
42
...a/com/contentgrid/spring/data/rest/hal/forms/HalFormsAttributeFieldOptionsCustomizer.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,42 @@ | ||
package com.contentgrid.spring.data.rest.hal.forms; | ||
|
||
import com.contentgrid.spring.data.rest.mapping.Container; | ||
import com.contentgrid.spring.data.rest.mapping.DomainTypeMapping; | ||
import com.contentgrid.spring.data.rest.validation.AllowedValues; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.hateoas.mediatype.MediaTypeConfigurationCustomizer; | ||
import org.springframework.hateoas.mediatype.hal.forms.HalFormsConfiguration; | ||
import org.springframework.hateoas.mediatype.hal.forms.HalFormsOptions; | ||
|
||
@RequiredArgsConstructor | ||
public class HalFormsAttributeFieldOptionsCustomizer implements MediaTypeConfigurationCustomizer<HalFormsConfiguration> { | ||
|
||
@NonNull | ||
private final DomainTypeMapping domainTypeMapping; | ||
|
||
@Override | ||
public HalFormsConfiguration customize(HalFormsConfiguration configuration) { | ||
for (Class<?> domainType : domainTypeMapping) { | ||
var container = domainTypeMapping.forDomainType(domainType); | ||
configuration = customizeConfiguration(configuration, domainType, container); | ||
} | ||
return configuration; | ||
} | ||
|
||
private HalFormsConfiguration customizeConfiguration(HalFormsConfiguration configuration, Class<?> domainType, | ||
Container container) { | ||
var configAtomic = new AtomicReference<>(configuration); | ||
container.doWithProperties(property -> { | ||
property.findAnnotation(AllowedValues.class) | ||
.map(AllowedValues::value) | ||
.ifPresent(options -> configAtomic.updateAndGet(config -> { | ||
return config.withOptions(domainType, property.getName(), metadata -> { | ||
return HalFormsOptions.inline(options); | ||
}); | ||
})); | ||
}); | ||
return configAtomic.get(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ng-data-rest/src/main/java/com/contentgrid/spring/data/rest/validation/AllowedValues.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,46 @@ | ||
package com.contentgrid.spring.data.rest.validation; | ||
|
||
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; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* The annotated element must have one of the allowed options as value. | ||
*/ | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@Constraint(validatedBy = {AllowedValuesValidator.class}) | ||
public @interface AllowedValues { | ||
|
||
/** | ||
* @return the allowed options of the constraint | ||
*/ | ||
String[] value(); | ||
|
||
/** | ||
* @return the error message template | ||
*/ | ||
String message() default "{com.contentgrid.spring.data.rest.validation.AllowedValues.message}"; | ||
|
||
/** | ||
* @return the groups the constraint belongs to | ||
*/ | ||
Class<?>[] groups() default {}; | ||
|
||
/** | ||
* @return the payload associated to the constraint | ||
*/ | ||
Class<? extends Payload>[] payload() default {}; | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...est/src/main/java/com/contentgrid/spring/data/rest/validation/AllowedValuesValidator.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 com.contentgrid.spring.data.rest.validation; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import java.util.Set; | ||
|
||
public class AllowedValuesValidator implements ConstraintValidator<AllowedValues, String> { | ||
|
||
private Set<String> options; | ||
|
||
@Override | ||
public void initialize(AllowedValues constraintAnnotation) { | ||
this.options = Set.of(constraintAnnotation.value()); | ||
} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if (value == null) { | ||
// Null values are checked by other constraints | ||
return true; | ||
} | ||
return options.contains(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
1 change: 1 addition & 0 deletions
1
contentgrid-spring-data-rest/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 |
---|---|---|
@@ -0,0 +1 @@ | ||
com.contentgrid.spring.data.rest.validation.AllowedValues.message= must be one of {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
Oops, something went wrong.