Skip to content

Commit

Permalink
re-format
Browse files Browse the repository at this point in the history
  • Loading branch information
making committed May 13, 2021
1 parent b851182 commit 0c65feb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 66 deletions.
13 changes: 9 additions & 4 deletions src/main/java/am/ik/yavi/builder/ValidatorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,13 @@ public Validator<T> build() {
* Create a <code>BiValidator</code> instance using the given constraints.
*
* In case of Spring Framework's Validator integration
* <pre>BiValidator&lt;CartItem, Errors&gt; validator = ValidatorBuilder
*
* <pre>
* BiValidator&lt;CartItem, Errors&gt; validator = ValidatorBuilder
* .&lt;CartItem&gt;of()
* .constraint(...)
* .build(Errors::rejectValue);</pre>
* .build(Errors::rejectValue);
* </pre>
*
* @param errorHandler handler that handle if the validation fails
* @param <E> the type of the error object
Expand Down Expand Up @@ -738,15 +741,17 @@ protected final <N> ValidatorBuilder<T> nest(Function<T, N> nested, String name,
};
}

private <N> Function<T, ?> toNestedFunction(Function<T, N> nested, ConstraintPredicates<N, ?> predicates) {
private <N> Function<T, ?> toNestedFunction(Function<T, N> nested,
ConstraintPredicates<N, ?> predicates) {
if (predicates instanceof NestedConstraintPredicates) {
return target -> {
N nestedValue = nested.apply(target);
if (nestedValue == null) {
return null;
}

return (N) ((NestedConstraintPredicates) predicates).nestedValue(nestedValue);
return (N) ((NestedConstraintPredicates) predicates)
.nestedValue(nestedValue);
};
}

Expand Down
111 changes: 49 additions & 62 deletions src/test/java/am/ik/yavi/core/NestedValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,121 +264,108 @@ class DeepNestingGH60Tests {
@Test
void shouldValidateObjectWithManyLayersNestedObjectWhenInnerNestedObjectIsRequired() {
final Validator<A> validator = ValidatorBuilder.of(A.class)
.nest(A::getB, "b",
b -> b.nest(B::getC, "c",
c -> c.nest(C::getD, "d",
d -> d.nest(D::getE, "e",
e -> e._integer(E::getValue, "value",
value -> value.greaterThanOrEqual(100))))))
.nest(A::getB, "b", b -> b.nest(B::getC, "c",
c -> c.nest(C::getD, "d", d -> d.nest(D::getE, "e",
e -> e._integer(E::getValue, "value",
value -> value.greaterThanOrEqual(100))))))
.build();

assertSoftly(softly -> {
softly.assertThat(validator.validate(new A(null)).isValid())
.as("1-layer nested object is null")
.isFalse();
.as("1-layer nested object is null").isFalse();
softly.assertThat(validator.validate(new A(new B(null))).isValid())
.as("2-layer nested object is null")
.isFalse();
.as("2-layer nested object is null").isFalse();
softly.assertThat(validator.validate(new A(new B(new C(null)))).isValid())
.as("3-layer nested object is null")
.isFalse();
softly.assertThat(validator.validate(new A(new B(new C(new D(null, null))))).isValid())
.as("3-layer nested object is null").isFalse();
softly.assertThat(validator
.validate(new A(new B(new C(new D(null, null))))).isValid())
.as("4-layer nested object is null (when it is required)")
.isFalse();
softly.assertThat(validator.validate(new A(new B(new C(new D(new E(100), null))))).isValid())
.as("5-layer constraint is valid")
.isTrue();
softly.assertThat(validator
.validate(new A(new B(new C(new D(new E(100), null))))).isValid())
.as("5-layer constraint is valid").isTrue();
});
}

@Test
void shouldValidateObjectWithManyLayersNestedObjectWhenInnerNestedObjectIsOptional() {
final Validator<A> validator = ValidatorBuilder.of(A.class)
.nest(A::getB, "b",
b -> b.nestIfPresent(B::getC, "c",
c -> c.nest(C::getD, "d",
d -> d.nestIfPresent(D::getE, "e",
e -> e._integer(E::getValue, "value",
value -> value.greaterThanOrEqual(100))))))
.nest(A::getB, "b", b -> b.nestIfPresent(B::getC, "c",
c -> c.nest(C::getD, "d", d -> d.nestIfPresent(D::getE, "e",
e -> e._integer(E::getValue, "value",
value -> value.greaterThanOrEqual(100))))))
.build();

assertSoftly(softly -> {
softly.assertThat(validator.validate(new A(null)).isValid())
.as("1-layer nested object is null")
.isFalse();
.as("1-layer nested object is null").isFalse();
softly.assertThat(validator.validate(new A(new B(null))).isValid())
.as("2-layer nested object is null (when it is optional)")
.isTrue();
softly.assertThat(validator.validate(new A(new B(new C(null)))).isValid())
.as("3-layer nested object is null")
.isFalse();
softly.assertThat(validator.validate(new A(new B(new C(new D(null, null))))).isValid())
.as("3-layer nested object is null").isFalse();
softly.assertThat(validator
.validate(new A(new B(new C(new D(null, null))))).isValid())
.as("4-layer nested object is null (when it is optional)")
.isTrue();
softly.assertThat(validator.validate(new A(new B(new C(new D(new E(100), null))))).isValid())
.as("5-layer constraint is valid")
.isTrue();
softly.assertThat(validator
.validate(new A(new B(new C(new D(new E(100), null))))).isValid())
.as("5-layer constraint is valid").isTrue();
});
}

@Test
void shouldValidateObjectWithManyLayersNestedObjectWhenForEachMethodIsUsed() {
final Validator<A> validator = ValidatorBuilder.of(A.class)
.nest(A::getB, "b",
b -> b.nest(B::getC, "c",
c -> c.nest(C::getD, "d",
d -> d.forEach(D::getList, "list",
e -> e._integer(E::getValue, "value",
value -> value.greaterThanOrEqual(100))))))
.nest(A::getB, "b", b -> b.nest(B::getC, "c",
c -> c.nest(C::getD, "d", d -> d.forEach(D::getList, "list",
e -> e._integer(E::getValue, "value",
value -> value.greaterThanOrEqual(100))))))
.build();


assertSoftly(softly -> {
softly.assertThat(validator.validate(new A(null)).isValid())
.as("1-layer nested object is null")
.isFalse();
.as("1-layer nested object is null").isFalse();
softly.assertThat(validator.validate(new A(new B(null))).isValid())
.as("2-layer nested object is null")
.isFalse();
.as("2-layer nested object is null").isFalse();
softly.assertThat(validator.validate(new A(new B(new C(null)))).isValid())
.as("3-layer nested object is null")
.isFalse();
softly.assertThat(validator.validate(new A(new B(new C(new D(null, null))))).isValid())
.as("3-layer nested object is null").isFalse();
softly.assertThat(validator
.validate(new A(new B(new C(new D(null, null))))).isValid())
.as("4-layer nested object is null (when it is required)")
.isFalse();
softly.assertThat(validator.validate(new A(new B(new C(new D(null, singletonList(new E(100))))))).isValid())
.as("5-layer constraint is valid")
.isTrue();
softly.assertThat(validator
.validate(new A(
new B(new C(new D(null, singletonList(new E(100)))))))
.isValid()).as("5-layer constraint is valid").isTrue();
});
}

@Test
void shouldValidateObjectWithManyLayersNestedObjectWhenForEachIfPresentMethodIsUsed() {
final Validator<A> validator = ValidatorBuilder.of(A.class)
.nest(A::getB, "b",
b -> b.nestIfPresent(B::getC, "c",
c -> c.nest(C::getD, "d",
d -> d.forEachIfPresent(D::getList, "list",
e -> e._integer(E::getValue, "value",
value -> value.greaterThanOrEqual(100))))))
final Validator<A> validator = ValidatorBuilder.of(A.class).nest(A::getB, "b",
b -> b.nestIfPresent(B::getC, "c", c -> c.nest(C::getD, "d",
d -> d.forEachIfPresent(D::getList, "list",
e -> e._integer(E::getValue, "value",
value -> value.greaterThanOrEqual(100))))))
.build();

assertSoftly(softly -> {
softly.assertThat(validator.validate(new A(null)).isValid())
.as("1-layer nested object is null")
.isFalse();
.as("1-layer nested object is null").isFalse();
softly.assertThat(validator.validate(new A(new B(null))).isValid())
.as("2-layer nested object is null (when it is optional)")
.isTrue();
softly.assertThat(validator.validate(new A(new B(new C(null)))).isValid())
.as("3-layer nested object is null")
.isFalse();
softly.assertThat(validator.validate(new A(new B(new C(new D(null, null))))).isValid())
.as("3-layer nested object is null").isFalse();
softly.assertThat(validator
.validate(new A(new B(new C(new D(null, null))))).isValid())
.as("4-layer nested object is null (when it is optional)")
.isTrue();
softly.assertThat(validator.validate(new A(new B(new C(new D(null, singletonList(new E(100))))))).isValid())
.as("5-layer constraint is valid")
.isTrue();
softly.assertThat(validator
.validate(new A(
new B(new C(new D(null, singletonList(new E(100)))))))
.isValid()).as("5-layer constraint is valid").isTrue();
});
}
}
Expand Down

0 comments on commit 0c65feb

Please sign in to comment.