-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Validator / JSR-303 and Severity
PostPosted: Tue Oct 04, 2011 10:11 am 
Newbie

Joined: Tue Oct 04, 2011 4:15 am
Posts: 5
Hibernate Validator 4.2 - JSR 303

The documentation for JSR 303 actually mentions the use case of a Severity as a payload into the annotation property. However, I actually need the severity to be determined by code dynamically.

e.g. The same validation may be of severity of an Error in one case but can be a Warning in another case, and whether or not the severity is a Warning or an Error is determined by some business logic / code. When I read about the Severity as a payload, I said "great!" ... but the severity-as-payload use-case means that the validator client needs to set the payload, and since the payload is an annotation property, that means I need to have a different bean, one for each type of severity, just for the sake of setting the severity.

Instead of the validation client setting the severity via a payload, what I think should happen is that a ConstraintViolation should have the Severity attached to it. That is, the validation client should not ( must not ? ) set the severity of the constraint violation, but the class implementing the ConstraintValidator must determine the severity of the violation as it has the business logic behind it to know whether it should be an Error or a Warning.

Is there a way to create / use a custom ConstraintViolation with a Severity attached to it, within the JSR-303 or Hibernater 4.2 framework ?


Top
 Profile  
 
 Post subject: Re: Validator / JSR-303 and Severity
PostPosted: Thu Oct 06, 2011 3:22 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

the short answer to your question is, that there is no such feature.
One potential solution is to have multiple groups, one for each severity level and then validate only this group. Does this help in your use-case?

--Hardy


Top
 Profile  
 
 Post subject: Re: Validator / JSR-303 and Severity
PostPosted: Thu Oct 06, 2011 5:52 am 
Newbie

Joined: Tue Oct 04, 2011 4:15 am
Posts: 5
What I have done so far is adding a severity elemen / attribute into an annotation as such:

Code:
public @interface AuthorityToBackdate {
    < .... >
    Severity severity() default Severity.ERROR;
}



I then define an enum of the severity, and within that enum class, I then obtain the severity:

Code:
import javax.validation.ConstraintViolation;
import javax.validation.Payload;

public enum Severity implements Payload {
   INFO, WARNING, ERROR, FATAL;
   
   public static Severity getSeverity(ConstraintViolation violation) {
      Severity severity = (Severity) violation.getConstraintDescriptor().getAttributes().get("severity");
      return severity;
   }
}


Then from the custom validator, I can then change the attribute to a WARNING.
And then from the BV client, after doing the validation, I can then do something lik below:

Code:
Severity severity = Severity.getSeverity(violation);


.. to find out the severity.

However, the above means that:

1) All my custom annotations must have the attribute severity defined
2) I should modify the method Severit.getSeverity() so that it returns Severity.ERROR by default if the attribute does not exist.

Some questions though:

Since the severity is an attribute of the constraint and obtained via ConstraintDescriptor.getAttribute(), is setting any attribute for that matter within a ConstraintValidator ( e.g. to change it from default of ERROR to WARNING ) thread safe ?

Also, if after an execution of the custom validator, the attribute was changed to WARNING, will subsequent calls to the validator and / or will subsequent BV client calls see the attribute as WARNING instead of ERROR ? That is, changing the value of an attribute becomes its new current value unless otherwise changed again ?


Top
 Profile  
 
 Post subject: Re: Validator / JSR-303 and Severity
PostPosted: Thu Oct 06, 2011 7:20 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I am not quite sure how your solution is supposed to work.

salvojo wrote:
Since the severity is an attribute of the constraint and obtained via ConstraintDescriptor.getAttribute(), is setting any attribute for that matter within a ConstraintValidator ( e.g. to change it from default of ERROR to WARNING ) thread safe ?


You cannot set it all. The returned map is immutable (or at least should be).

I think you should investigates groups.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.