Compare commits

..

No commits in common. "9d44c12fac88601ae0693db5cbd6b4040b68283d" and "1309a553a40d244ec7ef7f68edacef7f9f1f4913" have entirely different histories.

13 changed files with 33 additions and 241 deletions

15
pom.xml
View File

@ -2,11 +2,10 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
@ -16,7 +15,7 @@
<name>spring-validation</name>
<properties>
<java.version>21</java.version>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -29,11 +28,15 @@
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>

View File

@ -1,109 +0,0 @@
{
"info": {
"_postman_id": "bb233500-0318-496e-9882-071ee5de34a0",
"name": "Spring Validation",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "16442716"
},
"item": [
{
"name": "Person",
"item": [
{
"name": "Create Person",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 400\", function () {",
" pm.response.to.have.status(400);",
"});",
"",
"pm.test(\"violations is not null\", function () {",
" var jsonData = pm.response.json();",
" pm.expect(jsonData.violations).is.not.null;",
" pm.expect(jsonData.violations[0].fieldName).to.be.oneOf([\"numberBetweenOneAndTen\", \"ipAddress\", \"name\"])",
" pm.expect(jsonData.violations[1].fieldName).to.be.oneOf([\"numberBetweenOneAndTen\", \"ipAddress\", \"name\"])",
" pm.expect(jsonData.violations[2].fieldName).to.be.oneOf([\"numberBetweenOneAndTen\", \"ipAddress\", \"name\"])",
" pm.expect(jsonData.violations[0].message).to.be.oneOf([\"Не соответствует формату IP адреса\", \"должно быть не больше 10\", \"не должно быть пустым\"])",
" pm.expect(jsonData.violations[1].message).to.be.oneOf([\"Не соответствует формату IP адреса\", \"должно быть не больше 10\", \"не должно быть пустым\"])",
" pm.expect(jsonData.violations[2].message).to.be.oneOf([\"Не соответствует формату IP адреса\", \"должно быть не больше 10\", \"не должно быть пустым\"])",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"name\": null,\n \"numberBetweenOneAndTen\": 20,\n \"ipAddress\": \"15434.250.124.\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/api/person",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"person"
]
}
},
"response": []
},
{
"name": "Get Person",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 400\", function () {",
" pm.response.to.have.status(400);",
"});",
"",
"pm.test(\"violations is not null\", function () {",
" var jsonData = pm.response.json();",
" pm.expect(jsonData.violations).is.not.null;",
" pm.expect(jsonData.violations[0].fieldName).to.be.eq(\"getById.personId\")",
" pm.expect(jsonData.violations[0].message).to.be.eq(\"должно быть не меньше 0\")",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/api/person/-1",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"person",
"-1"
]
}
},
"response": []
}
]
}
]
}

View File

@ -1,6 +1,5 @@
package org.sadtech.example.springvalidation.controller;
import jakarta.validation.ConstraintViolationException;
import org.sadtech.example.springvalidation.dto.ValidationErrorResponse;
import org.sadtech.example.springvalidation.dto.Violation;
import org.springframework.http.HttpStatus;
@ -10,6 +9,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

View File

@ -2,12 +2,7 @@ package org.sadtech.example.springvalidation.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.sadtech.example.springvalidation.dto.PersonDto;
import org.sadtech.example.springvalidation.service.PersonService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
@ -18,18 +13,16 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/person")
@Tag(name = "Пользователи системы", description = "Валидация на уровне контроллера")
public class PersonController {
private final PersonService personService;
@PostMapping
@Operation(summary = "Сохранение пользователя")
public ResponseEntity<String> save(@Valid @RequestBody PersonDto personDto) {
@ -52,15 +45,4 @@ public class PersonController {
return ResponseEntity.ok("valid");
}
@GetMapping("birthday")
@Operation(summary = "Получить пользователей в диапазоне ДР")
public ResponseEntity<List<PersonDto>> getByPersonBirthday(
@RequestParam("from") LocalDate from,
@RequestParam("to") LocalDate to
) {
return ResponseEntity.ok(
personService.getAllByHappyBirthdayBetween(from, to)
);
}
}

View File

@ -1,7 +1,6 @@
package org.sadtech.example.springvalidation.controller;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.sadtech.example.springvalidation.dto.PersonDto;
import org.sadtech.example.springvalidation.valid.Marker;
import org.springframework.http.ResponseEntity;
@ -12,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@Validated
@RestController

View File

@ -12,9 +12,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/no-valid/person")
@Tag(name = "Пользователи системы 2", description = "Валидация на уровне сервиса")
@RequiredArgsConstructor
public class PersonControllerNoValidation {
private final PersonService personService;

View File

@ -1,18 +0,0 @@
package org.sadtech.example.springvalidation.dto;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Address {
@NotBlank
private String state;
@NotBlank
private String city;
}

View File

@ -1,23 +1,23 @@
package org.sadtech.example.springvalidation.dto;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import jakarta.validation.constraints.Pattern;
import lombok.Getter;
import lombok.Setter;
import org.sadtech.example.springvalidation.valid.CapitalLetter;
import org.sadtech.example.springvalidation.valid.Marker;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Pattern;
@Entity
@Getter
@Setter
@ -46,7 +46,4 @@ public class PersonDto {
)
private String ipAddress;
@Transient
private Address address;
}

View File

@ -1,14 +1,10 @@
package org.sadtech.example.springvalidation.service;
import jakarta.validation.Valid;
import org.sadtech.example.springvalidation.dto.PersonDto;
import org.sadtech.example.springvalidation.valid.LocalDateBetween;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import javax.validation.Valid;
@Service
@Validated
@ -18,10 +14,4 @@ public class PersonService {
// do something
}
@LocalDateBetween
public List<PersonDto> getAllByHappyBirthdayBetween(LocalDate from, LocalDate to) {
System.out.println("valid");
return Collections.emptyList();
}
}

View File

@ -1,8 +1,7 @@
package org.sadtech.example.springvalidation.valid;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

View File

@ -1,8 +1,7 @@
package org.sadtech.example.springvalidation.valid;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class CapitalLetterValidator implements ConstraintValidator<CapitalLetter, String> {

View File

@ -1,23 +0,0 @@
package org.sadtech.example.springvalidation.valid;
import jakarta.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Documented
@Target(METHOD)
@Retention(RUNTIME)
public @interface LocalDateBetween {
String message() default "Дата невалидна";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

View File

@ -1,28 +0,0 @@
package org.sadtech.example.springvalidation.valid;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraintvalidation.SupportedValidationTarget;
import jakarta.validation.constraintvalidation.ValidationTarget;
import java.time.LocalDate;
@SupportedValidationTarget(ValidationTarget.PARAMETERS)
public class LocalDateBetweenValidator implements ConstraintValidator<LocalDateBetween, Object[]> {
@Override
public boolean isValid(Object[] value, ConstraintValidatorContext context) {
final Object objDateFrom = value[0];
final Object objDateTo = value[1];
if (
!(objDateFrom instanceof final LocalDate dateFrom)
|| !(objDateTo instanceof final LocalDate dateTo)
) {
throw new IllegalArgumentException("Illegal method signature, expected two parameters of type LocalDate.");
}
return dateFrom.isAfter(LocalDate.now()) && dateFrom.isBefore(dateTo);
}
}