Hibernate Validator 4.0.0 Beta1

Posted by    |      

The first beta version of Hibernate Validator - 4.0.0 Beta1 - is now available. This version is based on JSR 303 Specification 1.0.CR2[1].

New in this release is the ability to configure Hibernate Validator via XML (see section 7 of the specification[1]). There are two types of configuration files. Firstly, validation.xml which allows to programmatically configure your Bean Validation framework. Here is an example, which specifically states the Bean Validation Provider and message interpolator. It also specifies the location of further mapping files:

<?xml version="1.0" encoding="UTF-8"?>
<validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <default-provider>org.hibernate.validation.engine.HibernateValidatorConfiguration</default-provider>
    <message-interpolator>org.hibernate.validation.engine.ResourceBundleMessageInterpolator</message-interpolator>
    <constraint-mapping>META-INF/validation/order-constraints.xml</constraint-mapping>
    <constraint-mapping>META-INF/validation/user-constraints.xml</constraint-mapping>
    <property name="org.hibernate.validator.test">foobar</property>
</validation-config>

The second type of XML configuration files are the actual mapping file which let you declare constraints declarations. The XML matches closely the annotation declaration approach. user-constraints.xml as used in the above example could look like this:

<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
                     xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
    <default-package>org.hibernate.validation.engine.xml</default-package>
    <bean class="User" ignore-annotations="false">
        <class ignore-annotations="true">
            <group-sequence>
                <value>User</value>
                <value>Optional</value>
            </group-sequence>
            <constraint annotation="org.hibernate.validation.engine.xml.ConsistentUserInformation">
                <message>Message defined in xml</message>
                <groups>
                    <value>javax.validation.groups.Default</value>
                </groups>
                <element name="stringParam">foobar</element>
            </constraint>
        </class>
        <field name="lastname">
            <constraint annotation="javax.validation.constraints.Pattern">
                <message>Last name has to start with with a capital letter.</message>
                <element name="regexp">^[A-Z][a-z]+</element>
            </constraint>
        </field>
        <field name="creditcard">
            <valid/>
        </field>
        <getter name="firstname" ignore-annotations="true">
            <constraint annotation="javax.validation.constraints.Size">
                <message>Size is limited!</message>
                <groups>
                    <value>org.hibernate.validation.engine.xml.TestGroup</value>
                    <value>javax.validation.groups.Default</value>
                </groups>
                <element name="max">30</element>
            </constraint>
        </getter>
    </bean>
    <constraint-definition annotation="org.hibernate.validation.engine.xml.ConsistentUserInformation">
        <validated-by include-existing-validators="false">
            <value>org.hibernate.validation.engine.xml.CustomConsistentUserValidator</value>
        </validated-by>
    </constraint-definition>
</constraint-mappings>

with a matching User entity of :

public class User {

	private String firstname;
	private String lastname;
	private CreditCard creditcard;
	private String phoneNumber;

        // standard getter and setter follow
        ...
}

This first beta release is nearly feature complete now, except some minor discrepancies between implementation and specification due to ongoing specification work. We are aiming at least one more beta release to completely align implementation and specification. Work on this has already started and the outcome will be a Bean Validation TCK. The foundation for the TCK is build on the JSR 299 TCK framework. Thanks guys ;-) Of course we will also try to complete our reference documentation as fast as possible.

Any feedback for the release is welcome.

Enjoy!


Back to top