Skip to content

Commit c7832b7

Browse files
authored
Change andThen() to continue false when first validator is false (#275)
This is fixing a semantic bug (that has no impact because the validation chain stops anyway) to return false indicating that the validation shouldn't continue. It has no impact because currently the generated code uses a single chain and that DOES actually stop processing (but then just returns the wrong value). Tidy this up so that a followup refactor turning this into a lambda is then no behaviour change.
1 parent a414eed commit c7832b7

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ default ValidationAdapter<T> andThen(ValidationAdapter<? super T> after) {
9797
if (validate(value, req, propertyName)) {
9898
return after.validate(value, req, propertyName);
9999
}
100-
return true;
100+
return false;
101101
};
102102
}
103103

validator/src/test/java/io/avaje/validation/core/adapters/NullableAdapterTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ void continueOnInvalid_expect_false() {
2929
assertThat(notNulladapter.validate(null, request, "foo")).isFalse();
3030
}
3131

32+
@Test
33+
void andThenContinue_expect_false() {
34+
ValidationAdapter<Object> otherAdapter =
35+
ctx.adapter(SizeTest.Size.class, Map.of("message", "blank?", "min", 2, "max", 3));
36+
37+
var combinedAndThen =
38+
ctx.<String>adapter(NotNull.class, Map.of("message", "myCustomNullMessage"))
39+
.andThen(otherAdapter);
40+
41+
assertThat(combinedAndThen.validate(null, request, "foo")).isFalse();
42+
}
43+
3244
@Test
3345
void testNull() {
3446
assertThat(isValid(nulladapter, null)).isTrue();

0 commit comments

Comments
 (0)