Adding Custom Validators
You can customize the validation performed when creating or updating limit structures, limits, and incidents by adding beans
that implement the ILimitStructureValidator
, ILimitValidator
, and IIncidentValidator
interfaces, respectively.
Here’s how to do this:
1. Create the Spring Bean(s)
These interfaces provide the explicit type parameter for the generically-typed IValidator
interface:
public interface ILimitStructureValidator extends IValidator<LimitStructureDTO> {}
public interface ILimitValidator extends IValidator<LimitDTO> {}
public interface IIncidentValidator extends IValidator<IncidentDTO> {}
To implement a custom validator, create a class that extends the appropriate interface and override its methods. A custom
implementation of ILimitValidator
could look like this:
@Primary
@Component
public class MyLimitValidator implements ILimitValidator {
@Autowired
IValidationErrorHandler<LimitDTO> validationErrorHandler;
@Getter private List<LimitDTO> validObjects = new ArrayList<>();
@Getter private List<LimitDTO> invalidObjects = new ArrayList<>();
@Override
public boolean validate(LimitDTO limitDTO) {
// Custom validation logic
return true;
}
@Override
public void preValidation(Collection<LimitDTO> limitDTOs) {
// Custom pre-validation logic
}
@Override
public void postValidation(Collection<LimitDTO> validatedLimitDTOs) {
// Custom post-validation logic
}
@Override
boolean validateTuple(Object[] tuple, int lineNumber) {
// Custom tuple validation logic
return true;
}
@Override
public IValidationErrorHandler<LimitDTO> getValidationErrorHandler() {
return validationErrorHandler;
}
@Override
public void reset(boolean throwException) {
validObjects.clear();
invalidObjects.clear();
validationErrorHandler.getValidationErrors().clear();
}
}
note
Note that the hasErrors
method is not overridden in the custom validator. The default implementation of this method in IValidator
is sufficient for most use cases.
note
Note the inclusion of the @Primary
annotation, which tells Spring to use MyLimitValidator
instead
of DefaultLimitValidator
.
2. Import the Spring Bean
Once the bean is created, import it into the project. See Importing Spring Beans into the Project on how to do this. Once done, Spring will use the custom bean when validating the relevant object type.