0

I try to validate beans by Hibernate Validator. It should be programable and based on a JSON config file. data models:

class A {
    field,
    other fields
}
class B {
    class A a,
    other fields
}
class C {
    class A a,
    other fields
}
class D {
    class B b,
    class C c,
    other fields
}

Validate D based on a JSON config file. The config file wants to validate the following: D.b.a.field should not be null. D.c.a.field is nullable.

Using program way, I set the validation like this:

constraintMapping
    .type( D.class )
        .property( "b", FIELD )
            .constraint( new NotNullDef() )
            .valid()
.type( B.class )
    .property( "a", FIELD )
        .constraint( new NotNullDef() )
        .valid()
.type ( A.class )
    .property( "field", FIELD )
        .constraint( new NotNullDef() );

Now, the A.field has been set to not null validator, but the problem is, now the D.c.a.filed also cannot be null.

This is just an example, I absolutely don't want to change the data models.

Wesley
  • 1

2 Answers2

0

Using groups at constraint level might help you solve your problem. To add a group to constraint you just need to call group method and pass a list of groups in which the constraint should be included:

constraintMapping
    .type( D.class )
        .property( "b", FIELD )
            .constraint( new NotNullDef().groups( SomeGroup.class )
            .valid()

And then you could create different groups for different "validation paths" and use them when validating an object. Thant might not work great though if your validation configs are fully dynamic.Also see the doc for more info on groups.

mark_o
  • 2,052
  • 1
  • 12
  • 18
  • Thank you for your advice. Yes, the validation configs are fully dynamic. I'll test and update the result later. – Wesley Mar 15 '19 at 12:06
  • Seems there are length limitation on comment, I have to post it as an answer. – Wesley Apr 01 '19 at 15:38
0

@mark-o, I tested the following solution, but I failed. It validates field1 and field2 for both GroupB and GroupC, but my expectation is validating field1 only for GroupB but not for GroupC, and validate field2 only for GroupC but not for GroupB.

A little change for Class A (has field1 and field2), and add a wrapper for class A which is used by both class B and class C. constraintMapping .type( D.class ) .property( "b", FIELD ) .constraint( new NotNullDef().groups(GroupB.class) ) .valid() .property( "c", FIELD ) .constraint( new NotNullDef().groups(GroupC.class) ) .valid() .type( B.class ) .property( "aWrapper", FIELD ) .constraint( new NotNullDef().groups(GroupB.class) ) .valid() .type( C.class ) .property( "aWrapper", FIELD ) .constraint( new NotNullDef().groups(GroupC.class) ) .valid() .type ( AWrapper.class ) .property( "a", FIELD ) .constraint( new NotNullDef().groups(GroupB.class, GroupC.class) ) .valid() .type ( A.class ) .property( "field1", FIELD ) .constraint( new NotNullDef().groups(GroupB.class) ) .property( "field2", FIELD ) .constraint( new NotNullDef().groups(GroupC.class) );

Wesley
  • 1