<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.opentaps.org/docs/index.php?action=history&amp;feed=atom&amp;title=Opentaps_2_Service_Validation</id>
	<title>Opentaps 2 Service Validation - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://docs.opentaps.org/docs/index.php?action=history&amp;feed=atom&amp;title=Opentaps_2_Service_Validation"/>
	<link rel="alternate" type="text/html" href="https://docs.opentaps.org/docs/index.php?title=Opentaps_2_Service_Validation&amp;action=history"/>
	<updated>2026-05-15T17:50:43Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.32.1</generator>
	<entry>
		<id>https://docs.opentaps.org/docs/index.php?title=Opentaps_2_Service_Validation&amp;diff=7196&amp;oldid=prev</id>
		<title>Jwickers: Protected &quot;Opentaps 2 Service Validation&quot;: Sysop page [edit=sysop:move=sysop]</title>
		<link rel="alternate" type="text/html" href="https://docs.opentaps.org/docs/index.php?title=Opentaps_2_Service_Validation&amp;diff=7196&amp;oldid=prev"/>
		<updated>2012-01-30T14:50:21Z</updated>

		<summary type="html">&lt;p&gt;Protected &amp;quot;&lt;a href=&quot;/docs/index.php?title=Opentaps_2_Service_Validation&quot; title=&quot;Opentaps 2 Service Validation&quot;&gt;Opentaps 2 Service Validation&lt;/a&gt;&amp;quot;: Sysop page [edit=sysop:move=sysop]&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 14:50, 30 January 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Jwickers</name></author>
		
	</entry>
	<entry>
		<id>https://docs.opentaps.org/docs/index.php?title=Opentaps_2_Service_Validation&amp;diff=7195&amp;oldid=prev</id>
		<title>Jwickers: New page: There are currently two level of validations, one at the service level which validates the parameters given, and one at the persistence level.  == General Concept ==  Validation at those l...</title>
		<link rel="alternate" type="text/html" href="https://docs.opentaps.org/docs/index.php?title=Opentaps_2_Service_Validation&amp;diff=7195&amp;oldid=prev"/>
		<updated>2012-01-30T14:50:21Z</updated>

		<summary type="html">&lt;p&gt;New page: There are currently two level of validations, one at the service level which validates the parameters given, and one at the persistence level.  == General Concept ==  Validation at those l...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;There are currently two level of validations, one at the service level which validates the parameters given, and one at the persistence level.&lt;br /&gt;
&lt;br /&gt;
== General Concept ==&lt;br /&gt;
&lt;br /&gt;
Validation at those levels is implemented using Bean Validation (JSR-303), which simply consist of annotations on a POJO's properties or accessors. There are numerous online articles to learn more about it, eg: http://java.dzone.com/articles/bean-validation-made-simple&lt;br /&gt;
&lt;br /&gt;
Some validators are quite simple, like ''javax.validation.constraints.NotNull'', and it is also very easy to define your own, like for example ''org.opentaps.validation.contraints.NotEmpty''.&lt;br /&gt;
&lt;br /&gt;
A Validator can also be set on a class itself when complex validation involving multiple properties is needed.&lt;br /&gt;
&lt;br /&gt;
== Validation on Entities ==&lt;br /&gt;
&lt;br /&gt;
JPA supports Bean Validation internally so an entity which is annotated will only be persisted if it is valid.&lt;br /&gt;
&lt;br /&gt;
== Validation in the Services ==&lt;br /&gt;
&lt;br /&gt;
Validation in the Services is done in a similar manner, the Input and Output objects are annotated and the service implementation is responsible for calling the validator.&lt;br /&gt;
&lt;br /&gt;
For this the ''validation.api'' bundle defines a ''ValidationService'' which is provided by the ''validation.impl'' bundle which itself simply expose the implementation given by Geronimo.&lt;br /&gt;
&lt;br /&gt;
A typical pattern in the service implementation can be seen in ''org.opentaps.notes.services.impl.CreateNoteServiceImpl'' and is basically:&lt;br /&gt;
&lt;br /&gt;
  Set&amp;lt;ConstraintViolation&amp;lt;MyServiceInput&amp;gt;&amp;gt; inputViolations = validationService.getValidator().validate(input);&lt;br /&gt;
  if (inputViolations.size() &amp;gt; 0) {&lt;br /&gt;
    throw new ServiceValidationException(&amp;quot;My service failed.&amp;quot;, (Set) inputViolations);&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  try {&lt;br /&gt;
    // do something involving  persistence&lt;br /&gt;
    ...&lt;br /&gt;
  } catch (ConstraintViolationException e) {&lt;br /&gt;
    throw new ServiceValidationException(&amp;quot;My service failed.&amp;quot;, e);&lt;br /&gt;
  } catch (PersistenceException e) {&lt;br /&gt;
    throw new ServiceException(&amp;quot;My service failed.&amp;quot;, e);&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // populate the output&lt;br /&gt;
  MyServiceOutput out = new MyServiceOutput();&lt;br /&gt;
  ...&lt;br /&gt;
  &lt;br /&gt;
  Set&amp;lt;ConstraintViolation&amp;lt;MyServiceOutput&amp;gt;&amp;gt; outputViolations = validationService.getValidator().validate(out);&lt;br /&gt;
  if (outputViolations.size() &amp;gt; 0) {&lt;br /&gt;
    throw new ServiceValidationException(&amp;quot;My service failed.&amp;quot;, (Set) outputViolations);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Sending Validation Errors to the Client ==&lt;br /&gt;
&lt;br /&gt;
As an example the Note rest service `org.opentaps.notes.rest.NoteResource` simply extends `org.opentaps.rest.ServerResource` which implements an exception handler that translates a `ServiceValidationException` into a JSON structure.&lt;br /&gt;
&lt;br /&gt;
Part of the JSON response includes a `errorDetail` object which is an array of `ServiceValidationException.FieldError` which contains 3 fields:&lt;br /&gt;
* ''field'' which is the field name in the service interface that was not valid&lt;br /&gt;
* ''message'' which is the error message returned by the validator&lt;br /&gt;
* ''value'' which was the value given that failed validation&lt;br /&gt;
&lt;br /&gt;
The client can then read that response to present the errors to the user.&lt;/div&gt;</summary>
		<author><name>Jwickers</name></author>
		
	</entry>
</feed>