title: Get JAXB to enforce XSD restrictions when marshalling to XML
body: <p>I have an XML schema that restricts movie ratings to have a value between 1 and 10 inclusive. However the JAXB Binding allows me save a movie with a rating of 11. Is there a way to get JAXB to throw an exception during the marshal if a value does not adhere to the restrictions in the XSD?</p>
  
  <p>Here is the XSD:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  
      &lt;xsd:simpleType name="RatingType"&gt;
          &lt;xsd:annotation&gt;
              &lt;xsd:documentation xml:lang="en"&gt;A rating between 1 and 10 inclusive&lt;/xsd:documentation&gt;
          &lt;/xsd:annotation&gt;
          &lt;xsd:restriction base="xsd:float"&gt;
              &lt;xsd:minInclusive value="1.0" /&gt;
              &lt;xsd:maxInclusive value="10.0" /&gt;
          &lt;/xsd:restriction&gt;
      &lt;/xsd:simpleType&gt;
  
      &lt;xsd:complexType name="MovieRatingType"&gt;
          &lt;xsd:sequence&gt;
              &lt;xsd:element name="Title" type="xsd:string" minOccurs="1" maxOccurs="1" /&gt;
              &lt;xsd:element name="Rating" type="RatingType" minOccurs="1" maxOccurs="1" /&gt;
          &lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
  
      &lt;xsd:complexType name="MoviesListType"&gt;
          &lt;xsd:sequence&gt;
              &lt;xsd:element name="Movie" type="MovieRatingType" maxOccurs="unbounded" /&gt;
          &lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
  
      &lt;xsd:element name="Movies" type="MoviesListType" /&gt;
  
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>import generated.MovieRatingType;
  import generated.MoviesListType;
  import generated.ObjectFactory;
  import java.io.File;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBElement;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Marshaller;
  
  public class RestrictionTest {
  
      public static void main(String[] args) {
  
          ObjectFactory factory = new ObjectFactory();
  
          MovieRatingType movie = new MovieRatingType();
          movie.setTitle("Terminator");
          movie.setRating(11.3f); //&lt;--- rating set to higher than the allowed value
  
          MoviesListType movieList = factory.createMoviesListType();
          movieList.getMovie().add(movie);
  
          JAXBElement&lt;MoviesListType&gt; moviesListJAXB = factory.createMovies(movieList);
  
          try {
  
              File file = new File("C:\\Users\\perezsmithf\\Desktop\\dev\\java\\scratch\\src\\output.xml");
              JAXBContext jaxbContext = JAXBContext.newInstance(MoviesListType.class);
              Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  
              // output pretty printed
              jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  
              jaxbMarshaller.marshal(moviesListJAXB, file); //&lt;--- at this point it should throw an exception because the rating is too high
              jaxbMarshaller.marshal(moviesListJAXB, System.out);
  
          } catch (JAXBException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>