|
26 | 26 | import br.com.fluentvalidator.rule.Rule; |
27 | 27 | import br.com.fluentvalidator.transform.ValidationResultTransform; |
28 | 28 |
|
| 29 | +/** |
| 30 | + * Interface that defines the contract for fluent validators. |
| 31 | + * <p> |
| 32 | + * This interface extends {@link Rule} and provides methods for building and executing |
| 33 | + * validation rules in a fluent, chainable manner. It supports validation of single objects, |
| 34 | + * collections of objects, and provides flexible result transformation capabilities. |
| 35 | + * </p> |
| 36 | + * <p> |
| 37 | + * Implementations should define validation rules by implementing the {@link #rules()} method |
| 38 | + * and using the fluent API provided by {@link #ruleFor(Function)} and {@link #ruleForEach(Function)} |
| 39 | + * to build validation constraints. |
| 40 | + * </p> |
| 41 | + * <p> |
| 42 | + * This interface supports: |
| 43 | + * <ul> |
| 44 | + * <li>Single object validation with {@link #validate(Object)}</li> |
| 45 | + * <li>Collection validation with {@link #validate(Collection)}</li> |
| 46 | + * <li>Result transformation for custom output formats</li> |
| 47 | + * <li>Fail-fast validation mode</li> |
| 48 | + * <li>Contextual property storage and retrieval</li> |
| 49 | + * </ul> |
| 50 | + * </p> |
| 51 | + * |
| 52 | + * @param <T> the type of object being validated |
| 53 | + */ |
29 | 54 | public interface Validator<T> extends Rule<T> { |
30 | 55 |
|
31 | 56 | /** |
32 | | - * |
| 57 | + * Defines the validation rules for this validator. |
| 58 | + * <p> |
| 59 | + * Subclasses should implement this method to define validation rules using |
| 60 | + * the fluent API provided by {@link #ruleFor(Function)} and {@link #ruleForEach(Function)}. |
| 61 | + * This method is called once during initialization in a thread-safe manner. |
| 62 | + * </p> |
33 | 63 | */ |
34 | 64 | void rules(); |
35 | 65 |
|
36 | 66 | /** |
37 | | - * |
| 67 | + * Configures this validator to use fail-fast rule processing. |
| 68 | + * <p> |
| 69 | + * When fail-fast mode is enabled, validation will stop at the first rule failure |
| 70 | + * instead of continuing to evaluate all rules. This can improve performance when |
| 71 | + * early validation failure is acceptable. |
| 72 | + * </p> |
38 | 73 | */ |
39 | 74 | void failFastRule(); |
40 | 75 |
|
41 | 76 | /** |
| 77 | + * Returns the current validation counter. |
| 78 | + * <p> |
| 79 | + * The counter tracks the number of validation operations or rules that have been |
| 80 | + * processed. This can be useful for debugging, monitoring, or performance analysis. |
| 81 | + * </p> |
42 | 82 | * |
43 | | - * @return Current count element on collection |
| 83 | + * @return the current counter value, or null if no counter is available |
44 | 84 | */ |
45 | 85 | Integer getCounter(); |
46 | 86 |
|
47 | 87 | /** |
| 88 | + * Sets a property name to be used in the validation context. |
| 89 | + * <p> |
| 90 | + * This property name will be associated with the validated object in the validation |
| 91 | + * context, allowing rules to access contextual information during validation. |
| 92 | + * </p> |
48 | 93 | * |
49 | | - * @param property |
| 94 | + * @param property the property name to set in the validation context |
50 | 95 | */ |
51 | 96 | void setPropertyOnContext(final String property); |
52 | 97 |
|
53 | 98 | /** |
| 99 | + * Retrieves a property value from the validation context. |
| 100 | + * <p> |
| 101 | + * This method allows access to contextual information that was previously stored |
| 102 | + * in the validation context, enabling rules to make decisions based on broader |
| 103 | + * validation state. |
| 104 | + * </p> |
54 | 105 | * |
55 | | - * @param property |
56 | | - * @param clazz |
57 | | - * @return |
| 106 | + * @param <P> the type of the property value |
| 107 | + * @param property the name of the property to retrieve |
| 108 | + * @param clazz the class type of the property value |
| 109 | + * @return the property value cast to the specified type, or null if not found |
58 | 110 | */ |
59 | 111 | <P> P getPropertyOnContext(final String property, final Class<P> clazz); |
60 | 112 |
|
61 | 113 | /** |
| 114 | + * Validates a single instance and returns the validation result. |
| 115 | + * <p> |
| 116 | + * This method processes the given instance through all configured validation rules |
| 117 | + * and returns a comprehensive result containing any validation errors or success indicators. |
| 118 | + * </p> |
62 | 119 | * |
63 | | - * @param instance |
64 | | - * @return |
| 120 | + * @param instance the object instance to validate |
| 121 | + * @return a ValidationResult containing the outcome of validation |
65 | 122 | */ |
66 | 123 | ValidationResult validate(final T instance); |
67 | 124 |
|
68 | 125 | /** |
| 126 | + * Validates a single instance and transforms the result using the provided transformer. |
| 127 | + * <p> |
| 128 | + * This method combines validation with result transformation in a single operation, |
| 129 | + * allowing for custom result formats or processing without intermediate objects. |
| 130 | + * </p> |
69 | 131 | * |
70 | | - * @param instance |
71 | | - * @param transform |
72 | | - * @return |
| 132 | + * @param <E> the type of the transformed result |
| 133 | + * @param instance the object instance to validate |
| 134 | + * @param transform the transformer to apply to the validation result |
| 135 | + * @return the transformed validation result |
73 | 136 | */ |
74 | 137 | <E> E validate(final T instance, final ValidationResultTransform<E> transform); |
75 | 138 |
|
76 | 139 | /** |
| 140 | + * Validates a collection of instances and returns a list of validation results. |
| 141 | + * <p> |
| 142 | + * Each instance in the collection is validated independently, and the results are |
| 143 | + * collected into a list. The order of results corresponds to the order of instances |
| 144 | + * in the input collection. |
| 145 | + * </p> |
77 | 146 | * |
78 | | - * @param instances |
79 | | - * @return |
| 147 | + * @param instances the collection of instances to validate |
| 148 | + * @return a list of ValidationResult objects, one for each input instance |
80 | 149 | */ |
81 | 150 | List<ValidationResult> validate(final Collection<T> instances); |
82 | 151 |
|
83 | 152 | /** |
| 153 | + * Validates a collection of instances and transforms each result using the provided transformer. |
| 154 | + * <p> |
| 155 | + * This method combines collection validation with result transformation, applying |
| 156 | + * the transformer to each individual validation result. |
| 157 | + * </p> |
84 | 158 | * |
85 | | - * @param instances |
86 | | - * @param transform |
87 | | - * @return |
| 159 | + * @param <E> the type of the transformed results |
| 160 | + * @param instances the collection of instances to validate |
| 161 | + * @param transform the transformer to apply to each validation result |
| 162 | + * @return a list of transformed validation results |
88 | 163 | */ |
89 | 164 | <E> List<E> validate(final Collection<T> instances, final ValidationResultTransform<E> transform); |
90 | 165 |
|
91 | 166 | /** |
| 167 | + * Creates a validation rule for a specific property of the validated object. |
| 168 | + * <p> |
| 169 | + * This method starts a fluent chain for defining validation rules that apply to a |
| 170 | + * property extracted from the validated object using the provided function. The property |
| 171 | + * name will be automatically derived from the function if possible. |
| 172 | + * </p> |
92 | 173 | * |
93 | | - * @param <P> |
94 | | - * @param function |
95 | | - * @return |
| 174 | + * @param <P> the type of the property being validated |
| 175 | + * @param function a function that extracts the property value from the validated object |
| 176 | + * @return a RuleBuilderProperty for chaining additional validation constraints |
96 | 177 | */ |
97 | 178 | <P> RuleBuilderProperty<T, P> ruleFor(final Function<T, P> function); |
98 | 179 |
|
99 | 180 | /** |
| 181 | + * Creates a validation rule for a named property of the validated object. |
| 182 | + * <p> |
| 183 | + * This method starts a fluent chain for defining validation rules that apply to a |
| 184 | + * property extracted from the validated object. The field name is explicitly provided |
| 185 | + * and will be used in error messages and validation context. |
| 186 | + * </p> |
100 | 187 | * |
101 | | - * @param <P> |
102 | | - * @param fieldName |
103 | | - * @param function |
104 | | - * @return |
| 188 | + * @param <P> the type of the property being validated |
| 189 | + * @param fieldName the name of the field being validated (used in error messages) |
| 190 | + * @param function a function that extracts the property value from the validated object |
| 191 | + * @return a RuleBuilderProperty for chaining additional validation constraints |
105 | 192 | */ |
106 | 193 | <P> RuleBuilderProperty<T, P> ruleFor(final String fieldName, final Function<T, P> function); |
107 | 194 |
|
108 | 195 | /** |
| 196 | + * Creates validation rules for each element in a collection property. |
| 197 | + * <p> |
| 198 | + * This method starts a fluent chain for defining validation rules that apply to each |
| 199 | + * element of a collection extracted from the validated object. The field name will be |
| 200 | + * automatically derived from the function if possible. |
| 201 | + * </p> |
109 | 202 | * |
110 | | - * @param <P> |
111 | | - * @param function |
112 | | - * @return |
| 203 | + * @param <P> the type of elements in the collection being validated |
| 204 | + * @param function a function that extracts the collection from the validated object |
| 205 | + * @return a RuleBuilderCollection for chaining additional validation constraints |
113 | 206 | */ |
114 | 207 | <P> RuleBuilderCollection<T, P> ruleForEach(final Function<T, Collection<P>> function); |
115 | 208 |
|
116 | 209 | /** |
| 210 | + * Creates validation rules for each element in a collection property. |
| 211 | + * <p> |
| 212 | + * This method starts a fluent chain for defining validation rules that apply to each |
| 213 | + * element of a collection extracted from the validated object. The field name is |
| 214 | + * explicitly provided for error reporting. |
| 215 | + * </p> |
117 | 216 | * |
118 | | - * @param <P> |
119 | | - * @param fieldName |
120 | | - * @param function |
121 | | - * @return |
| 217 | + * @param <P> the type of elements in the collection being validated |
| 218 | + * @param fieldName the name of the collection field being validated |
| 219 | + * @param function a function that extracts the collection from the validated object |
| 220 | + * @return a RuleBuilderCollection for chaining additional validation constraints |
122 | 221 | */ |
123 | 222 | <P> RuleBuilderCollection<T, P> ruleForEach(final String fieldName, final Function<T, Collection<P>> function); |
124 | 223 | } |
0 commit comments