Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 71265ae

Browse files
Added Validator folder in the MVCPracticeAdvanced section of a small course on Java EE
1 parent 6d25ad5 commit 71265ae

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 AirportSimulatorTwo.Validator;
2+
3+
import AirportSimulatorTwo.DTO.CreateUserDto;
4+
import AirportSimulatorTwo.Entity.EntityEnum.Gender;
5+
import AirportSimulatorTwo.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 AirportSimulatorTwo.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 AirportSimulatorTwo.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 AirportSimulatorTwo.Validator;
2+
3+
public interface Validator <T>{
4+
ValidationResult isValid(T object);
5+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /