Provide an API to ease documenting a request field’s constraints
Previously, users that wanted to document a request field’s constraints had to roll their own solution. This commit introduces a new API that makes it easier to document constraints. Support is provided for discovering Bean Validation constraints and resolving descriptions for them. The constraint descriptions can then be used as required. For example, they can included in a field’s description or in an additional constraints attribute that’s included in an additional table column via the use of a custom snippet template. Closes gh-42
This commit is contained in:
31
docs/src/test/java/com/example/Constraints.java
Normal file
31
docs/src/test/java/com/example/Constraints.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.example;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.springframework.restdocs.constraints.ConstraintDescriptions;
|
||||
|
||||
public class Constraints {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
// tag::constraints[]
|
||||
public void example() {
|
||||
ConstraintDescriptions userConstraints = new ConstraintDescriptions(UserInput.class); // <1>
|
||||
List<String> descriptions = userConstraints.descriptionsForProperty("name"); // <2>
|
||||
}
|
||||
|
||||
static class UserInput {
|
||||
|
||||
@NotNull
|
||||
@Size(min = 1)
|
||||
String name;
|
||||
|
||||
@NotNull
|
||||
@Size(min = 8)
|
||||
String password;
|
||||
}
|
||||
// end::constraints[]
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user