#Validation of a container class

6 messages · Page 1 of 1 (latest)

keen notch
#

Hello, I have a container class defined like so:

public sealed interface Either<L,R> {
    record Left<L, R>(L value) implements Either<L, R> {}
    record Right<L, R>(R value) implements Either<L, R> {}
}

also I wrote a required ValueExtractor

public class RightValueExtractor<L, R> implements ValueExtractor<Either<L, @ExtractedValue R>> {
    @Override
    public void extractValues(Either<L, R> either, ValueReceiver valueReceiver) {
        switch(either) {
            case Either.Right<L, R> right -> valueReceiver.value(null, right.value());
            default -> throw new IllegalStateException("Unexpected value: " + either);
        }
    }
}
// LeftValueExtractor implemented likewise

and mentioned it in the services file appropriately (meaning ide does not complain)

But when I'm trying to validate the value inside of Either with a following sample

@AllArgsConstructor
class ValidationTesting {
    private final Either<Ignore, @Email String> email;
}
...
ValidationTesting email = new ValidationTesting(new Either.Left(Ignore.IGNORE));
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
assertEquals("Validation: ", 0, validator.validate(email).size());

i get this error:

jakarta.validation.ConstraintDeclarationException: HV000197: No value extractor found for type parameter 'R' of type com.example.Either.

What is wrong here? Is there something that I'm missing?

late bobcatBOT
#

This post has been reserved for your question.

Hey @keen notch! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

tropic stream
#

Are you adding respective extractor instances to the context?

keen notch
tropic stream
#

Ah, perhaps it's automatically added when constructed with buildDefaultValidatorFactory then.