Easy Validation

This project contains the api for validation

Лицензия

Лицензия

Группа

Группа

at.meks
Идентификатор

Идентификатор

easy-validation
Последняя версия

Последняя версия

1.0.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

Easy Validation
This project contains the api for validation
Ссылка на сайт

Ссылка на сайт

https://github.com/meks77/easy-validation
Система контроля версий

Система контроля версий

https://github.com/meks77/easy-validation

Скачать easy-validation

Как подключить последнюю версию

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

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
org.apache.commons : commons-lang3 jar 3.7

test (5)

Идентификатор библиотеки Тип Версия
junit : junit jar 4.12
org.easytesting : fest-assert-core jar 2.0M10
org.mockito : mockito-core jar 2.9.0
org.powermock : powermock-api-mockito2 jar 1.7.4
org.powermock : powermock-module-junit4 jar 1.7.4

Модули Проекта

Данный проект не имеет модулей.

Sonarcloud Status Sonarcloud Status Sonarcloud Status Sonarcloud Status Sonarcloud Status Sonarcloud Status Sonarcloud Status Sonarcloud Status Sonarcloud Status

easy-validation

Very easy way to validate simple and complex objects by using an fluent interface.

Goals

  • No 3rd party dependencies
  • Very small artefact size
  • clean and natural api

Exmples

Simply validate a method argument and throw an IllegalArgumentException

If you want to validate just a method argument you can do this by using the module easy-validation-args.

validate().that(arg1)
        .withMessage(() -> "arg1")
        .isNotBlank()
        .hasMinLength(30)
        .matches(value -> value.contains("whatIsExpected"));

Commonly when you implement a validation the code would look like this:

if (StringUtils.isBlank(arg1)) {
    throw new IllegalArgumentException("arg1");
}
if (arg1.length() <= 30) {
    throw new IllegalArgumentException("arg1");
}
if (arg1.contains("whatIsExpected") {
    throw new IllegalArgumentException("arg1);
}

Just image you have 20+ different input values you have to validate. The code is growing very fast and it will get hard to maintain is it grows.

You also have the possibility to set a seperate message per validation.

validate().that(arg1)
        .withMessage(() -> "arg1 mustn't be blank").isNotBlank()
        .withMessage(() -> "arg1 must have at leat 30 chars").hasMinLength(30)
        .withMessage(() -> "arg1 must contain \"whatIsExpected\"").matches(value -> value.contains("whatIsExpected"));

Do more validations to report in any way you want

Expect the case you want to validate the input to be not blank and it mustn't contain whitespace, but you want to report it with different error messages.

List<String> occuredErrorKeys = new ArrayList<>();
String validatedValue = " ";
Validator.reportTo(occuredErrorKeys::add)
        .verify(validatedValue)
        .usingKey("notBlank").matches(StringUtils::isNotBlank)
        .and().usingKey("whitespaces").matches(StringUtils::doesNotContainWhitespace)
        .and().usingKey("minLength").matches(value -> StringUtils.length(value) > 10);

In the list occuredErrorKeys the errors notBlank, whitespaces and minLength will be added. With this information you can report the error however you want.

Throw the Exception you want

If you have the need to throw your own exception in case of the first violation, e.g. for throwing an error code, just go ahead like this:

Validator.stopOnFirstError()
        .throwing(() -> new MyCheckedExceptionWithErrorCode(999))
        .verify(validatedValue)
        .matches(StringUtils::isNotBlank)
        .and(StringUtils::doesNotContainWhitespace)

Matchers

First I thought using commons-lang3 is fine. But I realized that commons-lang3 size is about 500 KB. Furthermore: How to avoid dependency clashes?

Therefore I decided to use my own matchers. Currently in version 2.0.0-RC2 the size is ~ 6 KB. Furthermore the whole validation has no dependency to any 3rd party library. So you can use allways as long you use at least java 8.

History

While developing the first release I followed the main concept of using lambda combined with a fluent interface is from Joel Planes, what can be found here.

But I was getting tired to always invoke the method test and afterwards the method throwIfInvalid.

Thanks to Eric Evans input I developed it from scratch with the possibility to decide if I want to throw an exception or not easy, without duplicating so much method signatures. I hope you like the result.

Версии библиотеки

Версия
1.0.0
1.0.0-RC2
1.0.0-RC1
1.0.0-M3
1.0.0-M2
1.0.0-M1.1