New API proposal
Proposal:
- It should provide a generic validator that can be used all the times on fields
- The Validation annotation must be scope aware
Standard Validators
- Repeatable annotation @Validation
- Optional
scope
parameter. The validators will always run, by order of declaration, if a scope is not defined. - The
value
parameter will reference a validator class. This class needs to implement theCustomValidator
API and there will be 2 ways of creating and using such these validators: 0. The class is tentatively fetched from the SpringApplicationContext
. If it is registered as a Spring bean then it is used as such; 0. If it is not a Spring bean, a new instance is created with the default constructor. If this is not possible, anIllegalStateException
is thrown.
@Validation(NotNullValidation.class)
@Validation(scope = AppValidationScopes.EUROPE, value = EuropeanCountriesValidation.class);
@Validation(scope = AppValidationScopes.WORLD, value = WorldwideCountriesValidation.class);
private String name;
Semantic Validators
Specific validator annotations might be created to shorten the code needed to validate specific fields. If this is the case, first define the custom validator annotation:
@Retention(RUNTIME)
@ValidationScope(AppValidationScopes.EUROPE)
@ValidationStrategy(EuropeanCountriesValidation.class)
public @interface EuropeCountry {
}
The validation strategy will be run exactly in the same way as with the standard validators. These custom annotations will also be run by order and can be mixed with standard ones.
@ValidationScope is optional and, if not defined, all validation triggers will run the validator.
Validation Contexts
Contexts are moments where validations are triggered according to their scopes. Validations can be triggered by annotating elements with @ValidationContext
. Elements annotated with this can be either:
- Classes (where all method executions will have their arguments validated if they have any field annotated with a validation annotation)
- Methods (where only the annotated method will have their arguments validated, if any)
Contexts have a value
parameter that represents all the scopes that will be validated, by order of definition.