Nicolas Richeton's blog

XmlField : new validation API

☕️ 1 min read
logo

XmlField 0.7 which was just released provides a new validation API, based on JSR 303, with some additions borrowed from Hibernate validation.

It is now very simple to ensure objects are valid, without complex XPath expressions or XPath duplication.

Add constraints to your interfaces :

@ResourceXPath("/database")
public interface IDatabase {
   @NotEmpty
   @FieldXPath("@name")
   String getName();

   @Size(min=1)
   @FieldXPath("tables/table")
   ITable[] getTables();
}

Validate :

IDatabase db = new XmlField().xmlToObject( xmlString, IDatabase.class );

Set<ConstraintViolation<Object>> violations = new XmlFieldValidator().validate( db );

Supported annotations :

@NotEmpty : value exists and is not blank
@Size : string length, array size
@Range : numbers between min and max
@Values : check for accepted values

Enjoy :-)

See also : Introducing XmlField : Java xml/object mapping framework