|
15 | 15 | */
|
16 | 16 | package io.avaje.validation.core;
|
17 | 17 |
|
| 18 | +import java.lang.reflect.Array; |
18 | 19 | import java.time.LocalDate;
|
19 | 20 | import java.time.LocalTime;
|
20 | 21 | import java.time.temporal.TemporalAccessor;
|
| 22 | +import java.util.Collection; |
21 | 23 | import java.util.Map;
|
22 | 24 |
|
23 | 25 | import io.avaje.validation.adapter.AnnotationValidationAdapter;
|
24 | 26 | import io.avaje.validation.adapter.ValidationRequest;
|
25 | 27 | import jakarta.validation.constraints.AssertTrue;
|
26 | 28 | import jakarta.validation.constraints.NotBlank;
|
27 | 29 | import jakarta.validation.constraints.Past;
|
| 30 | +import jakarta.validation.constraints.Size; |
28 | 31 |
|
29 | 32 | final class JakartaTypeAdapters {
|
| 33 | + private JakartaTypeAdapters() {} |
30 | 34 |
|
31 | 35 | @SuppressWarnings({"unchecked", "rawtypes"})
|
32 | 36 | static final AnnotationValidationAdapter.Factory FACTORY =
|
33 | 37 | (annotationType, validator, interpolator) -> {
|
34 | 38 | if (annotationType == AssertTrue.class) return new AssertTrueAdapter(interpolator);
|
35 | 39 | if (annotationType == NotBlank.class) return new NotBlankAdapter(interpolator);
|
36 | 40 | if (annotationType == Past.class) return new PastAdapter(interpolator);
|
| 41 | + if (annotationType == Size.class) return new SizeAdapter(interpolator); |
37 | 42 | return null;
|
38 | 43 | };
|
39 | 44 |
|
| 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 | + |
40 | 107 | private static final class PastAdapter implements AnnotationValidationAdapter<TemporalAccessor> {
|
41 | 108 |
|
42 | 109 | private String message;
|
|
0 commit comments