Bean validation library for Core Java

This library uses java reflection to validation

License

License

GroupId

GroupId

com.github.enesusta
ArtifactId

ArtifactId

tasdik
Last Version

Last Version

1.7.7
Release Date

Release Date

Type

Type

jar
Description

Description

Bean validation library for Core Java
This library uses java reflection to validation
Project URL

Project URL

https://github.com/enesusta/tasdik
Source Code Management

Source Code Management

http://github.com/enesusta/tasdik

Download tasdik

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.enesusta/tasdik/ -->
<dependency>
    <groupId>com.github.enesusta</groupId>
    <artifactId>tasdik</artifactId>
    <version>1.7.7</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.enesusta/tasdik/
implementation 'com.github.enesusta:tasdik:1.7.7'
// https://jarcasting.com/artifacts/com.github.enesusta/tasdik/
implementation ("com.github.enesusta:tasdik:1.7.7")
'com.github.enesusta:tasdik:jar:1.7.7'
<dependency org="com.github.enesusta" name="tasdik" rev="1.7.7">
  <artifact name="tasdik" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.enesusta', module='tasdik', version='1.7.7')
)
libraryDependencies += "com.github.enesusta" % "tasdik" % "1.7.7"
[com.github.enesusta/tasdik "1.7.7"]

Dependencies

compile (9)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-core jar 2.10.3
com.fasterxml.jackson.core : jackson-databind jar 2.10.3
com.liferay : com.fasterxml.jackson.databind jar 2.10.0.LIFERAY-PATCHED-1
org.springframework : spring-core jar 5.0.0.RELEASE
org.springframework : spring-context jar 5.0.0.RELEASE
org.springframework : spring-web jar 5.0.0.RELEASE
org.mockito : mockito-core jar 3.0.0
com.google.auto.service : auto-service jar 1.0-rc5
org.apache.maven.scm : maven-scm-provider-gitexe jar 1.9.5

provided (1)

Group / Artifact Type Version
javax.servlet : javax.servlet-api jar 4.0.1

test (3)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.5.1
org.junit.jupiter : junit-jupiter-engine jar 5.5.1
org.hamcrest : hamcrest-all jar 1.3

Project Modules

There are no modules declared in this project.

tasdik

Travis CI w/ Logo Issues Maven Central javadoc License

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
  • @Email

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;
    }

}

Visualisation Of Implementation

Versions

Version
1.7.7
1.7.6
1.7.5
1.7.4-STABLE
1.7.3
1.7.2