52 lines
4.0 KiB
HTML
52 lines
4.0 KiB
HTML
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg"><head><title>Validating Input</title><link rel="stylesheet" type="text/css" href="docbook-epub.css"/><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"/><link rel="prev" href="ch06s10.xhtml" title="Reusing Existing Services"/><link rel="next" href="ch06s12.xhtml" title="Preventing State Persistence"/></head><body><header/><section class="section" title="Validating Input" epub:type="subchapter" id="validatingInput"><div class="titlepage"><div><div><h2 class="title" style="clear: both">Validating Input</h2></div></div></div><p>During the course of this chapter, multiple approaches to parsing
|
|
input have been discussed. Each major implementation will throw an
|
|
exception if it is not 'well-formed'. The
|
|
<code class="classname">FixedLengthTokenizer</code> will throw an exception if a
|
|
range of data is missing. Similarly, attempting to access an index in a
|
|
<code class="classname">RowMapper</code> of <code class="classname">FieldSetMapper</code>
|
|
that doesn't exist or is in a different format than the one expected will
|
|
cause an exception to be thrown. All of these types of exceptions will be
|
|
thrown before <code class="methodname">read</code> returns. However, they don't
|
|
address the issue of whether or not the returned item is valid. For
|
|
example, if one of the fields is an age, it obviously cannot be negative.
|
|
It will parse correctly, because it existed and is a number, but it won't
|
|
cause an exception. Since there are already a plethora of Validation
|
|
frameworks, Spring Batch does not attempt to provide yet another, but
|
|
rather provides a very simple interface that can be implemented by any
|
|
number of frameworks:</p><pre class="programlisting">public interface Validator {
|
|
|
|
void validate(Object value) throws ValidationException;
|
|
|
|
}</pre><p>The contract is that the <code class="methodname">validate</code> method
|
|
will throw an exception if the object is invalid, and return normally if
|
|
it is valid. Spring Batch provides an out of the box
|
|
<code class="classname">ItemProcessor:</code></p><pre class="programlisting"><bean class="org.springframework.batch.item.validator.ValidatingItemProcessor">
|
|
<property name="validator" ref="validator" />
|
|
</bean>
|
|
|
|
<bean id="validator"
|
|
class="org.springframework.batch.item.validator.SpringValidator">
|
|
<property name="validator">
|
|
<bean id="orderValidator"
|
|
class="org.springmodules.validation.valang.ValangValidator">
|
|
<property name="valang">
|
|
<value>
|
|
<![CDATA[
|
|
{ orderId : ? > 0 AND ? <= 9999999999 : 'Incorrect order ID' : 'error.order.id' }
|
|
{ totalLines : ? = size(lineItems) : 'Bad count of order lines'
|
|
: 'error.order.lines.badcount'}
|
|
{ customer.registered : customer.businessCustomer = FALSE OR ? = TRUE
|
|
: 'Business customer must be registered'
|
|
: 'error.customer.registration'}
|
|
{ customer.companyName : customer.businessCustomer = FALSE OR ? HAS TEXT
|
|
: 'Company name for business customer is mandatory'
|
|
:'error.customer.companyname'}
|
|
]]>
|
|
</value>
|
|
</property>
|
|
</bean>
|
|
</property>
|
|
</bean></pre><p>This simple example shows a simple
|
|
<code class="classname">ValangValidator</code> that is used to validate an order
|
|
object. The intent is not to show Valang functionality as much as to show
|
|
how a validator could be added.</p></section><footer/></body></html> |