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)

Each of these interfaces contain four methods which provide explicit type parameters for the generically-typed methods in the IValidator interface:

public interface ILimitStructureValidator extends IValidator<LimitStructureDTO> {

    @Override
    boolean validate(LimitStructureDTO limitStructureDTO);

    @Override
    boolean validateAll(Collection<LimitStructureDTO> limitStructureDTOS);

    @Override
    void postValidation(Collection<LimitStructureDTO> validatedLimitStructureDTOs);

    @Override
    IValidationErrorHandler<LimitStructureDTO> getValidationErrorHandler();
}
public interface ILimitValidator extends IValidator<LimitDTO> {

    @Override
    boolean validate(LimitDTO limitDTO);

    @Override
    boolean validateAll(Collection<LimitDTO> limitDTOS);

    @Override
    void postValidation(Collection<LimitDTO> validatedLimitDTOs);

    @Override
    IValidationErrorHandler<LimitDTO> getValidationErrorHandler();
}
public interface IIncidentValidator extends IValidator<IncidentDTO> {

    @Override
    boolean validate(IncidentDTO incidentDTO);

    @Override
    boolean validateAll(Collection<IncidentDTO> incidentDTOs);

    @Override
    void postValidation(Collection<IncidentDTO> validatedIncidentDTOs);

    @Override
    IValidationErrorHandler<IncidentDTO> getValidationErrorHandler();
}

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;

    @Override
    public boolean validate(LimitDTO limitDTO) {
        // Custom validation logic
        return true;
    }

    @Override
    public boolean validateAll(List<LimitDTO> limitDTOs) {
        // Custom validation logic
        return true;
    }
    
    @Override
    public void postValidation(Collection<LimitDTO> validatedLimitDTOs) {
        // Custom post-validation logic
    }

    @Override
    public IValidationErrorHandler<LimitDTO> getValidationErrorHandler() {
        return validationErrorHandler;
    }
}

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.