Here is the validation.xml
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.1.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping" version="1.1">
<default-package>com.esq.rbac.model</default-package>
<bean class="Tenant" ignore-annotations="true">
...
<field name="ivrPin">
<constraint annotation="javax.validation.constraints.Size">
<element name="min">6</element>
<element name="max">6</element>
<message>IVR Pin must be 6 digit numeric</message>
</constraint>
<constraint annotation="javax.validation.constraints.Pattern">
<element name="regexp">[0-9]+</element>
</constraint>
</field>
</bean>
During execution getting error saying
c.e.u.imports.Main [run] - HV000115: Error parsing mapping file.
javax.validation.ValidationException: HV000115: Error parsing mapping file.
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a:
Invalid content was found starting with element 'message'. One of '{"http://jboss.org/xml/ns/javax/validation/mapping":element}' is expected.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
How to include the error message properly
You just need to make one change by placing your message
directly after constraint annotation
tag, before the element
tags.
<bean class="Tenant" ignore-annotations="true">
...
<field name="ivrPin">
<constraint annotation="javax.validation.constraints.Size">
<message>IVR Pin must be 6 digit numeric</message>
<element name="min">6</element>
<element name="max">6</element>
</constraint>
The reason for this is as follows:
If you look at the xsd file for validation-mapping:
validation-mapping-1.1.xsd
<xs:complexType name="constraintType">
<xs:sequence>
<xs:element type="xs:string" name="message" minOccurs="0"/>
<xs:element type="map:groupsType"
name="groups"
minOccurs="0"/>
<xs:element type="map:payloadType"
name="payload"
minOccurs="0"/>
<xs:element type="map:elementType"
name="element"
maxOccurs="unbounded"
minOccurs="0"/>
</xs:sequence>
<xs:attribute type="xs:string" name="annotation" use="required"/>
</xs:complexType>
It is using xs:sequence
tag which specifies that the child elements must appear in a sequence. Refer element sequence. Here message attribute comes before other attributes like groups, payload or element.