<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hibernate validator Archives - The Story of David Kaspar</title>
	<atom:link href="https://blog.davidkaspar.com/archives/tag/hibernate-validator/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.davidkaspar.com/archives/tag/hibernate-validator</link>
	<description>Online journal and ponderings about the Internet, gadgets and photography.</description>
	<lastBuildDate>Tue, 12 Jul 2011 16:27:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://blog.davidkaspar.com/wp-content/uploads/2020/05/chubby.gif</url>
	<title>hibernate validator Archives - The Story of David Kaspar</title>
	<link>https://blog.davidkaspar.com/archives/tag/hibernate-validator</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>JSR 303: Combining custom and standard validators</title>
		<link>https://blog.davidkaspar.com/archives/2011/07/jsr-303-combining-custom-and-standard-validators.php</link>
					<comments>https://blog.davidkaspar.com/archives/2011/07/jsr-303-combining-custom-and-standard-validators.php#respond</comments>
		
		<dc:creator><![CDATA[David Kaspar]]></dc:creator>
		<pubDate>Tue, 12 Jul 2011 16:27:49 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[hibernate validator]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jsr 303]]></category>
		<category><![CDATA[validation]]></category>
		<guid isPermaLink="false">https://davidkaspar.com/wp/2011/07/12/jsr-303-combining-custom-and-standard-validators/</guid>

					<description><![CDATA[<p>We are using the JSR 303 Bean validation API (Hibernate validator as the implementation) on a project and recently faced a problem. Whenever we were using our own custom validators in combination with standard validators like NotNull and NotEmpty, the standard validators seemed to be ignored. The result was that we were getting NullPointerExceptions in &#8230; <a href="https://blog.davidkaspar.com/archives/2011/07/jsr-303-combining-custom-and-standard-validators.php" class="more-link">Continue reading<span class="screen-reader-text"> "JSR 303: Combining custom and standard validators"</span></a></p>
<p>The post <a href="https://blog.davidkaspar.com/archives/2011/07/jsr-303-combining-custom-and-standard-validators.php">JSR 303: Combining custom and standard validators</a> appeared first on <a href="https://blog.davidkaspar.com">The Story of David Kaspar</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>We are using the <a href="http://jcp.org/en/jsr/detail?id=303">JSR 303 Bean validation</a> API (<a href="http://www.hibernate.org/subprojects/validator.html">Hibernate validator</a> as the implementation) on a project and recently faced a problem.<br />
Whenever we were using our own custom validators in combination with standard validators like NotNull and NotEmpty, the standard validators seemed to be ignored.<br />
The result was that we were getting NullPointerExceptions in our custom validators and sometimes we were getting duplicate error messages for the same invalid field.<br />
I haven&#8217;t poured over all of the JSR 303 documentation in detail so maybe I&#8217;ve missed a recommended best practice but the following code in the default NotBlankValidator gave me a hint.</p>
<pre style='color:#000020;background:#f6f8ff;'>public boolean isValid<span style='color:#308080; '>(</span><span style='color:#003060; '>String</span> s<span style='color:#308080; '>,</span>
ConstraintValidatorContext constraintValidatorContext<span style='color:#308080; '>)</span> <span style='color:#406080; '>{</span>
<span style='color:#200080; font-weight:bold; '>if</span> <span style='color:#308080; '>(</span> s <span style='color:#308080; '>=</span><span style='color:#308080; '>=</span> null <span style='color:#308080; '>)</span> <span style='color:#406080; '>{</span>
<span style='color:#200080; font-weight:bold; '>return</span> true<span style='color:#406080; '>;</span>
<span style='color:#406080; '>}</span>
<span style='color:#308080; '>.</span><span style='color:#308080; '>.</span><span style='color:#308080; '>.</span>
</pre>
<p>Why would you ever return true (i.e. valid) for an input that is null? That doesn&#8217;t make sense until you realise that validation is a *combination* of all constraints.<br />
This means that your validator should only return false for your specific test. If it fails for a chained validation, for example if input is null, it should return true and rely on the NotNull validation to report that error.<br />
You still have to guard for null and empty inputs but return true if they occur rather then returning false.<br />
Hide any required basic validations inside your custom constraint annotation. For example NotBlank does that:</p>
<pre style='color:#000020;background:#f6f8ff;'>...
@NotNull
<span style='color:#200080; font-weight:bold; '>public</span> @<span style='color:#200080; font-weight:bold; '>interface</span> NotBlank <span style='color:#406080; '>{</span>
<span style='color:#308080; '>.</span><span style='color:#308080; '>.</span><span style='color:#308080; '>.</span>
</pre>
<p>The post <a href="https://blog.davidkaspar.com/archives/2011/07/jsr-303-combining-custom-and-standard-validators.php">JSR 303: Combining custom and standard validators</a> appeared first on <a href="https://blog.davidkaspar.com">The Story of David Kaspar</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.davidkaspar.com/archives/2011/07/jsr-303-combining-custom-and-standard-validators.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
