Skip to content

Commit e76da56

Browse files
committed
Added Validator folder in the MVCPractice section of a small course on Java EE
1 parent 5c13975 commit e76da56

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package AirportSimulator.Validator;
2+
3+
public interface Validator <T>{
4+
ValidationResult isValid(T object);
5+
}

0 commit comments

Comments
 (0)