File tree Expand file tree Collapse file tree
main/java/it/aboutbits/springboot/testing/validation/core
test/java/it/aboutbits/springboot/testing/validation Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2626import java .util .function .Consumer ;
2727
2828@ RequiredArgsConstructor
29- public abstract class BaseRuleBuilder <R extends BaseRuleBuilder <? >> implements
29+ public abstract class BaseRuleBuilder <R extends BaseRuleBuilder <R >> implements
3030 ValidationRulesData ,
3131 BetweenRule <R >,
3232 FutureRule <R >,
@@ -54,9 +54,10 @@ public void addRule(@NonNull Rule rule) {
5454 rules .add (rule );
5555 }
5656
57- public BaseRuleBuilder <R > withAdditionalRules (Consumer <BaseRuleBuilder <R >> registrar ) {
58- registrar .accept (this );
59- return this ;
57+ public <T extends BaseRuleBuilder <T >> T withAdditionalRules (Consumer <T > registrar ) {
58+ var self = (T ) this ;
59+ registrar .accept (self );
60+ return self ;
6061 }
6162
6263 public void isCompliant () {
Original file line number Diff line number Diff line change 3232import java .time .YearMonth ;
3333import java .time .ZonedDateTime ;
3434import java .time .temporal .ChronoUnit ;
35+ import java .util .function .Consumer ;
3536
3637import static it .aboutbits .springboot .testing .validation .ValidationAssertTest .TestValidationAssert .assertThatValidation ;
3738import static org .assertj .core .api .Assertions .assertThatCode ;
@@ -641,6 +642,24 @@ void shouldAlsoWorkForExtendedClassesEvenWithoutAllArgsConstructors() {
641642 .isCompliant ()
642643 );
643644 }
645+
646+ @ Test
647+ void usingRuleRegistrarShouldWork () {
648+ var item = new SomeExtendingClass ();
649+ item .notNull = "notNull" ;
650+ item .notNullPositiveOrZero = ScaledBigDecimal .ONE ;
651+
652+ Consumer <TestValidationAssert .TestRuleBuilder > registrar = (ruleBuilder -> ruleBuilder
653+ .notNull ("notNull" )
654+ .notNull ("notNull" )
655+ .positiveOrZero ("notNullPositiveOrZero" )
656+ );
657+
658+ assertThatValidation ().of (item )
659+ .usingBeanValidation ()
660+ .withAdditionalRules (registrar )
661+ .isCompliant ();
662+ }
644663 }
645664
646665 private static SomeValidParameter getSomeValidParameter () {
You can’t perform that action at this time.
0 commit comments