You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@bval.apache.org by Umesh Awasthi <um...@gmail.com> on 2013/10/10 11:44:33 UTC

determine between class and property level constraint

I need to determine type of constraint violation from
[ConstraintViolation][1] object.
one way is to use `ConstraintViolation#getPropertyPath()`. If `getName()`
returns null on the `leaf` node you have a class level constraint,
otherwise a property level constraint.


One option is like

     Iterator<Node> violationNodes=violation.getPropertyPath().iterator();
        Node leafNode=null;
        while (violationNodes.hasNext()){
            leafNode=violationNodes.next();
        }

        if(leafNode!=null){
        // property constraint
        }
        else{
         // class constraint
        }


Is this good approach to determine or there can be other efficient or good
approach to do this?

-- 
With Regards
Umesh Awasthi
http://www.travellingrants.com/

Re: determine between class and property level constraint

Posted by Matt Benson <gu...@gmail.com>.
Work is in progress to release a Bean Validation 1.1-compatible release of
Apache BVal.  Thanks for your interest!

Matt


On Thu, Oct 10, 2013 at 11:05 AM, Umesh Awasthi <um...@gmail.com>wrote:

> I believe violation.getPropertyPath().toString() will do work in all cases.
> Thanks for your valuable inputs Matt.
>
> I have one more question regarding Bean Validation 1.1, is Apache Bval up
> with 1.1 ?
>
>
> On Thu, Oct 10, 2013 at 9:24 PM, Umesh Awasthi <um...@gmail.com>wrote:
>
>> Thanks for your input Matt.
>>
>> I only need to check if constraint  is declared on the class of the root
>> bean being validated, i am not interested about complete object graph.
>>
>>
>> I need to see more details about "violation.getPropertyPath().toString()"
>> .
>>
>> Thanks
>> Umesh
>>
>>
>> On Thu, Oct 10, 2013 at 9:18 PM, Matt Benson <gu...@gmail.com>wrote:
>>
>>> I am still not sure of your terminology.  If by "class level" you mean
>>> the constraint in question was declared on the class of the root bean being
>>> validated, then yes.  Actually, no--the right test would be
>>> violation.getPropertyPath().iterator().next().getName() == null.  If by
>>> "class level" you mean to learn whether the constraint was declared at the
>>> class level anywhere in your object graph, this test is not sufficient.
>>>
>>> The most efficient way I know to get an EL-compatible path such as you
>>> describe, at least in Apache BVal, is to invoke
>>> violation.getPropertyPath().toString().  ;)
>>>
>>> Matt
>>>
>>>
>>> On Thu, Oct 10, 2013 at 10:33 AM, Umesh Awasthi <um...@gmail.com>wrote:
>>>
>>>> Thanks Maat for your inputs,in short you mean if
>>>> "violation.getPropertyPath().
>>>> iterator().hasNext()." have elements than constraints is property level
>>>> else its a class level?
>>>>
>>>> Additionally i have one more issue to deal for nested bean properties
>>>> validation, say i have one bean "User" with few properties and another bean
>>>> "User Credentials" with more properties, so in order to populate data
>>>> Underlying framework uses following naming mechanism for HTML input fields
>>>>
>>>> user.firstName
>>>> user.user Credential.username etc
>>>>
>>>> which will be converted to getUser().setFirstName() etc by framework to
>>>> fill data
>>>>
>>>> and in order to show these messages at UI level, i need to get
>>>> information about field name being validated, one option is iterate through
>>>> "ConstraintViolation.getPropertyPath().iterator()" and build field names
>>>> but i am wondering if there is any other more efficient way to do this?
>>>>
>>>> Thanks
>>>> Umesh
>>>>
>>>>
>>>> On Thu, Oct 10, 2013 at 8:51 PM, Matt Benson <gu...@gmail.com>wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> Your sample code seems to do more work than necessary for the result
>>>>> it finds.  Every Node returned by the Path should be non-null, therefore
>>>>> your whole test could be reduced to
>>>>> violation.getPropertyPath().iterator().hasNext().
>>>>>
>>>>> I'm not sure the above actually gives you exactly what it is you're
>>>>> looking for.  If you simply want to know whether the violated constraint
>>>>> was declared as a class level constraint of the root bean, then it works.
>>>>>  But to use the Business/Employee model as an example, if you need to
>>>>> differentiate between a constraint declared on the Employee class as
>>>>> opposed to one declared on Business.manager (of type Employee), your
>>>>> approach will fall short.
>>>>>
>>>>> It appears to me as though (violation.getLeafBean() ==
>>>>> violation.getInvalidValue()) should be true when the constraint was
>>>>> declared on the bean class.
>>>>>
>>>>> Matt
>>>>>
>>>>>
>>>>>
>>>>> On Thu, Oct 10, 2013 at 4:44 AM, Umesh Awasthi <umeshawasthi@gmail.com
>>>>> > wrote:
>>>>>
>>>>>> I need to determine type of constraint violation from
>>>>>> [ConstraintViolation][1] object.
>>>>>> one way is to use `ConstraintViolation#getPropertyPath()`. If
>>>>>> `getName()` returns null on the `leaf` node you have a class level
>>>>>> constraint, otherwise a property level constraint.
>>>>>>
>>>>>>
>>>>>> One option is like
>>>>>>
>>>>>>      Iterator<Node>
>>>>>> violationNodes=violation.getPropertyPath().iterator();
>>>>>>         Node leafNode=null;
>>>>>>         while (violationNodes.hasNext()){
>>>>>>             leafNode=violationNodes.next();
>>>>>>         }
>>>>>>
>>>>>>         if(leafNode!=null){
>>>>>>         // property constraint
>>>>>>         }
>>>>>>         else{
>>>>>>          // class constraint
>>>>>>         }
>>>>>>
>>>>>>
>>>>>> Is this good approach to determine or there can be other efficient or
>>>>>> good approach to do this?
>>>>>>
>>>>>> --
>>>>>> With Regards
>>>>>> Umesh Awasthi
>>>>>> http://www.travellingrants.com/
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> With Regards
>>>> Umesh Awasthi
>>>> http://www.travellingrants.com/
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>> --
>> With Regards
>> Umesh Awasthi
>> http://www.travellingrants.com/
>>
>>
>>
>
>
>
> --
> With Regards
> Umesh Awasthi
> http://www.travellingrants.com/
>
>
>

Re: determine between class and property level constraint

Posted by Matt Benson <gu...@gmail.com>.
I am still not sure of your terminology.  If by "class level" you mean the
constraint in question was declared on the class of the root bean being
validated, then yes.  Actually, no--the right test would be
violation.getPropertyPath().iterator().next().getName() == null.  If by
"class level" you mean to learn whether the constraint was declared at the
class level anywhere in your object graph, this test is not sufficient.

The most efficient way I know to get an EL-compatible path such as you
describe, at least in Apache BVal, is to invoke
violation.getPropertyPath().toString().  ;)

Matt


On Thu, Oct 10, 2013 at 10:33 AM, Umesh Awasthi <um...@gmail.com>wrote:

> Thanks Maat for your inputs,in short you mean if
> "violation.getPropertyPath().
> iterator().hasNext()." have elements than constraints is property level
> else its a class level?
>
> Additionally i have one more issue to deal for nested bean properties
> validation, say i have one bean "User" with few properties and another bean
> "User Credentials" with more properties, so in order to populate data
> Underlying framework uses following naming mechanism for HTML input fields
>
> user.firstName
> user.user Credential.username etc
>
> which will be converted to getUser().setFirstName() etc by framework to
> fill data
>
> and in order to show these messages at UI level, i need to get information
> about field name being validated, one option is iterate through
> "ConstraintViolation.getPropertyPath().iterator()" and build field names
> but i am wondering if there is any other more efficient way to do this?
>
> Thanks
> Umesh
>
>
> On Thu, Oct 10, 2013 at 8:51 PM, Matt Benson <gu...@gmail.com> wrote:
>
>> Hello,
>>
>> Your sample code seems to do more work than necessary for the result it
>> finds.  Every Node returned by the Path should be non-null, therefore your
>> whole test could be reduced to
>> violation.getPropertyPath().iterator().hasNext().
>>
>> I'm not sure the above actually gives you exactly what it is you're
>> looking for.  If you simply want to know whether the violated constraint
>> was declared as a class level constraint of the root bean, then it works.
>>  But to use the Business/Employee model as an example, if you need to
>> differentiate between a constraint declared on the Employee class as
>> opposed to one declared on Business.manager (of type Employee), your
>> approach will fall short.
>>
>> It appears to me as though (violation.getLeafBean() ==
>> violation.getInvalidValue()) should be true when the constraint was
>> declared on the bean class.
>>
>> Matt
>>
>>
>>
>> On Thu, Oct 10, 2013 at 4:44 AM, Umesh Awasthi <um...@gmail.com>wrote:
>>
>>> I need to determine type of constraint violation from
>>> [ConstraintViolation][1] object.
>>> one way is to use `ConstraintViolation#getPropertyPath()`. If
>>> `getName()` returns null on the `leaf` node you have a class level
>>> constraint, otherwise a property level constraint.
>>>
>>>
>>> One option is like
>>>
>>>      Iterator<Node>
>>> violationNodes=violation.getPropertyPath().iterator();
>>>         Node leafNode=null;
>>>         while (violationNodes.hasNext()){
>>>             leafNode=violationNodes.next();
>>>         }
>>>
>>>         if(leafNode!=null){
>>>         // property constraint
>>>         }
>>>         else{
>>>          // class constraint
>>>         }
>>>
>>>
>>> Is this good approach to determine or there can be other efficient or
>>> good approach to do this?
>>>
>>> --
>>> With Regards
>>> Umesh Awasthi
>>> http://www.travellingrants.com/
>>>
>>>
>>>
>>
>>
>
>
> --
> With Regards
> Umesh Awasthi
> http://www.travellingrants.com/
>
>
>

Re: determine between class and property level constraint

Posted by Umesh Awasthi <um...@gmail.com>.
Thanks Maat for your inputs,in short you mean if
"violation.getPropertyPath().
iterator().hasNext()." have elements than constraints is property level
else its a class level?

Additionally i have one more issue to deal for nested bean properties
validation, say i have one bean "User" with few properties and another bean
"User Credentials" with more properties, so in order to populate data
Underlying framework uses following naming mechanism for HTML input fields

user.firstName
user.user Credential.username etc

which will be converted to getUser().setFirstName() etc by framework to
fill data

and in order to show these messages at UI level, i need to get information
about field name being validated, one option is iterate through
"ConstraintViolation.getPropertyPath().iterator()" and build field names
but i am wondering if there is any other more efficient way to do this?

Thanks
Umesh


On Thu, Oct 10, 2013 at 8:51 PM, Matt Benson <gu...@gmail.com> wrote:

> Hello,
>
> Your sample code seems to do more work than necessary for the result it
> finds.  Every Node returned by the Path should be non-null, therefore your
> whole test could be reduced to
> violation.getPropertyPath().iterator().hasNext().
>
> I'm not sure the above actually gives you exactly what it is you're
> looking for.  If you simply want to know whether the violated constraint
> was declared as a class level constraint of the root bean, then it works.
>  But to use the Business/Employee model as an example, if you need to
> differentiate between a constraint declared on the Employee class as
> opposed to one declared on Business.manager (of type Employee), your
> approach will fall short.
>
> It appears to me as though (violation.getLeafBean() ==
> violation.getInvalidValue()) should be true when the constraint was
> declared on the bean class.
>
> Matt
>
>
>
> On Thu, Oct 10, 2013 at 4:44 AM, Umesh Awasthi <um...@gmail.com>wrote:
>
>> I need to determine type of constraint violation from
>> [ConstraintViolation][1] object.
>> one way is to use `ConstraintViolation#getPropertyPath()`. If `getName()`
>> returns null on the `leaf` node you have a class level constraint,
>> otherwise a property level constraint.
>>
>>
>> One option is like
>>
>>      Iterator<Node> violationNodes=violation.getPropertyPath().iterator();
>>         Node leafNode=null;
>>         while (violationNodes.hasNext()){
>>             leafNode=violationNodes.next();
>>         }
>>
>>         if(leafNode!=null){
>>         // property constraint
>>         }
>>         else{
>>          // class constraint
>>         }
>>
>>
>> Is this good approach to determine or there can be other efficient or
>> good approach to do this?
>>
>> --
>> With Regards
>> Umesh Awasthi
>> http://www.travellingrants.com/
>>
>>
>>
>
>


-- 
With Regards
Umesh Awasthi
http://www.travellingrants.com/

Re: determine between class and property level constraint

Posted by Matt Benson <gu...@gmail.com>.
Hello,

Your sample code seems to do more work than necessary for the result it
finds.  Every Node returned by the Path should be non-null, therefore your
whole test could be reduced to
violation.getPropertyPath().iterator().hasNext().

I'm not sure the above actually gives you exactly what it is you're looking
for.  If you simply want to know whether the violated constraint was
declared as a class level constraint of the root bean, then it works.  But
to use the Business/Employee model as an example, if you need to
differentiate between a constraint declared on the Employee class as
opposed to one declared on Business.manager (of type Employee), your
approach will fall short.

It appears to me as though (violation.getLeafBean() ==
violation.getInvalidValue()) should be true when the constraint was
declared on the bean class.

Matt



On Thu, Oct 10, 2013 at 4:44 AM, Umesh Awasthi <um...@gmail.com>wrote:

> I need to determine type of constraint violation from
> [ConstraintViolation][1] object.
> one way is to use `ConstraintViolation#getPropertyPath()`. If `getName()`
> returns null on the `leaf` node you have a class level constraint,
> otherwise a property level constraint.
>
>
> One option is like
>
>      Iterator<Node> violationNodes=violation.getPropertyPath().iterator();
>         Node leafNode=null;
>         while (violationNodes.hasNext()){
>             leafNode=violationNodes.next();
>         }
>
>         if(leafNode!=null){
>         // property constraint
>         }
>         else{
>          // class constraint
>         }
>
>
> Is this good approach to determine or there can be other efficient or good
> approach to do this?
>
> --
> With Regards
> Umesh Awasthi
> http://www.travellingrants.com/
>
>
>