Skip to content

Commit 8253c85

Browse files
committed
Implement @Length using the SizeAdapter
1 parent 39e26b8 commit 8253c85

File tree

1 file changed

+1
-33
lines changed

1 file changed

+1
-33
lines changed

validator/src/main/java/io/avaje/validation/core/adapters/BasicAdapters.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ private BasicAdapters() {}
3030
case "NotBlank" -> new NotBlankAdapter(context.message(attributes), groups);
3131
case "NotEmpty" -> new NotEmptyAdapter(context.message(attributes), groups);
3232
case "Pattern" -> new PatternAdapter(context.message(attributes), groups, attributes);
33-
case "Size" -> new SizeAdapter(context.message(attributes), groups, attributes);
34-
case "Length" -> new LengthAdapter(context.message(attributes), groups, attributes);
33+
case "Size", "Length" -> new SizeAdapter(context.message(attributes), groups, attributes);
3534
default -> null;
3635
};
3736

@@ -113,37 +112,6 @@ public boolean validate(Object value, ValidationRequest req, String propertyName
113112
}
114113
}
115114

116-
private static final class LengthAdapter implements ValidationAdapter<Object> {
117-
118-
private final ValidationContext.Message message;
119-
private final Set<Class<?>> groups;
120-
private final int min;
121-
private final int max;
122-
123-
LengthAdapter(ValidationContext.Message message, Set<Class<?>> groups, Map<String, Object> attributes) {
124-
this.message = message;
125-
this.groups = groups;
126-
this.min = (int) attributes.get("min");
127-
this.max = (int) attributes.get("max");
128-
}
129-
130-
@Override
131-
public boolean validate(Object value, ValidationRequest req, String propertyName) {
132-
if (!checkGroups(groups, req) || value == null) {
133-
return true;
134-
}
135-
136-
if (value instanceof final CharSequence sequence) {
137-
final var len = sequence.length();
138-
if (len > max || len < min) {
139-
req.addViolation(message, propertyName);
140-
return false;
141-
}
142-
}
143-
return true;
144-
}
145-
}
146-
147115
private static final class NotBlankAdapter extends AbstractConstraintAdapter<CharSequence> {
148116

149117
NotBlankAdapter(ValidationContext.Message message, Set<Class<?>> groups) {

0 commit comments

Comments
 (0)