• Stars
    star
    645
  • Rank 67,990 (Top 2 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Yet Another Validation for Java (A lambda based type safe validation framework)

YAVI (Yet Another ValIdation)

Apache 2.0 Maven Central Javadocs Actions Status Codacy Badge Codacy Badge

YAVI Logo

YAVI (pronounced jɑ-vάɪ) is a lambda based type safe validation for Java.

Why YAVI?

YAVI sounds as same as a Japanese slang "YABAI (ヤバイ)" that means awesome or awful depending on the context (like "Crazy"). If you use YAVI, you will surely understand that it means the former.

The concepts are

  • No reflection!
  • No (runtime) annotation!
  • Not only Java Beans!
  • Zero dependency!

If you are not a fan of Bean Validation, YAVI will be an awesome alternative.

YAVI has the following features:

  • Type-safe constraints, unsupported constraints cannot be applied to the wrong type
  • Fluent and intuitive API
  • Constraints on any object. Java Beans, Records, Protocol Buffers, Immutables and anything else.
  • Lots of powerful built-in constraints
  • Easy custom constraints
  • Validation for groups, conditional validation
  • Validation for arguments before creating an object
  • Support for API and combination of validation results and validators that incorporate the concept of functional programming

See the reference documentation for details.

Presentations

Getting Started

This content is derived from https://hibernate.org/validator/documentation/getting-started/

Welcome to YAVI.

The following paragraphs will guide you through the initial steps required to integrate YAVI into your application.

Prerequisites

Project set up

In order to use YAVI within a Maven project, simply add the following dependency to your pom.xml:

<dependency>
    <groupId>am.ik.yavi</groupId>
    <artifactId>yavi</artifactId>
    <version>0.13.0</version>
</dependency>

This tutorial uses JUnit 5 and AssertJ. Add the following dependencies as needed:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.9.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.23.1</version>
    <scope>test</scope>
</dependency>

Applying constraints

Let’s dive into an example to see how to apply constraints:

Create src/main/java/com/example/Car.java and write the following code.

package com.example;

import am.ik.yavi.builder.ValidatorBuilder;
import am.ik.yavi.core.Validator;

public class Car {
    private final String manufacturer;

    private final String licensePlate;

    private final int seatCount;

    public static final Validator<Car> validator = ValidatorBuilder.<Car>of()
            .constraint(Car::getManufacturer, "manufacturer", c -> c.notNull())
            .constraint(Car::getLicensePlate, "licensePlate", c -> c.notNull().greaterThanOrEqual(2).lessThanOrEqual(14))
            .constraint(Car::getSeatCount, "seatCount", c -> c.greaterThanOrEqual(2))
            .build();

    public Car(String manufacturer, String licencePlate, int seatCount) {
        this.manufacturer = manufacturer;
        this.licensePlate = licencePlate;
        this.seatCount = seatCount;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public String getLicensePlate() {
        return licensePlate;
    }

    public int getSeatCount() {
        return seatCount;
    }
}

The ValidatorBuilder.constraint is used to declare the constraints which should be applied to the return values of getter for the Car instance:

  • manufacturer must never be null
  • licensePlate must never be null and must be between 2 and 14 characters long
  • seatCount must be at least 2

You can find the complete source code on GitHub.

Validating constraints

To perform a validation of these constraints, you use a Validator instance. To demonstrate this, let’s have a look at a simple unit test:

Create src/test/java/com/example/CarTest.java and write the following code.

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

Validator instances are thread-safe and may be reused multiple times.

The validate() method returns a ConstraintViolations instance, which you can iterate in order to see which validation errors occurred. The first three test methods show some expected constraint violations:

  • The notNull() constraint on manufacturer is violated in manufacturerIsNull()
  • The greaterThanOrEqual(int) constraint on licensePlate is violated in licensePlateTooShort()
  • The greaterThanOrEqual(int) constraint on seatCount is violated in seatCountTooLow()

If the object validates successfully, validate() returns an empty ConstraintViolations as you can see in carIsValid(). You can also check if the validation was successful with the ConstraintViolations.isValid method.

Where to go next?

That concludes the 5 minutes tour through the world of YAVI. If you want a more complete introduction, it is recommended to read "Using YAVI" in the reference document.

Required

  • Java 8+

License

Licensed under the Apache License, Version 2.0.

More Repositories

1

oauth2-sso-demo

OIDC SSO Demo with Spring Boot + Spring Security + Spring Cloud Gateway
Java
358
star
2

rsc

RSocket Client CLI (RSC) that aims to be a curl for RSocket
Java
233
star
3

hajiboot-samples

書籍「はじめてのSpring Boot」のサポートページ
Shell
134
star
4

spring-webapp-template

Template Web Application Project for Spring (Spring MVC+Spring Data JPA+Spring Security)
Java
97
star
5

spring-boot-db-samples

Spring Boot + Data access libraries
Java
69
star
6

spring-socks

A bad microservices (Spring Version of https://microservices-demo.github.io)
Java
57
star
7

spring-boot-blank

Spring Boot Maven archetype
Java
52
star
8

spring-jpetstore

A web application built on top of Spring, Spring MVC, MyBatis 3, and Spring Security forked from http://mybatis.github.io/spring/sample.html
Java
48
star
9

from-zero-to-hero-with-rest-and-oauth2

47
star
10

demo-http2

HTTP2 w/ Spring Boot + Undertow
Java
47
star
11

travis-ci-maven-deploy-skelton

How to deploy to Maven Central by Travis CI
Shell
45
star
12

reactjs-tutorial-spring-boot

React.js Tutorial with Server Side Rendering by Spring Boot and Nashorn
JavaScript
42
star
13

spring-security-oauth-workshop

Spring Security OAuth Workshop
41
star
14

spring-boot-docker-blank

Spring Boot + Docker blank project
Shell
40
star
15

cloud-native-workshop

1 day workshop to learn Spring Boot + Spring Cloud + Pivotal Cloud Foundry
37
star
16

cf-workshop

Cloud Foundry Workshop
37
star
17

springmvc-jpa-blank

Web App Example Using Spring MVC & Spring Data JPA
Java
36
star
18

concourse-ci-demo

Concourse CI + GitHub + Pivotal Tracker + Cloud Foundry Integration Demo
Shell
36
star
19

timeflake4j

Java implementation of Timeflake
Java
35
star
20

spring-cloud-stream-tutorial

Spring Cloud Stream Tutorial
31
star
21

demo-spring-boot-1.3

Spring Boot 1.3 tiny samples
Java
29
star
22

functional-spring-cookbook

Functional Spring Cookbook
Java
28
star
23

jsug-shop

CSS
24
star
24

aws-apa

Java Library for Amazon Product Advertising API using JAX-WS
Java
23
star
25

jjugccc-handson

Java
22
star
26

blog-handson

22
star
27

clj-gae-ds

a Datastore library on Google App Engine for Clojure
Clojure
21
star
28

beansviz-spring-boot-actuator

Spring Boot Actuator Endpoint to visualize beans hierarchy
Java
21
star
29

concourse-workshop

WIP
20
star
30

spring-boot-batch-multi-jobs

Shell
19
star
31

demo-jwt

Demo Spring Security + JWT
Java
18
star
32

ltsv4j

Labeled Tab Separated Value(http://ltsv.org/) manipulator for Java
Java
17
star
33

vanilla-spring-webflux-fn-blank

Vanilla Spring WebFlux.fn Blank (Small Footpint Spring WebFlux.fn)
Java
17
star
34

k8s-keycloak-oidc-helper

Keycloak Kubernetes OpenID Connect helper
Shell
17
star
35

gateway

API Gateway for *.ik.am
HTML
15
star
36

cf-grafana

Grafana on Cloud Foundry
Shell
14
star
37

jsug-spring-boot-handson

Python
14
star
38

cf-vault

Hashicorp Vault on Cloud Foundry
Shell
14
star
39

clj-aws-ecs

Clojure Client Library for Amazon Product Advertising API
Clojure
14
star
40

devsecops-demo

Simple DevSecOps Demo
Shell
13
star
41

clj-gae-blank

a blank project for Clojure on Google App Engine
Clojure
13
star
42

spring-boot-camp-instruction

Spring Bootキャンプのハンズオン資料です
Python
13
star
43

demo-boot-graal

Java
12
star
44

voicetext4j

Java Client Library for VoiceText Web API (https://cloud.voicetext.jp/webapi)
Java
12
star
45

uaa-ui

Web UI for Cloud Foundry UAA
JavaScript
11
star
46

spring-boot-jersey-blank

Maven archetype to crate blank project for Spring Boot + Jersery (JAX-RS)
Java
11
star
47

categolj

CategoLJ is a categorizer written in clojure. This is a simple blog system.
JavaScript
10
star
48

thrift-server

Spring Boot + Thrift
Java
10
star
49

apahe-geode-on-kubernetes

Apache Geode on Kubernetes
Shell
10
star
50

spring-mvc-msgpack-sample

Spring MVC + MessagePack sample
Java
9
star
51

jqiita

Qiita API Java Client
Java
9
star
52

code-server

VS Code Server on K8s
Dockerfile
9
star
53

spring-integration-tcp-sample

Java
9
star
54

learning-rsocket-using-rsc

Java
9
star
55

spring-boot-gae-blank

Maven archetype to create a GAE-configured maven project for Spring Boot Application
Java
9
star
56

nexus-boshrelease

BOSH Release for Nexus Repository Manager
Shell
7
star
57

spring-boot-actuator-dashboard

Unlocking Cloud Foundry actuator support for non-CF apps
Java
7
star
58

beyond-the-spring-tettei-nyumon

Beyond the "Spring徹底入門"
7
star
59

marked4j

marked(https://github.com/chjj/marked) for Java which is a markdown parser and compiler
JavaScript
7
star
60

prometheus-kustomize

7
star
61

java-cnb-builder

Cloud Native Buildpacks for Java
7
star
62

elm-spring-boot-blank

Blank multi project for Spring Boot + Elm
Java
6
star
63

spring-boot-demo-jvm-languages

Spring Boot Hello World project using some JVM languages
Groovy
6
star
64

backbone-wine-cellar

Spring MVC + JPA implementation of The Wine Cellar application (https://github.com/ccoenraets/backbone-jax-cellar)
JavaScript
6
star
65

cf-eureka-server

Eureka Server on Cloud Foundry
Shell
6
star
66

shared-mysql-service-broker

Open Service Broker API for an existing shared MySQL
Java
6
star
67

javaee7-first-tutorial

Java EE 7 Tutorial http://www.slideshare.net/makingx/netbeansjavaee7-first-tutorial
Java
6
star
68

rd-clj

the Reverse Dicrtionary of Clojure
JavaScript
6
star
69

csng

CSNG is an annotation processor that simply generates property names as constants.
Java
6
star
70

hajiboot2-samples

"はじめるSpring Boot 2"のサンプルコード
Java
6
star
71

spring-boot-demo

Spring Boot Demo
Java
6
star
72

pks-master-gateway

Java
6
star
73

sonarqube-on-cf

How to deploy SonarQube on Cloud Foundry
5
star
74

getting-started-with-spring-boot

Handson contents to learn Spring Boot
Shell
5
star
75

opentelemetry-javaagent-buildpack

Cloud Native Buildpack for OpenTelemetry Java Agent
Shell
5
star
76

kubernetes-route-definition-locator

This project provides a Kubernete native way to define RouteDefinitions for Spring Cloud Gateway using CRD.
Java
5
star
77

cf-kong

Kong on Cloud Foundry (with many hacks)
Shell
5
star
78

demo-oauth2

Spring Boot and Spring Security OAuth2 Tiny Demo
Java
5
star
79

finagle-spring

Examples of Finagle with Spring
Java
5
star
80

mrs

Meeting room Reservation System
Java
5
star
81

demo-observability

Java
5
star
82

new-controller

Lambda based extensible "New Controller" for Spring MVC insipired by Spark Java, Siden (aka Sinatra like framework)
Java
5
star
83

demo-micrometer

https://demo-micrometer.cfapps.io/actuator/info
Java
5
star
84

zipkin-boshrelease

Zipkin BOSH Release
Shell
5
star
85

minimal-webflux

Minimal Spring WebFlux.fn stack
Java
5
star
86

ldap-simple-ui

A web UI for updating a LDAP user
Java
5
star
87

cdi-spring-data

Sample for the CDI integration of Spring Data JPA
Java
5
star
88

categolj-java

Micro Blog System using Spring MVC3 + MongoDB
JavaScript
5
star
89

jqgrid-support

jqgrid-support is a library for jqGrid (http://www.trirand.com/blog/). Especially with Spring MVC3.
Java
5
star
90

graalvm-springmvc-blank

Maven archetype to create a pre-configured maven project for a Spring MVC + GraalVM application.
Shell
5
star
91

demo-webflux-protobuf

Java
4
star
92

hello-kotlin-js

Kotlin
4
star
93

jsug-spring-cloud

Shell
4
star
94

blog-services

Microservices for my blog application
Shell
4
star
95

blog.ik.am

Blog Contents
4
star
96

demo-restdocs-openapi

A sample project that generates OpenAPI doc from test code
Java
4
star
97

cfcr-aws

How to deploy Cloud Foundry Container Runtime (formerly known as Kubo/Kubernete on BOSH) on AWS
HCL
4
star
98

demo-jasper-report-ja

Shell
4
star
99

logspout-k8s

Shell
4
star
100

demo-foundationdb

Demo application using FoundationDB + Spring WebFlux.fn
Java
4
star