- java.lang.Object
-
- de.mlo.dev.validation.Validator
-
- All Implemented Interfaces:
ValidationSummarizer
public class Validator extends Object implements ValidationSummarizer
TheValidatorhelps to group, chain and executeValidationStatements.
Example:public ValidationResult validate(){ return new Validator() .add(this::validateName) .add(this::validateAge) .validateAll(); } ValidationInfo validateName(){ String name = unbindName(); if(name.isBlank()){ return ValidationInfo.invalid("Name is empty"); } return ValidationInfo.valid(); } ValidationInfo validateAge(){ int age = unbindAge(); if(age <= 0){ return ValdationInfo.invalid("Age must be positive value") } return ValidationInfo.valid(); }
Logging: Depending on theValidationRunneryou choose you will get detailed information about the validation process if you set the log level for this package to debug. The used logging framework istorg.apache.logging.log4j.Logger- Author:
- mlo
-
-
Constructor Summary
Constructors Constructor Description Validator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description @NotNull Validatoradd(@Nullable ValidationStatement statement)Adds a newValidationStatementto the list of existent statements.@NotNull Validatoradd(@Nullable ValidationSummarizer validationSummarizer)Adds aValidationSummarizer.@NotNull ValidatorsetValidateAll()Executes all addedValidationStatements in the order they have been added.@NotNull ValidatorsetValidateStopOnFirstFail()Executes the addedValidationStatements in the order they have been added until the firstValidationStatementfail.@NotNull ValidatorsetValidationRunner(@NotNull ValidationRunner validationRunner)Sets a customValidationRunner.@NotNull ValidationResultvalidate()This will start the validation process.@NotNull ValidationResultvalidateStopOnFirstFail()Shortcut for
-
-
-
Method Detail
-
add
@NotNull public @NotNull Validator add(@Nullable @Nullable ValidationStatement statement)
Adds a newValidationStatementto the list of existent statements. The added statement will be appended at the end of the list. The order of the statements is maintained.
Execute the added statements whichvalidate()
Example:public ValidationResult validate(Person person){ return new Validator() .add(() -> validateName(person.getName()) .add(() -> validateAge(person.getAge()) .add(() -> validateAddress(person.getAddress()) .validate(); }
For more complex stuff like grouping and nesting you can use the otheraddfunction which accepts wholevaldiatorsandresults.- Parameters:
statement- A new statement to add to the end of the list of statements. Null values are ignored- Returns:
- An instance of this
Validatorso you can chain 'add' calls
-
add
@NotNull public @NotNull Validator add(@Nullable @Nullable ValidationSummarizer validationSummarizer)
Adds aValidationSummarizer. Asummarizercan execute multiple parts of the validation process and can aggregate the result of these.
This function allows you to nest differentvalidatorswith their own runner.
Example: Check that the given person is not null and if this is true, the persons fields will be validated allpublic ValidationResult validatePerson(Person person){ return new Validator() .add(new Validator().add(() -> validatePersonNotNull(person)) .add(new Validator() .add(() -> validateName(person.getName()) .add(() -> validateAge(person.getAge())) .validateStopOnFirstFail() }- Parameters:
validationSummarizer- AValidationSummarizerwhich can be aValidatororValidationResultfor example. If the parameter is null it will be ignored.- Returns:
- The instance of this validator
-
validate
@NotNull public @NotNull ValidationResult validate()
This will start the validation process. The execution process depends on the used runner but usually the addedstatementswill be executed in the order they have been added.
Runners:- The default runner executes all added statements
- Use
setValidateStopOnFirstFail()to apply a runner which stops if one statement fails - Use
setValidationRunner(ValidationRunner)to apply a custom runner
- Specified by:
validatein interfaceValidationSummarizer- Returns:
- The aggregated result of all executed statements
-
validateStopOnFirstFail
@NotNull public @NotNull ValidationResult validateStopOnFirstFail()
Shortcut for
Executes the addedvalidator.setValidateStopOnFirstFail().validate();ValidationStatements in the order they have been added until the firstValidationStatementfail. If a single statement fails, theValidationResult.isValid()function will returnfalse. Only if all statement passes the test the result will be valid (ValidationResult.isValid()will betrue).
The validation result also contains the result of every single statements and can aggregate all failure messages:ValidationResult.getMessage().- Returns:
- The result of the validation process. The result will contain zero or only one information which indicates that the validation failed.
-
setValidateAll
@NotNull public @NotNull Validator setValidateAll()
Executes all addedValidationStatements in the order they have been added. If a single statement fails, theValidationResult.isValid()function will returnfalse. Only if all statement passes the test the result will be valid (ValidationResult.isValid()will betrue).
The validation result also contains the result of every single statements and can aggregate all failure messages:ValidationResult.getMessage().- Returns:
- The instance of this validator
-
setValidateStopOnFirstFail
@NotNull public @NotNull Validator setValidateStopOnFirstFail()
Executes the addedValidationStatements in the order they have been added until the firstValidationStatementfail. If a single statement fails, theValidationResult.isValid()function will returnfalse. Only if all statement passes the test the result will be valid (ValidationResult.isValid()will betrue).
The validation result also contains the result of every single statements and can aggregate all failure messages:ValidationResult.getMessage().- Returns:
- An aggregated
ValidationResult
-
setValidationRunner
@NotNull public @NotNull Validator setValidationRunner(@NotNull @NotNull ValidationRunner validationRunner)
Sets a customValidationRunner. A runner has to executestatementsand has to aggregate the results of them.- Parameters:
validationRunner- A custom validation runner- Returns:
- The instance of this validator
-
-