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

update/bean-validation-with-spring-boot #308

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion spring-boot/validation/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
.settings
.springBeans
.sts4-cache
bin/

### IntelliJ IDEA ###
.idea
Expand All @@ -23,4 +24,4 @@
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/.nb-gradle/
44 changes: 18 additions & 26 deletions spring-boot/validation/build.gradle
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
buildscript {
ext {
springBootVersion = '2.3.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
plugins {
id 'java'
id "io.freefair.lombok" version "8.6"
id 'org.springframework.boot' version '3.3.0'
id 'io.spring.dependency-management' version '1.1.5'
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'io.reflectoring'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 11
version = '0.0.1'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

repositories {
mavenCentral()
}

dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-configuration-processor')
runtimeOnly('com.h2database:h2')
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.junit.jupiter:junit-jupiter-engine:5.0.1')

// these dependencies are needed when running with Java 11, since they
// are no longer part of the JDK
implementation('javax.xml.bind:jaxb-api:2.3.1')
implementation('org.javassist:javassist:3.23.1-GA')
}

test{
tasks.named('test') {
useJUnitPlatform()
}
868 changes: 144 additions & 724 deletions spring-boot/validation/deps.txt

Large diffs are not rendered by default.

Binary file modified spring-boot/validation/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#Tue Feb 06 12:27:20 CET 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
package io.reflectoring.validation;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Pattern;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
public class Input {

@Id
@GeneratedValue
private Long id;

@Min(1)
@Max(10)
@Min(value = 1, message = "{number.invalid}")
@Max(value = 10, message = "{number.invalid}")
private int numberBetweenOneAndTen;

// Note that this is actually not a valid IP address pattern, since
// it allows values greater than 255 per octet.
@Pattern(regexp = "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$")
@Pattern(regexp = "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$", message = "{ip-address.invalid}")
private String ipAddress;

public int getNumberBetweenOneAndTen() {
return numberBetweenOneAndTen;
}

public void setNumberBetweenOneAndTen(int numberBetweenOneAndTen) {
this.numberBetweenOneAndTen = numberBetweenOneAndTen;
}

public String getIpAddress() {
return ipAddress;
}

public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package io.reflectoring.validation;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;

import io.reflectoring.validation.service.OnCreate;
import io.reflectoring.validation.service.OnUpdate;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
public class InputWithCustomValidator {

Expand All @@ -30,27 +33,4 @@ public class InputWithCustomValidator {
@Column
private String ipAddress;

public int getNumberBetweenOneAndTen() {
return numberBetweenOneAndTen;
}

public void setNumberBetweenOneAndTen(int numberBetweenOneAndTen) {
this.numberBetweenOneAndTen = numberBetweenOneAndTen;
}

public String getIpAddress() {
return ipAddress;
}

public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package io.reflectoring.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;

@Target({ FIELD })
@Target(FIELD)
@Retention(RUNTIME)
@Constraint(validatedBy = IpAddressValidator.class)
@Documented
public @interface IpAddress {

String message() default "{IpAddress.invalid}";
String message() default "{ip-address.invalid}";

Class<?>[] groups() default { };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
package io.reflectoring.validation;

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

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class IpAddressValidator implements ConstraintValidator<IpAddress, String> {

private static final Pattern IP_ADDRESS_PATTERN = Pattern.compile("^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$");

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
Pattern pattern = Pattern.compile("^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$");
Matcher matcher = pattern.matcher(value);
try {
if (!matcher.matches()) {
Matcher matcher = IP_ADDRESS_PATTERN.matcher(value);
boolean isValidIpAddress = matcher.matches();

if (isValidIpAddress) {
isValidIpAddress = isValidOctets(matcher);
}
return isValidIpAddress;
}

private boolean isValidOctets(Matcher matcher) {
for (int i = 1; i <= 4; i++) {
int octet = Integer.parseInt(matcher.group(i));
if (octet < 0 || octet > 255) {
return false;
} else {
for (int i = 1; i <= 4; i++) {
int octet = Integer.valueOf(matcher.group(i));
if (octet > 255) {
return false;
}
}
return true;
}
} catch (Exception e) {
return false;
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.reflectoring.validation;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@ProgrammerStereotype
public class ProgrammerRegisterationRequest {

@NotBlank
private String programmingLanguage;

@NotBlank
private String favoriteIDE;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.reflectoring.validation;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

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

@Documented
@Target(TYPE)
@Retention(RUNTIME)
@Constraint(validatedBy = ProgrammerStereotypeValidator.class)
public @interface ProgrammerStereotype {

String message() default "Stereotype violation detected! IDE and language not vibing.";

Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.reflectoring.validation;

import java.util.List;

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

public class ProgrammerStereotypeValidator implements ConstraintValidator < ProgrammerStereotype, ProgrammerRegisterationRequest > {

private static final List <String> UNWORTHY_JAVA_IDES = List.of("Notepad", "Netbeans");

@Override
public boolean isValid(ProgrammerRegisterationRequest request, ConstraintValidatorContext context) {
if (request.getProgrammingLanguage().equalsIgnoreCase("Java")) {
if (UNWORTHY_JAVA_IDES.contains(request.getFavoriteIDE())) {
return false;
}
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.reflectoring.validation;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Validated
@ConfigurationProperties(prefix = "io.reflectoring.jwt")
public class RequiredConfigurationProperties {

@NotBlank
private String privateKey;

@NotNull
@Positive
private Integer validityMinutes;

}
Loading
Loading