jakartaee / validation-spec

Jakarta Validation spec document
http://beanvalidation.org
Apache License 2.0
20 stars 22 forks source link

Enforce constraint validators to support injection if CDI is available #266

Open sdaschner opened 2 years ago

sdaschner commented 2 years ago

I thought this was actually enforced in the spec since a long time, but it turns out it's not:

If CDI is supported in the container, it would be a benefit for developers if they could rely that an implementation also supports dependency injection in the validator.

https://beanvalidation.org/2.0/spec/#integration-dependencyinjection enforces this only that the validator must be injectable itself, but not "inside". Additionally the scope then should be handled by CDI, if a CDI scoped annotation is present.

Example

@ApplicationScoped
public class OrderValidator implements ConstraintValidator<ValidOrder, CoffeeOrder> {

  @Inject
  CoffeeShop coffeeShop;

  // ...

Most impls IMO do this already, but it turns out not all (e.g. Payara), since it's not enforced in the spec. WDYT?

gsmet commented 2 years ago

It would probably make sense.

What's unclear to me is why Payara wouldn't support it given they are using Hibernate Validator IIRC and Hibernate Validator supports it.