-
Notifications
You must be signed in to change notification settings - Fork 2
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
[BE] feat: 리뷰 등록 요청 dto 형식 검증 로직을 SpringBeanValidator 를 통해 구현 #668
Open
nayonsoso
wants to merge
5
commits into
develop
Choose a base branch
from
be/feature/572-dto-validation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/reviewme/review/service/dto/request/EitherTextOrCheckbox.java
This file contains 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 reviewme.review.service.dto.request; | ||
|
||
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 = EitherTextOrCheckboxValidator.class) | ||
public @interface EitherTextOrCheckbox { | ||
|
||
String message() default "선택형 응답과 서술형 응답 중 하나만 입력해주세요."; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아루의 코멘트에 이어서 에러 메세지도 질문 타입 추가에 영향을 받지 않도록 수정되면 좋을 것 같아요. |
||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/reviewme/review/service/dto/request/EitherTextOrCheckboxValidator.java
This file contains 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,16 @@ | ||
package reviewme.review.service.dto.request; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
public class EitherTextOrCheckboxValidator | ||
implements ConstraintValidator<EitherTextOrCheckbox, ReviewAnswerRequest> { | ||
|
||
@Override | ||
public boolean isValid(ReviewAnswerRequest request, ConstraintValidatorContext context) { | ||
if (request.selectedOptionIds() != null) { | ||
return request.text() == null; | ||
} | ||
return request.text() != null; | ||
} | ||
} |
This file contains 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 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
13 changes: 0 additions & 13 deletions
13
.../src/main/java/reviewme/review/service/exception/CheckBoxAnswerIncludedTextException.java
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
...rc/main/java/reviewme/review/service/exception/TextAnswerIncludedOptionItemException.java
This file was deleted.
Oops, something went wrong.
This file contains 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
60 changes: 60 additions & 0 deletions
60
.../src/test/java/reviewme/review/service/dto/request/EitherTextOrCheckboxValidatorTest.java
This file contains 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,60 @@ | ||
package reviewme.review.service.dto.request; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.Validation; | ||
import jakarta.validation.Validator; | ||
import jakarta.validation.ValidatorFactory; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class EitherTextOrCheckboxValidatorTest { | ||
|
||
private Validator validator; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); | ||
validator = factory.getValidator(); | ||
} | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("validRequests") | ||
void 유효한_응답에_대한_검증(String title, Long questionId, List<Long> selectedOptionIds, String text) { | ||
// given | ||
ReviewAnswerRequest request = new ReviewAnswerRequest(questionId, selectedOptionIds, text); | ||
|
||
// when | ||
Set<ConstraintViolation<ReviewAnswerRequest>> violations = validator.validate(request); | ||
|
||
// then | ||
assertThat(violations).isEmpty(); | ||
} | ||
|
||
static Stream<Arguments> validRequests() { | ||
return Stream.of( | ||
Arguments.of("선택형 응답만 존재", 1L, List.of(1L), null), | ||
Arguments.of("서술형 응답만 존재", 1L, null, "답"), | ||
Arguments.of("필수 아닌 질문 (둘 다 없음)", 1L, null, "") | ||
); | ||
} | ||
|
||
@Test | ||
void 유효하지_않은_응답에_대한_검증() { | ||
// given | ||
ReviewAnswerRequest request = new ReviewAnswerRequest(1L, List.of(1L), "답"); | ||
|
||
// when | ||
Set<ConstraintViolation<ReviewAnswerRequest>> violations = validator.validate(request); | ||
|
||
// then | ||
assertThat(violations).hasSize(1); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
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.
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.
Scale
이라는 타입이 추가되면 어떡해요?EitherTextOrCheckboxOrScale
이라고 이름 바꿔야 하나요..? 🤔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.
"해당하는 질문 타입에 대해서만 답변할 수 있다" 이런 의미를 담도록 바꿔보겠습니다 ㅎㅎ