Whether you are using the default validators or have implemented custom ones,
you can customize the behavior that occurs when validation fails, as well as create custom errors,
for limit structures, limits, and incidents by adding beans that implement the
ILimitStructureValidationErrorHandler, ILimitValidationErrorHandler, and IIncidentValidationErrorHandler
interfaces, respectively.
Pre-requisite
These interfaces provide the explicit type parameter for the generically-typed IValidationErrorHandler
interface:
DefaultValidationErrorHandler<T> is a generic implementation of IValidationErrorHandler and is
used by ILimitStructureValidationErrorHandler, ILimitValidationErrorHandler, and
IIncidentValidationErrorHandler.
This default implementation does the following:
- It collects all errors that occur during validation.
- It prints an error report in the logs from the
postValidation() method of the validator.
- If
throwException is true when reset() is called, it returns a LimitsDefinitionalValidationRuntimeException. This is a ProblemDetail object that
contains all the validation errors in its validationErrors property, including the index and field name for each error.
If you are implementing a custom error handler we recommend extending this class.
To implement a custom limit error handler that logs all errors as they occur and continues processing, you can do the
following:
1. Create the Spring Bean
2. Import the Spring Bean
Once the bean is created, you need to import it to the project. Once done,
Spring will use the custom bean for handling validation errors.
IValidationError
In addition to customizing the behavior when a validation error occurs, you can also customize the validation errors themselves.
The IValidationError interface allows you to define a custom validation error:
The default implementation of IValidationError is DefaultValidationError. This default is generic for all types of components that
are validated, and uses Lombok’s @AllArgsConstructor and @Getter annotations to provide its functionality.
To create a custom validation error specific to incidents that had some structured formatting and
default action, you can do the following: