The SpEL Validator extends the Jakarta Validation framework to allow flexible field validation using SpEL expressions. It integrates with Spring MVC's native validation mechanism. For the validation to trigger, a class must be annotated with @SpelValid and the method parameter must be annotated with @Valid or @Validated.
Execution Flow:
@Valid/@Validated triggers the standard Java validation mechanism.- The framework detects the
@SpelValid annotation on the class, which activates the SpelValidator. SpelValidator#isValid() is called, which invokes the SpelValidExecutor.SpelValidExecutor scans the class fields for @SpelConstraint annotations (e.g., @SpelSize, @SpelNotNull, @SpelFuture).- The executor evaluates the
condition and group settings to determine if the specific constraint should run. - The corresponding
SpelConstraintValidator executes the validation logic. - Errors are collected as
FieldError objects and converted into standard ConstraintViolation objects, allowing them to be handled by Spring's BindingResult or returned as 400 Bad Request responses.
@Valid/@Validated
↓
@SpelValid <- Activates SpelValidator
↓
SpelValidator#isValid() <- Calls SpelValidExecutor
↓
SpelValidExecutor <- Scans fields, parses expressions, calls constraint validators
↓
SpelConstraintValidator <- Executes constraint validation
↓
FieldError <- Collects error information
↓
ConstraintViolation <- Converts to standard validation framework structure