-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: 입력 형식과 관련된 검증은 Dto 내부에서 하게 한다. #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ef8a532
refactor: 서비스의 대학 선택 검증을 DTO에서 수행하도록 변경
Gyuhyeok99 c0496bd
refactor: 대학 지망 검증 에러 메시지를 상수로 분리
Gyuhyeok99 5567618
test: 기존 서비스 통합테스트에서 대학 선택 유효성 검사 테스트로 변경
Gyuhyeok99 eee9500
feat: 기본 응답메시지 수정
Gyuhyeok99 f7909cd
feat: 대학 선택 검증 메시지를 ErrorCode enum으로 통합
Gyuhyeok99 64bc751
style: 불필요한 개행 제거
Gyuhyeok99 a36eb45
feat: 대학 선택 필수값 검증 로직 통합
Gyuhyeok99 3e1e853
refactor: 검증 로직 리팩토링 및 가독성 개선
Gyuhyeok99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
src/main/java/com/example/solidconnection/application/dto/ApplyRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,17 @@ | ||
| package com.example.solidconnection.application.dto; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record ApplyRequest( | ||
|
|
||
| @NotNull(message = "gpa score id를 입력해주세요.") | ||
| Long gpaScoreId, | ||
|
|
||
| @NotNull(message = "language test score id를 입력해주세요.") | ||
| Long languageTestScoreId, | ||
|
|
||
| @Valid | ||
| UniversityChoiceRequest universityChoiceRequest | ||
| ) { | ||
| } |
5 changes: 2 additions & 3 deletions
5
src/main/java/com/example/solidconnection/application/dto/UniversityChoiceRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,10 @@ | ||
| package com.example.solidconnection.application.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
| import com.example.solidconnection.custom.validation.annotation.ValidUniversityChoice; | ||
|
|
||
| @ValidUniversityChoice | ||
| public record UniversityChoiceRequest( | ||
| @NotNull(message = "1지망 대학교를 입력해주세요.") | ||
| Long firstChoiceUniversityId, | ||
|
|
||
| Long secondChoiceUniversityId, | ||
| Long thirdChoiceUniversityId) { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
.../java/com/example/solidconnection/custom/validation/annotation/ValidUniversityChoice.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.example.solidconnection.custom.validation.annotation; | ||
|
|
||
| import com.example.solidconnection.custom.validation.validator.ValidUniversityChoiceValidator; | ||
| import jakarta.validation.Constraint; | ||
| import jakarta.validation.Payload; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target(ElementType.TYPE) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Constraint(validatedBy = ValidUniversityChoiceValidator.class) | ||
| public @interface ValidUniversityChoice { | ||
|
|
||
| String message() default "유효하지 않은 지망 대학 선택입니다."; | ||
| Class<?>[] groups() default {}; | ||
| Class<? extends Payload>[] payload() default {}; | ||
| } |
62 changes: 62 additions & 0 deletions
62
...m/example/solidconnection/custom/validation/validator/ValidUniversityChoiceValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package com.example.solidconnection.custom.validation.validator; | ||
|
|
||
| import com.example.solidconnection.application.dto.UniversityChoiceRequest; | ||
| import com.example.solidconnection.custom.validation.annotation.ValidUniversityChoice; | ||
| import jakarta.validation.ConstraintValidator; | ||
| import jakarta.validation.ConstraintValidatorContext; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static com.example.solidconnection.custom.exception.ErrorCode.DUPLICATE_UNIVERSITY_CHOICE; | ||
| import static com.example.solidconnection.custom.exception.ErrorCode.FIRST_CHOICE_REQUIRED; | ||
| import static com.example.solidconnection.custom.exception.ErrorCode.THIRD_CHOICE_REQUIRES_SECOND; | ||
|
|
||
| public class ValidUniversityChoiceValidator implements ConstraintValidator<ValidUniversityChoice, UniversityChoiceRequest> { | ||
|
|
||
| @Override | ||
| public boolean isValid(UniversityChoiceRequest request, ConstraintValidatorContext context) { | ||
| context.disableDefaultConstraintViolation(); | ||
|
|
||
| if (isFirstChoiceNotSelected(request)) { | ||
| context.buildConstraintViolationWithTemplate(FIRST_CHOICE_REQUIRED.getMessage()) | ||
| .addConstraintViolation(); | ||
| return false; | ||
| } | ||
|
|
||
| if (isThirdChoiceWithoutSecond(request)) { | ||
| context.buildConstraintViolationWithTemplate(THIRD_CHOICE_REQUIRES_SECOND.getMessage()) | ||
| .addConstraintViolation(); | ||
| return false; | ||
| } | ||
|
|
||
| if (isDuplicate(request)) { | ||
| context.buildConstraintViolationWithTemplate(DUPLICATE_UNIVERSITY_CHOICE.getMessage()) | ||
| .addConstraintViolation(); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private boolean isFirstChoiceNotSelected(UniversityChoiceRequest request) { | ||
| return request.firstChoiceUniversityId() == null; | ||
| } | ||
|
|
||
| private boolean isThirdChoiceWithoutSecond(UniversityChoiceRequest request) { | ||
| return request.thirdChoiceUniversityId() != null && request.secondChoiceUniversityId() == null; | ||
| } | ||
|
|
||
| private boolean isDuplicate(UniversityChoiceRequest request) { | ||
| Set<Long> uniqueIds = new HashSet<>(); | ||
| return Stream.of( | ||
| request.firstChoiceUniversityId(), | ||
| request.secondChoiceUniversityId(), | ||
| request.thirdChoiceUniversityId() | ||
| ) | ||
| .filter(Objects::nonNull) | ||
| .anyMatch(id -> !uniqueIds.add(id)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...ample/solidconnection/custom/validation/validator/ValidUniversityChoiceValidatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package com.example.solidconnection.custom.validation.validator; | ||
|
|
||
| import com.example.solidconnection.application.dto.UniversityChoiceRequest; | ||
| import jakarta.validation.ConstraintViolation; | ||
| import jakarta.validation.Validation; | ||
| import jakarta.validation.Validator; | ||
| import jakarta.validation.ValidatorFactory; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| import static com.example.solidconnection.custom.exception.ErrorCode.DUPLICATE_UNIVERSITY_CHOICE; | ||
| import static com.example.solidconnection.custom.exception.ErrorCode.FIRST_CHOICE_REQUIRED; | ||
| import static com.example.solidconnection.custom.exception.ErrorCode.THIRD_CHOICE_REQUIRES_SECOND; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @DisplayName("대학 선택 유효성 검사 테스트") | ||
| class ValidUniversityChoiceValidatorTest { | ||
|
|
||
| private static final String MESSAGE = "message"; | ||
|
|
||
| private Validator validator; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); | ||
| validator = factory.getValidator(); | ||
| } | ||
|
|
||
| @Test | ||
| void 정상적인_지망_선택은_유효하다() { | ||
| // given | ||
| UniversityChoiceRequest request = new UniversityChoiceRequest(1L, 2L, 3L); | ||
|
|
||
| // when | ||
| Set<ConstraintViolation<UniversityChoiceRequest>> violations = validator.validate(request); | ||
|
|
||
| // then | ||
| assertThat(violations).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void 첫_번째_지망만_선택하는_것은_유효하다() { | ||
| // given | ||
| UniversityChoiceRequest request = new UniversityChoiceRequest(1L, null, null); | ||
|
|
||
| // when | ||
| Set<ConstraintViolation<UniversityChoiceRequest>> violations = validator.validate(request); | ||
|
|
||
| // then | ||
| assertThat(violations).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void 두_번째_지망_없이_세_번째_지망을_선택하면_예외_응답을_반환한다() { | ||
| // given | ||
| UniversityChoiceRequest request = new UniversityChoiceRequest(1L, null, 3L); | ||
|
|
||
| // when | ||
| Set<ConstraintViolation<UniversityChoiceRequest>> violations = validator.validate(request); | ||
|
|
||
| // then | ||
| assertThat(violations) | ||
| .extracting(MESSAGE) | ||
| .contains(THIRD_CHOICE_REQUIRES_SECOND.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void 첫_번째_지망을_선택하지_않으면_예외_응답을_반환한다() { | ||
| // given | ||
| UniversityChoiceRequest request = new UniversityChoiceRequest(null, 2L, 3L); | ||
|
|
||
| // when | ||
| Set<ConstraintViolation<UniversityChoiceRequest>> violations = validator.validate(request); | ||
|
|
||
| // then | ||
| assertThat(violations) | ||
| .isNotEmpty() | ||
| .extracting(MESSAGE) | ||
| .contains(FIRST_CHOICE_REQUIRED.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void 대학을_중복_선택하면_예외_응답을_반환한다() { | ||
| // given | ||
| UniversityChoiceRequest request = new UniversityChoiceRequest(1L, 1L, 2L); | ||
|
|
||
| // when | ||
| Set<ConstraintViolation<UniversityChoiceRequest>> violations = validator.validate(request); | ||
|
|
||
| // then | ||
| assertThat(violations) | ||
| .isNotEmpty() | ||
| .extracting(MESSAGE) | ||
| .contains(DUPLICATE_UNIVERSITY_CHOICE.getMessage()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isValidChoice로 분리하신 점이 좋네요👍 addPropertyNode로 문제 필드 알려주는 것도 좋을 것 같습니다.
처럼 메서드를 나누어 검증하는 것도 좋을 것 같네요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
확인하면서 1번째 대학 없이 2번째 대학이 있는 경우는 없나 생각했는데, 1번은
로 검증되고 있었네요.
첫번째 대학도 필드 레벨 검증이 아닌 ValidUniversityChoiceValidator에서 같이 검증하도록 하는 것에 대해서는 어떻게 생각하시나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제 생각에 addPropertyNode 를 사용하는건 구현이 복잡해질 것 같고, 지금 메세지만으로도 충분히 디버깅할 수 있다 생각해요.
이 부분은 규혁님이 판단하셔서 선택해주세요~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 같은 생각입니다😊
"유효한 대학 선택인지"에 대한 검증이 한 군데에 모여있어야 더 응집도 있을 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그렇네요 기존코드를 유지한 채 한다는 생각을 자꾸 가지다보니 오히려 더 헷갈리게 만들었네요.. 😂 반영했습니다!