| Supposing a type with the following definition:
public final class StringWrapper {
private final Optional<@NotBlank String> num;
public StringWrapper(Optional<String> num) {
this.num = num;
}
}
I would expect Optional.empty() to generate no validation errors, because the value is not present. Certainly that's the way other constraints work. Unfortunately, what I get instead is:
'must not be blank'
It's pretty clear why this is happening:
@Override
public void extractValues(Optional<?> originalValue, ValueExtractor.ValueReceiver receiver) {
receiver.value( null, originalValue.isPresent() ? originalValue.get() : null );
}
...and since null isn't a valid value, it of course fails the validation. Any null-failing validator would complain at that point. A more idiomatic way to deal with optionals would be something like:
@Override
public void extractValues(Optional<?> originalValue, ValueExtractor.ValueReceiver receiver) {
originalValue.ifPresent( value -> receiver.value( null, value ));
}
...which would only call the receiver if it was present. However, that's only if the value was present, which is a behavior change, so I'm unsure of all the ramifications of that. |