File tree Expand file tree Collapse file tree 4 files changed +63
-0
lines changed
MVCPractice/src/AirportSimulator/Validator Expand file tree Collapse file tree 4 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ package AirportSimulator .Validator ;
2+
3+ import AirportSimulator .DTO .CreateUserDto ;
4+ import AirportSimulator .Entity .EntityEnum .Gender ;
5+ import AirportSimulator .Util .LocalDateFormatter ;
6+ import lombok .AccessLevel ;
7+ import lombok .NoArgsConstructor ;
8+
9+ @ NoArgsConstructor (access = AccessLevel .PRIVATE )
10+ public class CreateUserValidator implements Validator <CreateUserDto >{
11+
12+ private static final CreateUserValidator INSTANCE = new CreateUserValidator ();
13+
14+ public static CreateUserValidator getInstance () {
15+ return INSTANCE ;
16+ }
17+ /* Проверяем на валидность - правильность введенную дату и пол */
18+ @ Override
19+ public ValidationResult isValid (CreateUserDto object ) {
20+ ValidationResult validationResult = new ValidationResult ();
21+ if (!LocalDateFormatter .isValid (object .getBirthday ())){
22+ validationResult .addErrors (Error .of ("invalid.birthday" ,"Birthday is invalid" ));
23+ }
24+ if (object .getGender () == null || Gender .valueOf (object .getGender ()) == null ) {
25+ validationResult .addErrors (Error .of ("invalid.gender" ,"Gender is invalid" ));
26+ }
27+
28+ return validationResult ;
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ package AirportSimulator .Validator ;
2+ /* Класс описывающий ошибки при валидации */
3+ import lombok .Value ;
4+
5+ @ Value (staticConstructor = "of" )
6+ public class Error {
7+ private String code ;
8+ private String message ;
9+ }
Original file line number Diff line number Diff line change 1+ package AirportSimulator .Validator ;
2+
3+ import lombok .Getter ;
4+
5+ import java .util .ArrayList ;
6+ import java .util .List ;
7+
8+ public class ValidationResult {
9+ @ Getter
10+ private List <Error > errors = new ArrayList <>();
11+
12+ public void addErrors (Error find_error ) {
13+ this .errors .add (find_error );
14+ }
15+
16+ public boolean isValid (){
17+ return errors .isEmpty ();
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package AirportSimulator .Validator ;
2+
3+ public interface Validator <T >{
4+ ValidationResult isValid (T object );
5+ }
You can’t perform that action at this time.
0 commit comments