jirutka/validator-spring

SpELAssert not working on fields

Closed this issue · 2 comments

Using SpELAsssert 1.1.0 and Spring Boot 1.3.6.

SpELAsserts work fine at type level but the equivalent ones don't work at all at field level.

WORKS
@SpELAssert("value != null") class TypeAssertedRequest { public String value; }

DOESN'T WORK
class FieldAssertedRequest { @SpELAssert("#this != null") public String value; }

I've also put together a small maven project to reproduce it.

It seems that all null values are considered valid.
See

public boolean isValid(Object object, ConstraintValidatorContext context) {
if (object == null) return true;

Thanks for looking into this @aristotll
I guess that's fair, many implementations of constraint validators do the same.

The workaround is to have an additional @NotNull validator like below:
class FieldAssertedRequest { @NotNull @SpELAssert("#this != null") public String value; }