-
Notifications
You must be signed in to change notification settings - Fork 738
Add support for Bean Validation's constraint groups #887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
type: enhancement
Enhancement that adds a new feature
Comments
There's no support for constraint groups at the moment. |
After reading the source code of the library and some tests, I found a possible solution using a custom public class ConstrainedFields {
private final ConstraintDescriptions constraints;
private final List<Class<?>> groups; // Constraints groups to exclude
public ConstrainedFields(Class<?> clazz) {
groups = new ArrayList<>();
constraints = new ConstraintDescriptions(clazz, resolver(groups));
}
public FieldDescriptor pathExcludingGroups(String path, Class<?>... excludedGroups) {
groups.addAll(List.of(excludedGroups));
return path(path);
}
public FieldDescriptor path(String path) {
var description = constraints.descriptionsForProperty(path).stream()
.filter(s -> !s.isEmpty())
.collect(joining(". "));
return fieldWithPath(path).attributes(key("constraints").value(description));
}
private static ResourceBundleConstraintDescriptionResolver resolver(List<Class<?>> groups) {
return new ResourceBundleConstraintDescriptionResolver() {
@Override
public String resolveDescription(Constraint constraint) {
var description = super.resolveDescription(constraint);
if (!groups.isEmpty()) {
var constraintGroups = (Class<?>[]) constraint.getConfiguration().get("groups");
if (List.of(constraintGroups).containsAll(groups)) {
description = "";
groups.clear();
}
}
return description;
}
};
}
} |
nosan
added a commit
to nosan/spring-restdocs
that referenced
this issue
Sep 17, 2024
nosan
added a commit
to nosan/spring-restdocs
that referenced
this issue
Sep 17, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The docs gives an example of documenting constraints:
This way, you get all constraints descriptions for one property, but when using validation groups, how to get only the constraints descriptions for the given group? For example:
The text was updated successfully, but these errors were encountered: