Validations (Constraints / Facets)

XML Schema supports specifing a number of validations rules for XML simple types (called Facets or constraints) and constraints for repetitive elements. J2XB Supports XML Facets using the MOValidationString , MOValidationNumber annotations and constraints on repetitive elements using the MOValidationCollection annotation.

Note that at this time, those annotations are only used for the XML Schema generation. The methods to read values or write values to XML do not check for the validations.

String Validations (Constraints / Facets)

The MOValidationString annotation allows to set the minimum length of strings, the maximum length and a set of patterns that the string has to match (match at least one of the patterns).

An example usage of this annotation is

@MOValidationString(minLength = 1, maxLength = 20)
public String getName() {...}

and the resulting XML Schema fragment is

<xs:element name="employeeName">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:maxLength value="20"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Number Validations (Constraints / Facets)

The MOValidationNumber annotation allows to set the minimum and maximum values for numeric attributes. The minimum and maximum can be inclusive or exclusive.

An example usage of this annotation is

@MOValidationNumber(minExclusive = "0")
public int getSalary() {...}

and the resulting XML Schema fragment is

<xs:element name="salary" minOccurs="0">
  <xs:simpleType>
    <xs:restriction base="xs:int">
      <xs:minExclusive value="0"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Collection Validations (Constraints / Facets)

The MOValidationCollection annotation allows to set the minimum and maximum number of elements in a collection or array, or repetitive XML element.

An example usage of this annotation is

@MOValidationCollection(min = 2, max = 4)
public List<ListItem2> getNinthCase() {...}

and the resulting XML Schema fragment is

<xs:element name="nine" type="ns4:IntegerList" minOccurs="2" maxOccurs="4"/>

Other related issues

XML Schema support other features that relate to Facets - enumerations, key and keyRef.

Enumerations are supported, see the types page.

Key and KeyRef are not supported by J2XB at this time.