Skip to content

Commit

Permalink
java: move jsr303
Browse files Browse the repository at this point in the history
  • Loading branch information
l-lin committed Jun 26, 2024
1 parent df9b291 commit d2bbb45
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 32 deletions.
42 changes: 42 additions & 0 deletions java/jsr303/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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>
<artifactId>dev-cheat-sheet</artifactId>
<groupId>lin.louis</groupId>
<version>${revision}</version>
</parent>

<artifactId>jsr303</artifactId>

<properties>
<spring-boot.version>3.3.1</spring-boot.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;

@Target({METHOD, FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lin.louis.jsr303;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class CustomValidator implements ConstraintValidator<CustomValidation, String> {
@Override
Expand Down
40 changes: 40 additions & 0 deletions java/jsr303/src/main/java/lin/louis/jsr303/JSR303Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package lin.louis.jsr303;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public class JSR303Foo {

@NotBlank(message = "The firstName should not be blank!")
private String firstName;

@Size(min = 1, max = 5, message = "The lastName should have 5 characters max!")
private String lastName;

@CustomValidation
private String foo;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getFoo() {
return foo;
}

public void setFoo(String foo) {
this.foo = foo;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package lin.louis.jsr303;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertFalse;

@Slf4j
class JSR303ValidationTest {

@Test
Expand All @@ -23,11 +21,11 @@ void testValidate() {
Set<ConstraintViolation<JSR303Foo>> constraintViolations = validator.validate(foo);

assertFalse(constraintViolations.isEmpty());
constraintViolations.forEach(constraint -> log.info(
"{}.{} {}",
constraintViolations.forEach(constraint -> System.out.println(
"%s.%s %s".formatted(
constraint.getRootBeanClass().getSimpleName(),
constraint.getPropertyPath(),
constraint.getMessage()
));
)));
}
}
19 changes: 0 additions & 19 deletions java/src/test/java/lin/louis/jsr303/JSR303Foo.java

This file was deleted.

0 comments on commit d2bbb45

Please sign in to comment.