Validating all annotated fields of an instance is as trivial as merely calling Shell.validate() and passing it the instance you want validated. Here's a complete exmple:
import com.planetalia.dynject.injection.Environment;
import com.planetalia.dynject.injection.Shell;
import com.planetalia.dynject.validation.ErrorMap;
import com.planetalia.dynject.validation.Length;
public class SampleClass {
@Length(min=0,max=5)
private String validStringField = "ABCDE"
@Length(max=4)
private String invalidStringField1 = "ABCDEF";
@Length(min=10)
private String invalidStringField2 = "ABCDEF";
public static void main(String[] args){
SampleClass object = new SampleClass();
ErrorMap errors = Shell.validate(object);
System.out.println(errors);
}
}
See the main index for a list of all validating annotations you can use.
Once you have the map of errors (returned as an
ErrorMap), you can check if some field is invalid simply by calling errors.containsKey(fieldName) . For example:
if (errors.containsKey("customer"))
System.out.println("The customer is invalid");
You can also retrieve the full list of errors by invoking the get(fieldName) method. It will return a list of
ErrorMessage objects. Each ErrorMessage object contains full information about which field failed falidation, why, which annotation caused the error to be reported, and so forth.