ModelValidator { public List<ValidationError> getModelValidationErrors(Object object, Class validationGroup) { if (object == null) { return ImmutableList.of(ValidationError.of("", "object is null")); } Set<ConstraintViolation<Object>> violations = validator.validate(object, validationGroup); Set<String> errorFields = new HashSet<>(); ImmutableList.Builder<ValidationError> errors = ImmutableList.builder(); for (ConstraintViolation<Object> v : violations) { String field = v.getPropertyPath().toString(); if (!errorFields.contains(field)) { errors.add(ValidationError.of(field, v.getMessage())); errorFields.add(field); } } return errors.build(); } ModelValidator(Validator validator, ObjectMapper mapper); List<ValidationError> getModelValidationErrors(Object object, Class validationGroup); List<ValidationError> getCreateValidationErrors(Object create, FieldData fieldData); List<ValidationError> getUpdateValidationErrors(Update<?> update, FieldData fieldData); boolean validateModel(Object model); boolean validateCreate(Object create, FieldData fieldData); boolean validateUpdate(Update<?> update, FieldData fieldData);  }
@Test public void testGetModelValidationErrors() throws Exception { List<ValidationError> errors = validator.getModelValidationErrors(null, Default.class); assertEquals(1, errors.size()); assertEquals("object is null", errors.get(0).getMessage()); TestValidateModel model = TestValidateModel.builder().email("invalid").build(); errors = validator.getModelValidationErrors(model, Default.class); assertEquals(1, errors.size()); assertEquals("must be a well-formed email address", errors.get(0).getMessage()); model = TestValidateModel.builder().email("example@example.com").build(); errors = validator.getModelValidationErrors(model, Default.class); assertEquals(0, errors.size()); }