クラス Envali
java.lang.Object
org.thinkit.framework.envali.Envali
public final class Envali extends Object
Envali
is a powerful validator that provides common and intuitive
validation process for entity fields.
It is very easy and intuitive to use, you just need to annotate the fields of
the entity to be validated with such as RequireNonNull
and
RequireNonEmpty
and other annotations as follows.
Once you have annotated the fields of the entity to be validated, then
implement the ValidatableEntity
interface. This interface has no
processing, but this marker interface required when using the
validate(org.thinkit.framework.envali.entity.ValidatableEntity)
method.
For RequireRangeTo
, RequireRangeFromTo
,
RequireStartWith
, RequireEndWith
you need to create a content
file that defines your expectations and map them with the
ParameterMapping
annotation.
The above description can be summarized as follows.
Define the entities to be validated:
import org.thinkit.framework.envali.catalog.ErrorType;
import org.thinkit.framework.envali.annotation.ParameterMapping;
import org.thinkit.framework.envali.annotation.RequireNonNull;
import org.thinkit.framework.envali.annotation.RequirePositive;
import org.thinkit.framework.envali.annotation.RangeFromTo;
import org.thinkit.framework.envali.annotation.NestedEntity;
import org.thinkit.framework.envali.entity.ValidatableEntity;
import org.thinkit.framework.envali.result.ValidationResult;
import org.thinkit.framework.envali.result.BusinessError;
@ParameterMapping(content = "EnvaliContent")
public class ConcreteEntity implements ValidatableEntity {
@RequireNonNull
private String literal;
@RequirePositive( errorType = ErrorType.RECOVERABLE, message = "The number is negative!")
private int positive;
@RequireRangeFromTo( errorType = ErrorType.UNRECOVERABLE, message = "The number is out of range!")
private int number;
@NestedEntity
private ConcreteNestedEntity concreteNestedEntity;
}
Then just run thevalidate(org.thinkit.framework.envali.entity.ValidatableEntity)
method:ValidationResult validationResult = Envali.validate(concreteEntityObject); // Returns true if there is no error, otherwise false if (validationResult.hasError()) { // Returns the list of business errors associated with specified validatable entity // Returns the empty list if there is no error associated with specified validatable entity List<BusinessError> businessErrors = validationResult.getError(ConcreteEntity.class); for (BusinessError businessError : businessErrors) { if (businessError.isRecoverable()) { // do something for recoverable error Syetem.out.println(businessError.getMessage()); } else if (businessError.isUnrecoverable()) { // do something for unrecoverable error Syetem.out.println(businessError.getMessage()); } } }
- 導入されたバージョン:
- 1.0.0
-
メソッドの概要
修飾子とタイプ メソッド 説明 static ValidationResult
validate(ValidatableEntity entity)
Analyzes each annotation set to an entity object for validation and verifies the validity of the field's value.
-
メソッドの詳細
-
validate
Analyzes each annotation set to an entity object for validation and verifies the validity of the field's value.- パラメータ:
entity
- The entity object to be validated that implements theValidatableEntity
interface- 戻り値:
- The validation result includes business errors
- 例外:
NullPointerException
- Ifnull
is passed as an argumentUnsupportedOperationException
- When an unexpected operation is detected during the reflection process
-