[bv-dev] Tightly coupled validation

Hardy Ferentschik hardy at hibernate.org
Fri May 25 10:43:27 EDT 2012


Hi,

I am not quite sure where you are getting with this.

On May 25, 2012, at 4:06 PM, Cemo wrote:

> For example at interface level validator is declared by Interface.

What do you mean w/ "interface level validator". This is not a term used in the Bean Validation specification. 
There are class level constraints (constraints/annotations which you can place on a class or interface) in contrast 
to property constraints. However, I don't think this is what you mean.

> @Constraint(validatedBy = EmailValidator.class)
> @Retention(RUNTIME)
> public @interface Email 
> 
> And for the part EmailValidation part, we are referring again our Email Interface
> 
> public class EmailValidator implements ConstraintValidator<Email, String> 

There are two things at play first. First there is the constraint annotation definition (public @interface Email ). Here you are saying
there is a constraint @Email. You mark it as constraint via @Constraint. With validatedBy you are also specifying which class 
implements the validation logic for this constraint. If you wanted to you could leave this off, but then you would have to specify the 
implementations in xml. Something like:

    <constraint-definition annotation="com.acme.Emaill">
        <validated-by include-existing-validators="false">
            <value>com.acme.EmailValidator</value>
        </validated-by>
    </constraint-definition>

Secondly you have 'public class EmailValidator implements ConstraintValidator<Email, String> ' . Here you have Email again.
This makes the ConstraintValidator type safe and you cannot pass in any odd annotation, but an @Email annotation. So the first
type parameter tells me for which constraint this ConstraintValidator is intended for and the second which type it can validate.

> What I think is this contract is little tight. 

I don't quite follow.

 
> And let me to clarify my use case too. 
> 
> I have splitted my domain, validation and web projects (Web1, Web2, Web3) and using spring extensively. 
> 	• Domain (Has no dependency)
> 	• Validation (Has dependency to validation spec and optionally Spring Framework. For example, It can validate an uniqueEmail field by checking database. )
> 	• Web's (Has dependency to validation and domain)
> My domain needs dependency to validation for validating it but it means also depending spring and all other stuffs regarding Database validations. What I want to split Validation contract to interfaces and implementations. Interfaces could be declared at domain and implementations reside at validation module. Validation module has a dependency to domain perfectly. 

Hmm, sounds like the solution for you is the xml configuration. 

--Hardy




More information about the beanvalidation-dev mailing list