Skip to content
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

태그 및 카테고리 생성 시 글자수 검증 #728

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public record CreateCategoryRequest(
@Schema(description = "카테고리 이름", example = "Spring")
@NotBlank(message = "카테고리 이름이 null 입니다.", groups = NotNullGroup.class)
@Size(max = 255, message = "카테고리 이름은 최대 255자까지 입력 가능합니다.", groups = SizeCheckGroup.class)
@Size(max = 15, message = "카테고리 이름은 최대 15자까지 입력 가능합니다.", groups = SizeCheckGroup.class)
String name
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ByteLengthValidator.class)
@Constraint(validatedBy = {ByteLengthValidator.class, GroupedByteLengthValidator.class})
public @interface ByteLength {

String message();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package codezap.global.validation;

import java.nio.charset.StandardCharsets;
import java.util.List;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class GroupedByteLengthValidator implements ConstraintValidator<ByteLength, List<String>> {

private int max;
private int min;

@Override
public void initialize(ByteLength constraintAnnotation) {
max = constraintAnnotation.max();
min = constraintAnnotation.min();
}

@Override
public boolean isValid(List<String> strings, ConstraintValidatorContext constraintValidatorContext) {
return strings.stream()
.allMatch(this::isValid);
}

private boolean isValid(String target) {
int byteLength = target.getBytes(StandardCharsets.UTF_8).length;
return min <= byteLength && byteLength <= max;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ public record CreateTemplateRequest(

@Schema(description = "태그 목록")
@NotNull(message = "태그 목록이 null 입니다.", groups = NotNullGroup.class)
@ByteLength(max = 30, message = "태그 명은 최대 30자까지 입력 가능합니다.", groups = SizeCheckGroup.class)
@Valid
List<String> tags
) implements ValidatedSourceCodesOrdinalRequest {

@Override
public List<Integer> extractSourceCodesOrdinal() {
return sourceCodes.stream().map(CreateSourceCodeRequest::ordinal).toList();
return sourceCodes.stream()
.map(CreateSourceCodeRequest::ordinal)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ void createCategoryFailWithNotLogin() throws Exception {

@Test
@DisplayName("카테고리 생성 실패: 카테고리 이름 길이 초과")
void createCategoryFailWithlongName() throws Exception {
void createCategoryFailWithLongName() throws Exception {
CreateCategoryRequest createCategoryRequest = new CreateCategoryRequest("a".repeat(MAX_LENGTH + 1));

mvc.perform(post("/categories")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(createCategoryRequest)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail").value("카테고리 이름은 최대 255자까지 입력 가능합니다."));
.andExpect(jsonPath("$.detail").value("카테고리 이름은 최대 15자까지 입력 가능합니다."));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,28 @@ void createTemplateFailWithLongNullTags() throws Exception {
.andExpect(jsonPath("$.detail").value("태그 목록이 null 입니다."));
}

@Test
@DisplayName("템플릿 생성 실패: 태그 목록에서 30자 초과인 태그 존재")
void createTemplateFailWithOverSizeTags() throws Exception {
String exceededTag = "a".repeat(31);

CreateTemplateRequest templateRequest = new CreateTemplateRequest(
"title",
"description",
List.of(new CreateSourceCodeRequest("title", "sourceCode", 1)),
1,
1L,
List.of(exceededTag)
);

mvc.perform(post("/templates")
.cookie(cookie)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(templateRequest)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail").value("태그 명은 최대 30자까지 입력 가능합니다."));
}

@ParameterizedTest
@DisplayName("템플릿 생성 실패: 잘못된 소스 코드 순서 입력")
Expand Down