Create java object

First, you have to create a java object which you want to validate. It can be any java object from String to hierarchical structures (maps and JavaBeans). Let's take a String java object " john" as an example. Note, the space in the string.

Define SOVT components

Define your components in your SOVT library. E.g. my_sovt.xml file located in the classpath.

Some default components are already defined in the default SOVT library: sovt.xml

            <validator
               id="required"
               class="org.sovt.impl.RequiredValidator"
               msg="Enter value!"
               />

            <validator
               id="length"
               class="org.sovt.impl.LengthValidator"
               msg="Value length should be between {0} and {1}!"
               />

            <transformer
               id="trim"
               class="org.sovt.impl.TrimTransformer"
               />
         

Let's define "userName" inspector as an example.

            <inspector id="userName">
               <transformer ref="trim"/>
               <validator ref="required"/>
               <validator ref="length" min="4" max="8"/>
            </inspector>
         

This inspector performs three actions.

  1. It trims the object (object is converted to String first).
  2. It checks if the object is not null nor empty string.
  3. It checks if the length of the String is between 4 and 8.

Validate your object

Now you can perform object validation and transformation.

            String toValidate = " john";
            ValidationResult results = new ValidationResult();
            Inspector inspector = SovtLibrary.getInstance().getInspector("userName");
            toValidate = inspector.inspect(
                  toValidate,
                  results
            );
            if (result.isValid()) {
               System.out.println("Success for: " + toValidate);
            } else {
               System.out.println("Error: " + result);
            }
         

More to come

This example is really simple. There will be better examples soon on how to work with hierarchical object structures.