Skip to content

Commit 2298fab

Browse files
authored
Merge pull request #70 from SentryMan/doc
Start on Javadocs
2 parents e04978a + 6a9f257 commit 2298fab

20 files changed

+217
-88
lines changed

validator-inject-plugin/src/main/java/io/avaje/validation/inject/aspect/AOPMethodValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class AOPMethodValidator implements AspectProvider<ValidateMethod> {
1919
private final Map<Method, MethodAdapterProvider> paramAdapters;
2020

2121
public AOPMethodValidator(Validator validator, List<MethodAdapterProvider> adapterProviders) {
22-
this.ctx = (ValidationContext) validator;
22+
this.ctx = validator.getContext();
2323
this.paramAdapters =
2424
adapterProviders.stream().collect(toMap(MethodAdapterProvider::provide, p -> p));
2525
}

validator/src/main/java/io/avaje/validation/ImportValidPojo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.avaje.validation;
22

33
/**
4-
* Specify types to generate Valid Adapters for. Use if you can't place a @Valid annotation on an
5-
* external type.
4+
* Specify external types for which to generate Valid Adapters. Use when you can't place a @Valid
5+
* annotation on an external type (such as a mvn/gradle dependency).
66
*/
77
public @interface ImportValidPojo {
88

validator/src/main/java/io/avaje/validation/Validator.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public interface Validator {
3131
void validate(Object any, @Nullable Locale locale, @Nullable Class<?>... groups)
3232
throws ConstraintViolationException;
3333

34+
/** Get the validation context used to create adapters */
35+
ValidationContext getContext();
36+
3437
static Builder builder() {
3538
final var bootstrapService = ServiceLoader.load(Bootstrap.class).iterator();
3639
if (bootstrapService.hasNext()) {
@@ -48,17 +51,13 @@ interface Builder {
4851
/** Add a AnnotationValidationAdapter to use for the given type. */
4952
<T> Builder add(Class<? extends Annotation> type, ValidationAdapter<T> adapter);
5053

51-
/**
52-
* Lookup ResourceBundles with the given name and for error message interpolation
53-
*
54-
* @param bundleName the name of the bundleFiles
55-
*/
54+
/** Lookup ResourceBundles with the given names for error message interpolation */
5655
Builder addResourceBundles(String... bundleName);
5756

58-
/** Add ResourceBundle for error message interpolation */
57+
/** Add ResourceBundles for error message interpolation */
5958
Builder addResourceBundles(ResourceBundle... bundle);
6059

61-
/** Set Default Locale for this validator, if not set, will use Locale.getDefault() */
60+
/** Set Default Locale for this validator. If not set, will use Locale.getDefault() */
6261
Builder setDefaultLocale(Locale defaultLocale);
6362

6463
/** Adds additional Locales for this validator */
@@ -74,15 +73,17 @@ interface Builder {
7473
Builder temporalTolerance(Duration temporalTolerance);
7574

7675
/**
77-
* En- or disables the fail fast mode. When fail fast is enabled the validation will stop on the
76+
* Enable/Disable fail fast mode. When fail fast is enabled the validation will stop on the
7877
* first constraint violation detected.
7978
*/
8079
Builder failFast(boolean failFast);
8180

8281
/** Add a AdapterBuilder which provides a ValidationAdapter to use for the given type. */
8382
Builder add(Type type, AdapterBuilder builder);
8483

85-
/** Add a AdapterBuilder which provides a ValidationAdapter to use for the given type. */
84+
/**
85+
* Add a AdapterBuilder which provides a Annotation ValidationAdapter to use for the given type.
86+
*/
8687
Builder add(Class<? extends Annotation> type, AnnotationAdapterBuilder builder);
8788

8889
/** Add a Component which can provide multiple ValidationAdapters and or configuration. */
@@ -100,7 +101,7 @@ interface Builder {
100101
Validator build();
101102
}
102103

103-
/** Function to build a ValidationAdapter that needs Validator. */
104+
/** Function to build a ValidationAdapter from a Validation Context */
104105
@FunctionalInterface
105106
interface AdapterBuilder {
106107

validator/src/main/java/io/avaje/validation/adapter/AbstractContainerAdapter.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
package io.avaje.validation.adapter;
2-
2+
/**
3+
* Adapter that validates container types.
4+
*
5+
* @param <T> the type we are validating
6+
*/
37
public abstract class AbstractContainerAdapter<T> implements ValidationAdapter<T> {
48

5-
protected final ValidationAdapter<T> starterAdapter;
6-
private ValidationAdapter<Object> adapters;
9+
protected final ValidationAdapter<T> initalAdapter;
10+
11+
private ValidationAdapter<Object> multiAdapter;
712

8-
protected AbstractContainerAdapter(ValidationAdapter<T> starterAdapter) {
9-
this.starterAdapter = starterAdapter;
13+
/** @param initialAdapter initial adapter that can be used to validate the container itself */
14+
protected AbstractContainerAdapter(ValidationAdapter<T> initalAdapter) {
15+
this.initalAdapter = initalAdapter;
1016
}
1117

18+
/**
19+
* Compose the given adapter with the multiAdapter of this AbstractContainerAdapter for validating
20+
* multiple items.
21+
*/
1222
public AbstractContainerAdapter<T> andThenMulti(ValidationAdapter<?> adapter) {
13-
this.adapters =
14-
this.adapters != null
15-
? adapters.andThen((ValidationAdapter<Object>) adapter)
23+
this.multiAdapter =
24+
this.multiAdapter != null
25+
? multiAdapter.andThen((ValidationAdapter<Object>) adapter)
1626
: (ValidationAdapter<Object>) adapter;
1727
return this;
1828
}
1929

30+
/** Execute validations for all items in the given iterable */
2031
protected boolean validateAll(
2132
Iterable<Object> value, ValidationRequest req, String propertyName) {
2233
if (value == null) {
@@ -28,14 +39,15 @@ protected boolean validateAll(
2839
int index = -1;
2940
for (final var element : value) {
3041
index++;
31-
adapters.validate(element, req, String.valueOf(index));
42+
multiAdapter.validate(element, req, String.valueOf(index));
3243
}
3344
if (propertyName != null) {
3445
req.popPath();
3546
}
3647
return true;
3748
}
3849

50+
/** Execute validations for all items in the given array */
3951
protected boolean validateArray(Object[] value, ValidationRequest req, String propertyName) {
4052
if (value == null) {
4153
return true;
@@ -46,7 +58,7 @@ protected boolean validateArray(Object[] value, ValidationRequest req, String pr
4658
int index = -1;
4759
for (final Object element : value) {
4860
index++;
49-
adapters.validate(element, req, String.valueOf(index));
61+
multiAdapter.validate(element, req, String.valueOf(index));
5062
}
5163
if (propertyName != null) {
5264
req.popPath();

validator/src/main/java/io/avaje/validation/adapter/ArrayValidationAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ArrayValidationAdapter<T> extends AbstractContainerAdapter<T> {
88

99
@Override
1010
public boolean validate(T value, ValidationRequest req, String propertyName) {
11-
if (starterAdapter.validate(value, req, propertyName)) {
11+
if (initalAdapter.validate(value, req, propertyName)) {
1212

1313
return validateArray((Object[]) value, req, propertyName);
1414
}

validator/src/main/java/io/avaje/validation/adapter/IterableValidationAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class IterableValidationAdapter<T> extends AbstractContainerAdapter<T> {
99
@Override
1010
@SuppressWarnings("unchecked")
1111
public boolean validate(T value, ValidationRequest req, String propertyName) {
12-
if (starterAdapter.validate(value, req, propertyName)) {
12+
if (initalAdapter.validate(value, req, propertyName)) {
1313
return validateAll((Iterable<Object>) value, req, propertyName);
1414
}
1515

validator/src/main/java/io/avaje/validation/adapter/MapValidationAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MapValidationAdapter<T> extends AbstractContainerAdapter<T> {
1616
public boolean validate(T value, ValidationRequest req, String propertyName) {
1717
final var map = (Map<Object, Object>) value;
1818

19-
if (starterAdapter.validate(value, req, propertyName)) {
19+
if (initalAdapter.validate(value, req, propertyName)) {
2020
if (keys) {
2121
return validateAll(map.keySet(), req, propertyName);
2222
}

validator/src/main/java/io/avaje/validation/adapter/OptionalValidationAdapter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public boolean validate(T value, ValidationRequest req, String propertyName) {
1717
if (value == null) {
1818
return true;
1919
} else if (value instanceof final Optional o) {
20-
o.ifPresent(v -> starterAdapter.validate((T) v, req, propertyName));
20+
o.ifPresent(v -> initalAdapter.validate((T) v, req, propertyName));
2121
} else if (value instanceof final OptionalInt i) {
22-
i.ifPresent(v -> starterAdapter.validate(((T) (Object) v), req, propertyName));
22+
i.ifPresent(v -> initalAdapter.validate(((T) (Object) v), req, propertyName));
2323
} else if (value instanceof final OptionalLong l) {
24-
l.ifPresent(v -> starterAdapter.validate(((T) (Object) v), req, propertyName));
24+
l.ifPresent(v -> initalAdapter.validate(((T) (Object) v), req, propertyName));
2525
} else if (value instanceof final OptionalDouble d) {
26-
d.ifPresent(v -> starterAdapter.validate(((T) (Object) v), req, propertyName));
26+
d.ifPresent(v -> initalAdapter.validate(((T) (Object) v), req, propertyName));
2727
}
2828
return true;
2929
}

validator/src/main/java/io/avaje/validation/adapter/ValidationAdapter.java

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,90 @@
22

33
import java.util.Objects;
44
import java.util.Set;
5-
6-
@FunctionalInterface
5+
/**
6+
* This interface defines a set of validation methods for validating a value based on specific
7+
* rules. The methods in this interface allow for executing validations, composing validation
8+
* adapters, and checking validation groups.
9+
*
10+
* @param <T> The type of value to be validated
11+
*/
712
public interface ValidationAdapter<T> {
813

9-
/** Return true if validation should recurse */
14+
/**
15+
* Execute validations for the given value.
16+
*
17+
* @param value The value to be validated
18+
* @param req The validation request containing group/locale/violation information
19+
* @param propertyName The name of the property being validated
20+
* @return {@code true} if validation should continue, {@code false} otherwise
21+
*/
1022
boolean validate(T value, ValidationRequest req, String propertyName);
1123

24+
/**
25+
* Execute validations for the given value
26+
*
27+
* @param value The value to be validated
28+
* @param req The validation request containing group/locale/violation information
29+
* @return {@code true} if validation should continue, {@code false} otherwise
30+
*/
1231
default boolean validate(T value, ValidationRequest req) {
1332
return validate(value, req, null);
1433
}
1534

35+
/**
36+
* Create an adapter for validating a list of values.
37+
*
38+
* @return The adapter for list validation
39+
*/
1640
default AbstractContainerAdapter<T> list() {
17-
1841
return new IterableValidationAdapter<>(this);
1942
}
2043

44+
/**
45+
* Create an adapter for validating map keys.
46+
*
47+
* @return The adapter for map key validation
48+
*/
2149
default AbstractContainerAdapter<T> mapKeys() {
2250
return new MapValidationAdapter<>(this, true);
2351
}
2452

53+
/**
54+
* Create an adapter for validating map values.
55+
*
56+
* @return The adapter for map value validation
57+
*/
2558
default AbstractContainerAdapter<T> mapValues() {
2659
return new MapValidationAdapter<>(this, false);
2760
}
2861

62+
/**
63+
* Create an adapter for validating an array.
64+
*
65+
* @return The adapter for array validation
66+
*/
2967
default AbstractContainerAdapter<T> array() {
3068
return new ArrayValidationAdapter<>(this);
3169
}
3270

71+
/**
72+
* Create an adapter for validating an optional value.
73+
*
74+
* @return The adapter for optional value validation
75+
*/
3376
default AbstractContainerAdapter<T> optional() {
3477
return new OptionalValidationAdapter<>(this);
3578
}
3679

80+
/**
81+
* Compose this validation adapter with another adapter by applying the validations in sequence.
82+
*
83+
* @param after The validation adapter to be applied after this adapter
84+
* @return The composed validation adapter
85+
* @throws NullPointerException if {@code after} is null
86+
*/
3787
default ValidationAdapter<T> andThen(ValidationAdapter<? super T> after) {
38-
Objects.requireNonNull(after);
88+
Objects.requireNonNull(after, "after cannot be null");
3989
return (value, req, propertyName) -> {
4090
if (validate(value, req, propertyName)) {
4191
return after.validate(value, req, propertyName);
@@ -45,8 +95,13 @@ default ValidationAdapter<T> andThen(ValidationAdapter<? super T> after) {
4595
}
4696

4797
/**
48-
* Returns true if the validation request groups is empty or matches any of the adapter's
49-
* configured groups
98+
* Check if the validation request groups are empty or match any of the adapter's configured
99+
* groups.
100+
*
101+
* @param adapterGroups The groups configured for this adapter
102+
* @param request The validation request containing the groups to be checked
103+
* @return {@code true} if the groups match or if the validation request groups are empty, {@code
104+
* false} otherwise
50105
*/
51106
default boolean checkGroups(Set<Class<?>> adapterGroups, ValidationRequest request) {
52107
final var requestGroups = request.groups();

0 commit comments

Comments
 (0)