Skip to content

Commit f8d0869

Browse files
authored
Merge pull request #18 from avaje/feature/move-AnnotationValidatorFactory
Move AnnotationValidationAdapter.Factory -> AnnotationValidatorFactory
2 parents 82a3f12 + 1838e67 commit f8d0869

24 files changed

+431
-418
lines changed

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

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

3-
import static java.lang.annotation.ElementType.FIELD;
4-
import static java.lang.annotation.ElementType.PACKAGE;
5-
import static java.lang.annotation.ElementType.TYPE;
6-
import static java.lang.annotation.RetentionPolicy.CLASS;
7-
83
import java.lang.annotation.Retention;
94
import java.lang.annotation.Target;
105

6+
import static java.lang.annotation.ElementType.*;
7+
import static java.lang.annotation.RetentionPolicy.CLASS;
8+
119
/**
1210
* Marks a type for validation.
1311
*/

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

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

3+
import io.avaje.validation.adapter.*;
4+
import io.avaje.validation.core.DefaultBootstrap;
5+
import io.avaje.validation.spi.Bootstrap;
6+
37
import java.lang.annotation.Annotation;
48
import java.lang.reflect.Type;
59
import java.util.Iterator;
6-
import java.util.Map;
710
import java.util.ServiceLoader;
811

9-
import io.avaje.validation.adapter.AdapterBuildContext;
10-
import io.avaje.validation.adapter.AnnotationValidationAdapter;
11-
import io.avaje.validation.adapter.ValidationAdapter;
12-
import io.avaje.validation.adapter.ValidatorComponent;
13-
import io.avaje.validation.core.DefaultBootstrap;
14-
import io.avaje.validation.spi.Bootstrap;
15-
1612
public interface Validator {
1713

1814
void validate(Object any) throws ConstraintViolationException;
@@ -30,22 +26,22 @@ static Builder builder() {
3026
interface Builder {
3127

3228
/** Add a ValidationAdapter to use for the given type. */
33-
<T> Builder add(Type type, ValidationAdapter<T> jsonAdapter);
29+
<T> Builder add(Type type, ValidationAdapter<T> adapter);
3430

3531
/** Add a AnnotationValidationAdapter to use for the given type. */
36-
<T> Builder add(Class<Annotation> type, AnnotationValidationAdapter<T> jsonAdapter);
32+
<T> Builder add(Class<Annotation> type, ValidationAdapter<T> adapter);
3733

3834
/** Add a AdapterBuilder which provides a ValidationAdapter to use for the given type. */
3935
Builder add(Type type, AdapterBuilder builder);
4036

41-
/** Add a Component which can provide multiple JsonAdapters and or configuration. */
37+
/** Add a Component which can provide multiple ValidationAdapters and or configuration. */
4238
Builder add(ValidatorComponent component);
4339

44-
/** Add a ValidationAdapter.Factory which provides JsonAdapters to use. */
45-
Builder add(ValidationAdapter.Factory factory);
40+
/** Add a ValidationAdapter.Factory which provides ValidationAdapters to use. */
41+
Builder add(ValidationContext.AdapterFactory factory);
4642

47-
/** Add a ValidationAdapter.Factory which provides JsonAdapters to use. */
48-
Builder add(AnnotationValidationAdapter.Factory factory);
43+
/** Add a ValidationAdapter.Factory which provides ValidationAdapters to use. */
44+
Builder add(ValidationContext.AnnotationFactory factory);
4945

5046
/**
5147
* Build and return the Validator instance with all the given adapters and factories registered.
@@ -58,14 +54,14 @@ interface Builder {
5854
interface AdapterBuilder {
5955

6056
/** Create a ValidationAdapter given the Validator instance. */
61-
ValidationAdapter<?> build(AdapterBuildContext ctx);
57+
ValidationAdapter<?> build(ValidationContext ctx);
6258
}
6359

64-
/** Components register JsonAdapters Validator.Builder */
60+
/** Components register ValidationAdapters Validator.Builder */
6561
@FunctionalInterface
6662
interface GeneratedComponent extends ValidatorComponent {
6763

68-
/** Register JsonAdapters with the Builder. */
64+
/** Register ValidationAdapters with the Builder. */
6965
@Override
7066
void register(Builder builder);
7167
}

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

Lines changed: 0 additions & 15 deletions
This file was deleted.

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

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.avaje.validation.adapter;
2+
3+
/**
4+
*
5+
*/
6+
public enum RegexFlag {
7+
8+
/**
9+
* Enables Unix lines mode.
10+
*
11+
* @see java.util.regex.Pattern#UNIX_LINES
12+
*/
13+
UNIX_LINES(java.util.regex.Pattern.UNIX_LINES),
14+
15+
/**
16+
* Enables case-insensitive matching.
17+
*
18+
* @see java.util.regex.Pattern#CASE_INSENSITIVE
19+
*/
20+
CASE_INSENSITIVE(java.util.regex.Pattern.CASE_INSENSITIVE),
21+
22+
/**
23+
* Permits whitespace and comments in pattern.
24+
*
25+
* @see java.util.regex.Pattern#COMMENTS
26+
*/
27+
COMMENTS(java.util.regex.Pattern.COMMENTS),
28+
29+
/**
30+
* Enables multiline mode.
31+
*
32+
* @see java.util.regex.Pattern#MULTILINE
33+
*/
34+
MULTILINE(java.util.regex.Pattern.MULTILINE),
35+
36+
/**
37+
* Enables dotall mode.
38+
*
39+
* @see java.util.regex.Pattern#DOTALL
40+
*/
41+
DOTALL(java.util.regex.Pattern.DOTALL),
42+
43+
/**
44+
* Enables Unicode-aware case folding.
45+
*
46+
* @see java.util.regex.Pattern#UNICODE_CASE
47+
*/
48+
UNICODE_CASE(java.util.regex.Pattern.UNICODE_CASE),
49+
50+
/**
51+
* Enables canonical equivalence.
52+
*
53+
* @see java.util.regex.Pattern#CANON_EQ
54+
*/
55+
CANON_EQ(java.util.regex.Pattern.CANON_EQ);
56+
57+
//JDK flag value
58+
private final int value;
59+
60+
private RegexFlag(int value) {
61+
this.value = value;
62+
}
63+
64+
/**
65+
* @return flag value as defined in {@link java.util.regex.Pattern}
66+
*/
67+
public int getValue() {
68+
return value;
69+
}
70+
}
Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package io.avaje.validation.adapter;
22

3-
import java.lang.reflect.Type;
43
import java.util.Collection;
54
import java.util.Objects;
65

7-
import io.avaje.validation.Validator;
8-
96
public interface ValidationAdapter<T> {
107

118
/** Return true if validation should recurse */
@@ -15,37 +12,37 @@ default boolean validate(T value, ValidationRequest req) {
1512
return validate(value, req, null);
1613
}
1714

18-
default boolean validateAll(Collection<T> value, ValidationRequest req, String propertName) {
19-
if (propertName != null) {
20-
req.pushPath(propertName);
15+
default boolean validateAll(Collection<T> value, ValidationRequest req, String propertyName) {
16+
if (propertyName != null) {
17+
req.pushPath(propertyName);
2118
}
2219
int index = -1;
2320
for (final T element : value) {
2421
index++;
25-
validate(element, req, String.valueOf(index));
22+
validate(element, req, String.valueOf(index));
2623
}
27-
if (propertName != null) {
24+
if (propertyName != null) {
2825
req.popPath();
2926
}
3027
return true;
3128
}
3229

33-
default boolean validateAll(T[] value, ValidationRequest req, String propertName) {
34-
if (propertName != null) {
35-
req.pushPath(propertName);
30+
default boolean validateAll(T[] value, ValidationRequest req, String propertyName) {
31+
if (propertyName != null) {
32+
req.pushPath(propertyName);
3633
}
3734
int index = -1;
3835
for (final T element : value) {
3936
index++;
40-
validate(element, req, String.valueOf(index));
37+
validate(element, req, String.valueOf(index));
4138
}
42-
if (propertName != null) {
39+
if (propertyName != null) {
4340
req.popPath();
4441
}
4542
return true;
4643
}
4744

48-
default AnnotationValidationAdapter<T> andThen(ValidationAdapter<? super T> after) {
45+
default ValidationAdapter<T> andThen(ValidationAdapter<? super T> after) {
4946
Objects.requireNonNull(after);
5047
return (value, req, propertyName) -> {
5148
if (validate(value, req, propertyName)) {
@@ -55,14 +52,4 @@ default AnnotationValidationAdapter<T> andThen(ValidationAdapter<? super T> afte
5552
};
5653
}
5754

58-
/** Factory for creating a ValidationAdapter. */
59-
interface Factory {
60-
61-
/**
62-
* Create and return a ValidationAdapter given the type and annotations or return null.
63-
*
64-
* <p>Returning null means that the adapter could be created by another factory.
65-
*/
66-
ValidationAdapter<?> create(Type type, AdapterBuildContext ctx);
67-
}
6855
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.avaje.validation.adapter;
2+
3+
import java.lang.annotation.Annotation;
4+
import java.lang.reflect.Type;
5+
import java.util.Map;
6+
7+
public interface ValidationContext {
8+
9+
/**
10+
* Return the adapter for the given type.
11+
*/
12+
<T> ValidationAdapter<T> adapter(Class<T> cls);
13+
14+
/**
15+
* Return the adapter for the given type.
16+
*/
17+
<T> ValidationAdapter<T> adapter(Type type);
18+
19+
/**
20+
* Return the adapter for the given annotation with attributes.
21+
*/
22+
<T> ValidationAdapter<T> adapter(Class<? extends Annotation> cls, Map<String, Object> attributes);
23+
24+
/**
25+
* Return the message to use given the global key and attributes.
26+
* @param key Used to lookup the fallback default message format to use
27+
* @param attributes Attributes that could contain user defined message to use
28+
*/
29+
String message(String key, Map<String, Object> attributes);
30+
31+
/**
32+
* Factory for creating a ValidationAdapter for a given type.
33+
*/
34+
interface AdapterFactory {
35+
36+
/**
37+
* Create and return a ValidationAdapter given the type and annotations or return null.
38+
*
39+
* <p>Returning null means that the adapter could be created by another factory.
40+
*/
41+
ValidationAdapter<?> create(Type type, ValidationContext ctx);
42+
}
43+
44+
/**
45+
* Factory for creating a ValidationAdapter for a given annotation.
46+
*/
47+
interface AnnotationFactory {
48+
49+
/**
50+
* Create and return a ValidationAdapter given the type and annotations or return null.
51+
*
52+
* <p>Returning null means that the adapter could be created by another factory.
53+
*/
54+
ValidationAdapter<?> create(
55+
Class<? extends Annotation> annotationType, ValidationContext ctx, Map<String, Object> attributes);
56+
}
57+
}

0 commit comments

Comments
 (0)