What is it?
Tasdik is bean (JavaBean, POJO)
validation library that uses Java reflection API.
Quick Installation
If you are using Maven you can add the repository by adding the following XML to your project pom.xml
file.
<dependency>
<groupId>com.github.enesusta</groupId>
<artifactId>tasdik</artifactId>
<version>1.7.7</version>
</dependency>
How could you use it?
Tasdik has 10 annotation to validate bean which uses in general purposes.
Those are:
- @False
- @True
- @Max
- @Min
- @Negative
- @Positive
- @NonNull
- @Size
- @Regex
Lets assume that we have a bean which we want to validate.
import com.github.enesusta.tasdik.fals.False;
import com.github.enesusta.tasdik.max.Max;
import com.github.enesusta.tasdik.min.Min;
import com.github.enesusta.tasdik.negative.Negative;
import com.github.enesusta.tasdik.nonnull.NonNull;
import com.github.enesusta.tasdik.positive.Positive;
import com.github.enesusta.tasdik.regex.Regex;
import com.github.enesusta.tasdik.size.Size;
import com.github.enesusta.tasdik.tru.True;
import com.github.enesusta.tasdik.validator.DefaultValidator;
import com.github.enesusta.tasdik.validator.Validator;
public class Example {
public static class Bean {
@False
private boolean mustBeFalse;
@True
private boolean mustBeTrue;
@Max(value = 10)
private int mustBeAMaximumOf10;
@Min(value = 5)
private int mustBeAMin5;
@Negative
private int mustBeNegative;
@Positive
private int mustBePositive;
@NonNull
private Object mustBeNonNull;
@Size(min = 5, max = 10)
private String numberOfCharactersMustBeBetween5and10;
@Regex(pattern = "\\d+")
private String mustBeDigit;
@Email
private String mustBeEmail;
/**
* setter/getter/constructor and other stuffs
*/
}
public static void main(String[] args) throws IllegalAccessException {
Bean bean = new Bean();
/**
* setter / getter etc on your bean
*/
Validator validator = DefaultValidator.getInstance();
boolean isValid = validator.isValid(bean);
}
}
How tasdik works?
Tasdik is based on two interface.
- Validator
- FieldValidator
Validator
There are 1 concrete class of Validator.
- DefaultValidator
public interface Validator {
boolean isValid(Object object) throws IllegalAccessException;
default boolean hasAny(boolean[] arr) {
boolean valid = true;
for (boolean b : arr) {
if (!b) {
valid = false;
break;
}
}
return valid;
}
}
FieldValidator
FieldValidator interface is heart of the instrastructe of Tasdik. There are 10 concrete class of FieldValidator.
import java.lang.reflect.Field;
public interface FieldValidator {
boolean isFieldValid(Field field) throws IllegalAccessException;
}
Those are:
- FalseFieldValidator
- TrueFieldValidator
- MaxFieldValidator
- MinFieldValidator
- NegativeFieldValidator
- PositiveFieldValidator
- NonNullFieldValidator
- SizeFieldValidator
- RegexFieldValidator
- EmailFieldValidator
As mentioned above Validator
interface has one concrete class. In this manner DefaultValidator class uses a specific class which name is FieldContext that uses Strategy pattern
.
Implementation of FieldContext
public class FieldContext {
private static FieldContext instance = null;
private Map<Class<? extends Annotation>, FieldValidator> contextMap = new HashMap<>(9);
private FieldContext(final Object o) {
initialize(o);
}
private void initialize(final Object object) {
contextMap.put(Email.class, new EmailFieldValidator(object));
contextMap.put(Min.class, new MinFieldValidator(object));
contextMap.put(Max.class, new MaxFieldValidator(object));
contextMap.put(Negative.class, new NegativeFieldValidator(object));
contextMap.put(Positive.class, new PositiveFieldValidator(object));
contextMap.put(Regex.class, new RegexFieldValidator(object));
contextMap.put(Size.class, new SizeFieldValidator(object));
contextMap.put(True.class, new TrueFieldValidator(object));
contextMap.put(False.class, new FalseFieldValidator(object));
}
public static FieldContext getInstance(final Object o) {
if (instance == null)
instance = new FieldContext(o);
return instance;
}
public boolean isValid(final Field field) throws IllegalAccessException {
boolean isValid = true;
for (Annotation annotation : field.getAnnotations())
if (!contextMap.get(annotation.annotationType()).isFieldValid(field))
isValid = false;
return isValid;
}
}