I have a POJO like below:
{code} @Value. Immutable public Immutablepublic interface Customer { @NotBlank String @NotBlankString name(); Optional<@Valid @NotNull(groups = Address.class) AddressDetails> addressDetails(); @Value. Immutable interface Immutableinterface AddressDetails { @NotBlank String @NotBlankString details(); } } {code}
And I have written a validator like this:
{code} private static Set<ConstraintViolation> validate(final Object object, final Class<?>... groups) { Set<ConstraintViolation> violations = new HashSet<>(); for (Method method : object.getClass().getInterfaces()[0].getDeclaredMethods()) { try { VALIDATOR.validateReturnValue( object, method, method.invoke(object), groups).forEach(constraint -> { ConstraintViolation constraintViolation = new ConstraintViolation( method.getName(), constraint.getMessageTemplate() ); violations.add(constraintViolation); }); } catch (IllegalAccessException | InvocationTargetException e) { throw new IllegalStateException("daads", e); } } return violations; } {code}
When I pass the following Object of Customer in validate method above,
`
{code} ImmutableCustomer.builder().name("test").addressDetails(ImmutableAddressDetails.builder().details("")).build ` {code}
{{ in object and group as `Address.class` }}
I feel like it should return a violation as details field is empty and I marked it as NotBlank. But it does not fail. Any idea what I am doing wrong? |
|