Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete The Circle #73

Merged
merged 10 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Avaje Validator (site docs coming soon)

[![Build](https://github.com/avaje/avaje-validator/actions/workflows/build.yml/badge.svg)](https://github.com/avaje/avaje-validator/actions/workflows/build.yml)
<img src="https://img.shields.io/maven-central/v/io.avaje/avaje-validator.svg?label=Maven%20Central">
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/avaje/avaje-inject/blob/master/LICENSE)
[![Discord](https://img.shields.io/discord/1074074312421683250?color=%237289da&label=discord)](https://discord.gg/Qcqf9R27BR)

Reflection-free pojo validation via apt source code generation. A light (~85kb + generated code) source code generation style alternative to Hibernate Validation. (code generation vs reflection)

- Annotate java classes with `@Valid` (or use `@Valid.Import` for types we "don't own" or can't annotate)
- Annotate java classes with `@Valid` (or use `@ImportValidPojo` for types we "don't own" or can't annotate)
- `avaje-validator-generator` annotation processor generates Java source code to write validation classes
- Supports Avaje/Jakarta/Javax Constraint Annotations
- Group Support
- Groups Support
- Composable Contraint Annotations
- loading and interpolating error messages (with multiple Locales) through ResourceBundles
- Getter Validation
- Method parameter validation (via Avaje Inject AOP only)
- Method parameter validation (via Avaje Inject AOP only at the moment)

# Quick Start

Expand Down Expand Up @@ -49,7 +54,7 @@ for each type annotated with `@Valid`. These will be automatically registered wi
when it is started using a service loader mechanism.

```java
@Json
@Valid
public class Leyndell {
@NotBlank
private String street;
Expand Down
209 changes: 104 additions & 105 deletions blackbox-test/pom.xml
Original file line number Diff line number Diff line change
@@ -1,109 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator-parent</artifactId>
<version>0.10</version>
</parent>

<artifactId>validator-blackbox-test</artifactId>
<name>validator blackbox test</name>
<description>test module</description>

<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<java.release>17</java.release>
</properties>

<dependencies>

<!-- for testing fields with third party annotations -->
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>8.0.0.Final</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>validator-constraints</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator-inject-plugin</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator-generator</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
<version>${inject.version}</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<version>${inject.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-test</artifactId>
<version>${inject.version}</version>
<scope>test</scope>
</dependency>
<!-- test dependencies -->

<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>

</dependencies>


<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator-parent</artifactId>
<version>0.11</version>
</parent>

<artifactId>validator-blackbox-test</artifactId>
<name>validator blackbox test</name>
<description>test module</description>

<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<java.release>17</java.release>
</properties>

<dependencies>

<!-- for testing fields with third party annotations -->
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>8.0.0.Final</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>validator-constraints</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator-generator</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
<version>${inject.version}</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<version>${inject.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-test</artifactId>
<version>${inject.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-http-api</artifactId>
<version>1.45</version>
</dependency>

<!-- test dependencies -->

<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>

</dependencies>


<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import io.avaje.http.api.Validator;
import io.avaje.validation.inject.aspect.ValidateMethod;
import jakarta.inject.Singleton;
import jakarta.validation.constraints.NotEmpty;
Expand All @@ -10,6 +11,9 @@

@Singleton
public class MethodTest {

public MethodTest(Validator apiValidator) {}

@NotNull
@ValidateMethod
String test(@NotEmpty List<@NotNull String> str, @Positive int inty, String regular) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.avaje.validation.http;
package example.avaje.typeuse;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.avaje.http.api.ValidationException;
import io.avaje.http.api.Validator;
import io.avaje.inject.BeanScope;
import io.avaje.inject.spi.Builder;
import io.avaje.inject.spi.Module;
import io.avaje.inject.test.InjectTest;
import jakarta.inject.Inject;

@InjectTest
class DefaultValidatorProviderTest {
Module mod =
new Module() {
Expand All @@ -24,28 +26,14 @@ public Class<?>[] classes() {
@Override
public void build(Builder builder) {}
};
private Validator validator;

@Inject private Validator validator;

@BeforeAll
static void setLocale() {
System.setProperty("validation.locale.default", "en-us");
}

@BeforeEach
void before() {
System.setProperty("validation.locale.default", "en-us");
final var v =
io.avaje.validation.Validator.builder()
.add(CrewMate.class, CrewMateValidationAdapter::new)
.build();
this.validator =
BeanScope.builder()
.modules(mod)
.bean(io.avaje.validation.Validator.class, v)
.build()
.get(Validator.class);
}

@Test
void test() {
assertAll(() -> validator.validate(new CrewMate("hmm"), "en-GB,en;q=0.9,en-US;q=0.8,de;q=0.7"));
Expand Down
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.avaje</groupId>
Expand All @@ -9,7 +10,7 @@

<groupId>io.avaje</groupId>
<artifactId>avaje-validator-parent</artifactId>
<version>0.10</version>
<version>0.11</version>

<packaging>pom</packaging>
<name>validator parent</name>
Expand All @@ -27,6 +28,7 @@
<java.version>17</java.version>
<java.release>17</java.release>
<inject.version>9.3</inject.version>
<http.version>1.45</http.version>
</properties>

<modules>
Expand Down
13 changes: 2 additions & 11 deletions validator-constraints/pom.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator-parent</artifactId>
<version>0.10</version>
<version>0.11</version>
</parent>
<artifactId>validator-constraints</artifactId>
<dependencies>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* The annotated element must be false. Supported types are {@code boolean} and {@code Boolean}.
*
* <p>{@code null} elements are considered valid.
*
* @author Emmanuel Bernard
*/
@Constraint
@Target({METHOD, FIELD, ANNOTATION_TYPE, PARAMETER, TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* The annotated element must be true. Supported types are {@code boolean} and {@code Boolean}.
*
* <p>{@code null} elements are considered valid.
*
* @author Emmanuel Bernard
*/
@Constraint
@Target({METHOD, FIELD, ANNOTATION_TYPE, PARAMETER, TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Loading