Skip to content

Commit f515559

Browse files
committed
Size Adapter
1 parent 74975bf commit f515559

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
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;
36
import static java.lang.annotation.RetentionPolicy.CLASS;
47

5-
import java.lang.annotation.ElementType;
68
import java.lang.annotation.Retention;
79
import java.lang.annotation.Target;
810

911
/**
1012
* Marks a type for validation.
1113
*/
1214
@Retention(CLASS)
13-
@Target(ElementType.TYPE)
15+
@Target({TYPE, FIELD})
1416
public @interface ValidPojo {
1517

1618
/**
1719
*/
1820
@Retention(CLASS)
19-
@Target({ElementType.TYPE, ElementType.PACKAGE})
21+
@Target({TYPE, PACKAGE})
2022
@interface Import {
2123

2224
/**

validator/src/main/java/io/avaje/validation/core/JakartaTypeAdapters.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,95 @@
1515
*/
1616
package io.avaje.validation.core;
1717

18+
import java.lang.reflect.Array;
1819
import java.time.LocalDate;
1920
import java.time.LocalTime;
2021
import java.time.temporal.TemporalAccessor;
22+
import java.util.Collection;
2123
import java.util.Map;
2224

2325
import io.avaje.validation.adapter.AnnotationValidationAdapter;
2426
import io.avaje.validation.adapter.ValidationRequest;
2527
import jakarta.validation.constraints.AssertTrue;
2628
import jakarta.validation.constraints.NotBlank;
2729
import jakarta.validation.constraints.Past;
30+
import jakarta.validation.constraints.Size;
2831

2932
final class JakartaTypeAdapters {
33+
private JakartaTypeAdapters() {}
3034

3135
@SuppressWarnings({"unchecked", "rawtypes"})
3236
static final AnnotationValidationAdapter.Factory FACTORY =
3337
(annotationType, validator, interpolator) -> {
3438
if (annotationType == AssertTrue.class) return new AssertTrueAdapter(interpolator);
3539
if (annotationType == NotBlank.class) return new NotBlankAdapter(interpolator);
3640
if (annotationType == Past.class) return new PastAdapter(interpolator);
41+
if (annotationType == Size.class) return new SizeAdapter(interpolator);
3742
return null;
3843
};
3944

45+
private static final class SizeAdapter implements AnnotationValidationAdapter<Object> {
46+
47+
private String message;
48+
private final MessageInterpolator interpolator;
49+
private int min;
50+
private int max;
51+
52+
public SizeAdapter(MessageInterpolator interpolator) {
53+
this.interpolator = interpolator;
54+
}
55+
56+
@Override
57+
public AnnotationValidationAdapter<Object> init(Map<String, String> annotationValueMap) {
58+
message = interpolator.interpolate(annotationValueMap.get("message"));
59+
min = Integer.parseInt(interpolator.interpolate(annotationValueMap.get("min")));
60+
max = Integer.parseInt(interpolator.interpolate(annotationValueMap.get("max")));
61+
return this;
62+
}
63+
64+
@Override
65+
public boolean validate(Object value, ValidationRequest req, String propertyName) {
66+
67+
if (value instanceof CharSequence) {
68+
final var sequence = (CharSequence) value;
69+
final var len = sequence.length();
70+
if (len > max || len < min) {
71+
req.addViolation(message, propertyName);
72+
return false;
73+
}
74+
}
75+
76+
if (value instanceof Collection<?>) {
77+
final var col = (Collection<?>) value;
78+
final var len = col.size();
79+
if (len > max || len < min) {
80+
req.addViolation(message, propertyName);
81+
return false;
82+
}
83+
}
84+
85+
if (value instanceof Map<?, ?>) {
86+
final var col = (Map<?, ?>) value;
87+
final var len = col.size();
88+
if (len > max || len < min) {
89+
req.addViolation(message, propertyName);
90+
return false;
91+
}
92+
}
93+
94+
if (value.getClass().isArray()) {
95+
96+
final var len = Array.getLength(value);
97+
if (len > max || len < min) {
98+
req.addViolation(message, propertyName);
99+
return false;
100+
}
101+
}
102+
103+
return true;
104+
}
105+
}
106+
40107
private static final class PastAdapter implements AnnotationValidationAdapter<TemporalAccessor> {
41108

42109
private String message;

0 commit comments

Comments
 (0)