Class BeanValidator<T>

java.lang.Object
com.oorian.validation.bean.BeanValidator<T>
Type Parameters:
T - the bean type to validate

public class BeanValidator<T> extends Object
Validates Java bean instances using Jakarta Bean Validation (JSR 380) annotations.

BeanValidator reads standard constraint annotations (@NotNull, @Size, @Email, @Min, @Max, etc.) from a bean class and maps them to Oorian's validation framework. This bridges the standard Java Bean Validation API with Oorian's validation UI, error messages, and field highlighting.

Supported annotations:

  • @NotNull, @NotEmpty, @NotBlank → RequiredValidator
  • @Size(min, max) → LengthValidator
  • @Min, @Max, @DecimalMin, @DecimalMax → RangeValidator
  • @Positive, @PositiveOrZero, @Negative, @NegativeOrZero → RangeValidator
  • @Pattern(regexp) → PatternValidator
  • @Email → EmailValidator
  • @Digits → NumericValidator
  • @AssertTrue, @AssertFalse → inline predicate validators

Usage:


 // Define a bean with Jakarta validation annotations
 public class User {
     @NotBlank
     private String name;

     @Email
     private String email;

     @Size(min = 8, max = 100)
     private String password;

     @Min(0) @Max(150)
     private int age;
 }

 // Validate a bean instance
 BeanValidator<User> validator = new BeanValidator<>(User.class);
 ValidationResult result = validator.validate(user);

 if (result.isInvalid()) {
     // Handle validation errors
     for (ValidationMessage msg : result.getMessages()) {
         System.out.println(msg.getText());
     }
 }

 // Get validators for a specific field (for use with ValidatedInput)
 List<Validator<?>> emailValidators = validator.getFieldValidators("email");
 

Validation groups:


 public class User {
     @NotBlank(groups = Create.class)
     private String name;

     @Email
     private String email;
 }

 // Validate only Create group
 ValidationResult result = validator.validate(user, Create.class);
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • BeanValidator

      public BeanValidator(Class<T> beanClass)
      Creates a BeanValidator for the specified bean class.

      The constructor scans all declared fields (including inherited fields) for Jakarta Bean Validation constraint annotations and maps them to Oorian validators.

      Parameters:
      beanClass - the bean class to validate
  • Method Details

    • validate

      public ValidationResult validate(T bean)
      Validates all fields of the given bean instance.

      All constraints are checked regardless of their group assignment.

      Parameters:
      bean - the bean instance to validate
      Returns:
      the merged validation result for all fields
    • validate

      public ValidationResult validate(T bean, Class<?>... groups)
      Validates the bean instance for the specified validation groups.

      Only constraints belonging to the specified groups (or with no group assignment) are checked. If no groups are specified, all constraints are checked.

      Parameters:
      bean - the bean instance to validate
      groups - the validation groups to check (null or empty for all)
      Returns:
      the merged validation result for matching constraints
    • getFieldValidators

      public List<Validator<?>> getFieldValidators(String fieldName)
      Returns the Oorian validators discovered for the specified field.
      Parameters:
      fieldName - the bean field name
      Returns:
      list of validators for the field, or an empty list if none found
    • getConstrainedFields

      public Set<String> getConstrainedFields()
      Returns all field names that have at least one constraint annotation.
      Returns:
      set of constrained field names
    • getBeanClass

      public Class<T> getBeanClass()
      Returns the bean class this validator was created for.
      Returns:
      the bean class