If you’ve ever found yourself tangled up in the complexities of validating data in your Java applications, YAVI might just be the tool you’ve been looking for. YAVI, which stands for *Yet Another Validation Interface*, offers a lambda-based, type-safe approach to ensure that your data is valid. No runtime declarations, no reflection, and zero dependencies – just pure validation magic.
Why Choose YAVI?
YAVI stands apart from traditional validation frameworks like Bean Validation by offering tailored features:
- Type-safe constraints, ensuring you apply the right rules to the right data types.
- A fluent and intuitive API that makes coding validations feel like a breeze.
- Support for various object types, including Java Beans, Records, Protocol Buffers, and Immutables.
- A rich library of built-in constraints, along with ease of adding custom constraints.
- Validation capabilities for groups and arguments, reinforcing robustness before an object is even instantiated.
Simply put, YAVI is like having a personal assistant who ensures that all information enters your system correctly – safeguarding accuracy at every step.
Getting Started with YAVI
Ready to dive in? Follow these simple steps to integrate YAVI into your Java project.
Prerequisites
- Java Runtime (version 8 or higher)
- Apache Maven
Project Setup
Start by adding YAVI as a dependency in your pom.xml
file:
am.ik.yavi
yavi
0.14.1
This guide uses JUnit 5 and AssertJ for testing. You will also need to add the following dependencies:
org.junit.jupiter
junit-jupiter-api
5.9.3
test
org.assertj
assertj-core
3.24.2
test
Applying Constraints
Let’s visualize applying constraints in your code through an analogy. Imagine you are a strict librarian validating the borrowing process. You wouldn’t just allow any random book checked out. Instead, you’d have rules in place:
- Every book must have a title (manufacturer must not be null).
- A book’s ISBN should fall within a specific length (licensePlate’s length between 2 and 14).
- Each patron must have a minimum number of days to borrow (seatCount must be at least 2).
In code, you are doing something similar with YAVI by creating a Car
record and applying validations through the ValidatorBuilder
:
package com.example;
import am.ik.yavi.builder.ValidatorBuilder;
import am.ik.yavi.core.Validator;
public record Car(String manufacturer, String licensePlate, Integer seatCount) {
public static final Validator validator = ValidatorBuilder.of()
.constraint(Car::manufacturer, manufacturer, c -> c.notNull())
.constraint(Car::licensePlate, licensePlate, c -> c.notNull().greaterThanOrEqual(2).lessThanOrEqual(14))
.constraint(Car::seatCount, seatCount, c -> c.greaterThanOrEqual(2))
.build();
}
Validating Constraints
To ensure your validations work as expected, you can run tests. Just like how a librarian checks the borrowing process, you’ll create unit tests to validate our Car
class:
package com.example;
import am.ik.yavi.core.ConstraintViolations;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class CarTest {
@Test
void manufacturerIsNull() {
final Car car = new Car(null, "DD-AB-123", 4);
final ConstraintViolations violations = Car.validator.validate(car);
assertThat(violations.isValid()).isFalse();
assertThat(violations).hasSize(1);
assertThat(violations.get(0).message()).isEqualTo("manufacturer must not be null");
}
@Test
void licensePlateTooShort() {
final Car car = new Car("Morris", "D", 4);
final ConstraintViolations violations = Car.validator.validate(car);
assertThat(violations.isValid()).isFalse();
assertThat(violations).hasSize(1);
assertThat(violations.get(0).message()).isEqualTo("The size of licensePlate must be greater than or equal to 2. The given size is 1.");
}
@Test
void seatCountTooLow() {
final Car car = new Car("Morris", "DD-AB-123", 1);
final ConstraintViolations violations = Car.validator.validate(car);
assertThat(violations.isValid()).isFalse();
assertThat(violations).hasSize(1);
assertThat(violations.get(0).message()).isEqualTo("seatCount must be greater than or equal to 2");
}
@Test
void carIsValid() {
final Car car = new Car("Morris", "DD-AB-123", 2);
final ConstraintViolations violations = Car.validator.validate(car);
assertThat(violations.isValid()).isTrue();
assertThat(violations).hasSize(0);
}
}
Troubleshooting
If you encounter issues when integrating YAVI, consider the following:
- Ensure that you have added the correct dependencies to your
pom.xml
. - Check if you’ve written your validation constraints as intended.
- Ensure you are using the latest versions of Java and Maven.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
In just a breezy five minutes, you’ve explored how to begin using YAVI to add robust validations in Java applications. For an in-depth introduction, feel free to visit Using YAVI in their reference documentation.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.