diff --git a/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java b/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java index 09f29e0..b3534e6 100644 --- a/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java +++ b/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java @@ -2,6 +2,7 @@ package org.sadtech.example.springvalidation.dto; import lombok.Getter; import lombok.Setter; +import org.sadtech.example.springvalidation.valid.CapitalLetter; import javax.persistence.Column; import javax.persistence.Entity; @@ -26,6 +27,7 @@ public class PersonDto { private Long id; @NotBlank + @CapitalLetter private String name; @Min(1) diff --git a/src/main/java/org/sadtech/example/springvalidation/valid/CapitalLetter.java b/src/main/java/org/sadtech/example/springvalidation/valid/CapitalLetter.java new file mode 100644 index 0000000..c966cff --- /dev/null +++ b/src/main/java/org/sadtech/example/springvalidation/valid/CapitalLetter.java @@ -0,0 +1,24 @@ +package org.sadtech.example.springvalidation.valid; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target({FIELD}) +@Retention(RUNTIME) +@Constraint(validatedBy = CapitalLetterValidator.class) +@Documented +public @interface CapitalLetter { + + String message() default "должно быть с большой буквы"; + + Class[] groups() default {}; + + Class[] payload() default {}; + +} diff --git a/src/main/java/org/sadtech/example/springvalidation/valid/CapitalLetterValidator.java b/src/main/java/org/sadtech/example/springvalidation/valid/CapitalLetterValidator.java new file mode 100644 index 0000000..1ea360d --- /dev/null +++ b/src/main/java/org/sadtech/example/springvalidation/valid/CapitalLetterValidator.java @@ -0,0 +1,16 @@ +package org.sadtech.example.springvalidation.valid; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +public class CapitalLetterValidator implements ConstraintValidator { + + @Override + public boolean isValid(String value, ConstraintValidatorContext context) { + if (value != null && !value.isEmpty()) { + return Character.isUpperCase(value.charAt(0)); + } + return true; + } + +}