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<LimitStructure> {}
public interface ILimitValidator extends IValidator<Limit> {}
public interface IIncidentValidator extends IValidator<Incident> {}

To implement a custom validator, create a class that implements the appropriate interface and override its methods. A custom implementation of ILimitValidator could look like this:

@Component
public class MyLimitValidator implements ILimitValidator {

    @Autowired
    IValidationErrorHandler<Limit> validationErrorHandler;

    @Getter private List<Limit> validObjects = new ArrayList<>();
    @Getter private List<Limit> invalidObjects = new ArrayList<>();

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

    @Override
    public void preValidation(Collection<Limit> limits) {
        // Custom pre-validation logic
    }

    @Override
    public void postValidation(Collection<Limit> validatedLimits) {
        // Custom post-validation logic
    }

    @Override
    boolean validateTuple(Object[] tuple, int lineNumber) {
        // Custom tuple validation logic
        return true;
    }

    @Override
    public IValidationErrorHandler<Limit> 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.

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.