ConfigValidator { public Result validateRecursively(final AbstractConfig config) { final List<Result> resultList = new ArrayList<>(); final ConfigValidator.Result rootResult = validate(config); if (rootResult.hasErrorsOrWarnings()) { resultList.add(rootResult); } PropertyUtil.walkObjectTree( config, prop -> AbstractConfig.class.isAssignableFrom(prop.getValueClass()) && prop.getValueFromConfigObject() != null, prop -> { final AbstractConfig configObject = (AbstractConfig) prop.getValueFromConfigObject(); final ConfigValidator.Result result = validate(configObject); if (result.hasErrorsOrWarnings()) { resultList.add(result); } }); return ConfigValidator.Result.of(resultList); } @Inject  ConfigValidator(final Validator validator); Result validateValue(final Class<? extends AbstractConfig> configClass,
                                final String propertyName,
                                final Object value); Result validate(final AbstractConfig config); Result validateRecursively(final AbstractConfig config); static void logConstraintViolation(
        final ConstraintViolation<? extends AbstractConfig> constraintViolation,
        final ValidationSeverity severity);  }
@Test void testMyPojo_good() { final Injector injector = Guice.createInjector(new ValidationModule()); injector.injectMembers(this); var myPojo = new MyPojoErrors(); ConfigValidator.Result result = configValidator.validateRecursively(myPojo); LOGGER.info(result.toString()); result.handleViolations((constraintViolation, validationSeverity) -> { LOGGER.info("{}, {}", validationSeverity, constraintViolation); }); Assertions.assertThat(result.getErrorCount()) .isEqualTo(0); Assertions.assertThat(result.getWarningCount()) .isEqualTo(0); }
@Test void testMyPojo_errors_recursive() { final Injector injector = Guice.createInjector(new ValidationModule()); injector.injectMembers(this); var myPojo = new MyPojoErrors(); myPojo.setBooleanValue(false); myPojo.setRegexValue("((("); myPojo.setCronValue("xxxxxxxxxxxxx"); myPojo.setIntValue(0); myPojo.getChild().setBooleanValue(false); myPojo.getChild().setRegexValue("((("); myPojo.getChild().setCronValue("xxxxxxxxxxxxx"); myPojo.getChild().setIntValue(0); ConfigValidator.Result result = configValidator.validateRecursively(myPojo); LOGGER.info(result.toString()); result.handleViolations((constraintViolation, validationSeverity) -> { LOGGER.info("{}, {}", validationSeverity, constraintViolation); }); Assertions.assertThat(result.getErrorCount()).isEqualTo(4); Assertions.assertThat(result.getWarningCount()).isEqualTo(4); }