Home

Awesome

valdr Bean Validation

Build Status Coverage Status Maven Central License

Bean Validation (JSR 303) plugin for valdr

<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> <!-- **Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*--> <!-- END doctoc generated TOC please keep comment here to allow auto update -->

Offering

valdr Bean Validation parses Java model classes for Bean Validation constraints (aka JSR 303 annotations) and extracts their information into a JSON document to be used by valdr. This allows to apply the exact same validation rules on the server and on the client.

Features

Use

Check out the demo for usage samples of both CLI client and Servlet.

<dependency>
  <groupId>com.github.valdr</groupId>
  <artifactId>valdr-bean-validation</artifactId>
  <version>see-latest-version-at-the-top-of-this-page</version>
</dependency>

CLI client

Example of Maven integration:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>${exec-maven-plugin.version}</version>
      <executions>
        <execution>
          <id>process-bean-validation-annotations</id>
          <phase>process-classes</phase>
          <goals>
            <goal>java</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <mainClass>com.github.valdr.cli.ValdrBeanValidation</mainClass>
        <arguments>
          <!-- optional, if omitted valdr-bean-validation.json is expected at the root of the class path -->
          <argument>-cf</argument>
          <argument>my-config.json</argument>
          <!-- optional, overrides any 'outputFile' which may have been set in the above config file -->
          <argument>-outputFile</argument>
          <argument>${basedir}/src/main/webapp/validation/validation.json</argument>
        </arguments>
      </configuration>
    </plugin>
  </plugins>
</build>

Servlet

Example of web.xml:

<servlet>
  <servlet-name>valdr Bean Validation Servlet</servlet-name>
  <servlet-class>com.github.valdr.ValidationRulesServlet</servlet-class>
  <!-- if omitted valdr-bean-validation.json is expected at the root of the class path -->
  <init-param>
    <param-name>configFile</param-name>
    <param-value>my-config.json</param-value>
  </init-param>
</servlet>

Dependency on valdr

valdr Bean Validation is dependent on valdr in two ways:

To indicate which valdr version a specific valdr Bean Validation version supports there's a simple rule: the first digit of the valdr Bean Validation version denotes the supported valdr version. Version 1.x will support valdr 1. This means that valdr Bean Validation 1.x+1 may introduce breaking changes over 1.x because the second version digit kind-of represents the "major" version.

Mapping of Bean Validation constraints to valdr constraints

The BuiltInConstraint.java enum defines the mapping of Bean Validation constraints to valdr constraints.

Bean ValidationvaldrComment
NotNullrequired
Minmin
Maxmax
Sizesize
Digitsdigits
PatternpatternJava regex pattern is transformed to JavaScript pattern
Futurefuture
Pastpast
Emailemail
URLurlproprietary Hibernate Validator (not in Bean Validation spec)

Support

Ask a question on Stack Overflow and tag it with valdr-bean-validation.

License

MIT © Netcetera AG