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

[FEAT] 글자수 제한 추가 #252

Merged
merged 10 commits into from
Feb 3, 2025
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ dependencies {

//Validation
implementation 'commons-validator:commons-validator:1.7'

//icu4j
implementation 'com.ibm.icu:icu4j:73.2'
}

tasks.named('test') {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/tiki/server/common/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.tiki.server.common.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum ErrorCode {

/* 400 BAD REQUEST : 잘못된 요청 */
UNCAUGHT_EXCEPTION(HttpStatus.BAD_REQUEST, "예상치 못한 오류가 발생했습니다."),
EMOJI_NOT_ALLOWED(HttpStatus.BAD_REQUEST, "이모지는 사용할 수 없습니다."),
EXCEEDED_MAX_LENGTH(HttpStatus.BAD_REQUEST, "최대 길이를 초과했습니다.");

private final HttpStatus httpStatus;
private final String message;
}
14 changes: 14 additions & 0 deletions src/main/java/com/tiki/server/common/exception/TikiException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.tiki.server.common.exception;

import lombok.Getter;

@Getter
public class TikiException extends RuntimeException {

private final ErrorCode errorCode;

public TikiException(final ErrorCode errorCode) {
super("[TikiException] : " + errorCode.getMessage());
this.errorCode = errorCode;
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/tiki/server/common/util/Validator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.tiki.server.common.util;

import static com.tiki.server.common.exception.ErrorCode.EMOJI_NOT_ALLOWED;
import static com.tiki.server.common.exception.ErrorCode.EXCEEDED_MAX_LENGTH;

import com.ibm.icu.lang.UCharacter;
import com.ibm.icu.lang.UProperty;
import com.ibm.icu.text.BreakIterator;
import com.tiki.server.common.exception.TikiException;

public class Validator {

public static void validateLengthContainEmoji(final String text, final int maxLength) {
BreakIterator iterator = BreakIterator.getCharacterInstance();
iterator.setText(text);

int count = 0;
while (BreakIterator.DONE != iterator.next()) {
count++;
}
if(count > maxLength) {
throw new TikiException(EXCEEDED_MAX_LENGTH);
}
}

public static void validateLength(final String text, final int maxLength) {
BreakIterator iterator = BreakIterator.getCharacterInstance();
iterator.setText(text);
int count = 0;
int index = iterator.first();
while (index != BreakIterator.DONE) {
int codePoint = text.codePointAt(index);
if (UCharacter.hasBinaryProperty(codePoint, UProperty.EMOJI)) {
throw new TikiException(EMOJI_NOT_ALLOWED);
}
count++;
if (count > maxLength) {
throw new TikiException(EXCEEDED_MAX_LENGTH);
}
index = iterator.next();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.tiki.server.memberteammanager.controller.dto.request;

import com.tiki.server.common.util.Validator;
import jakarta.validation.constraints.NotNull;

public record UpdateTeamMemberNameRequest(
@NotNull String newName
) {

public UpdateTeamMemberNameRequest(final String newName) {
Validator.validateLength(newName, 32);
this.newName = newName;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tiki.server.note.controller.dto.request;

import com.tiki.server.common.util.Validator;
import jakarta.validation.constraints.NotNull;

import java.time.LocalDate;
Expand All @@ -15,4 +16,17 @@ public record NoteFreeCreateRequest(
@NotNull List<Long> documentIds,
@NotNull long teamId
) {

public NoteFreeCreateRequest(final String title, final boolean complete, final LocalDate startDate, final LocalDate endDate, final String contents, final List<Long> timeBlockIds, final List<Long> documentIds, final long teamId) {
Validator.validateLengthContainEmoji(title, 30);
this.title = title;
this.complete = complete;
this.startDate = startDate;
this.endDate = endDate;
this.contents = contents;
this.timeBlockIds = timeBlockIds;
this.documentIds = documentIds;
this.teamId = teamId;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tiki.server.note.controller.dto.request;

import com.tiki.server.common.util.Validator;
import jakarta.validation.constraints.NotNull;
import lombok.NonNull;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -16,4 +16,15 @@ public record NoteFreeUpdateRequest(
@NotNull List<Long> documentIds,
@NotNull long teamId
) {
public NoteFreeUpdateRequest(final String title, final boolean complete, final LocalDate startDate, final LocalDate endDate, final String contents, final List<Long> timeBlockIds, final List<Long> documentIds, final long teamId) {
Validator.validateLengthContainEmoji(title, 30);
this.title = title;
this.complete = complete;
this.startDate = startDate;
this.endDate = endDate;
this.contents = contents;
this.timeBlockIds = timeBlockIds;
this.documentIds = documentIds;
this.teamId = teamId;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tiki.server.note.controller.dto.request;

import com.tiki.server.common.util.Validator;
import jakarta.validation.constraints.NotNull;
import lombok.NonNull;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -19,4 +19,19 @@ public record NoteTemplateCreateRequest(
@NotNull List<Long> documentIds,
@NotNull long teamId
) {

public NoteTemplateCreateRequest(final String title, final boolean complete, final LocalDate startDate, final LocalDate endDate, final String answerWhatActivity, final String answerHowToPrepare, final String answerWhatIsDisappointedThing, final String answerHowToFix, final List<Long> timeBlockIds, final List<Long> documentIds, final long teamId) {
Validator.validateLengthContainEmoji(title, 30);
this.title = title;
this.complete = complete;
this.startDate = startDate;
this.endDate = endDate;
this.answerWhatActivity = answerWhatActivity;
this.answerHowToPrepare = answerHowToPrepare;
this.answerWhatIsDisappointedThing = answerWhatIsDisappointedThing;
this.answerHowToFix = answerHowToFix;
this.timeBlockIds = timeBlockIds;
this.documentIds = documentIds;
this.teamId = teamId;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tiki.server.note.controller.dto.request;

import com.tiki.server.common.util.Validator;
import jakarta.validation.constraints.NotNull;
import lombok.NonNull;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -19,4 +19,18 @@ public record NoteTemplateUpdateRequest(
@NotNull List<Long> documentIds,
@NotNull long teamId
) {
public NoteTemplateUpdateRequest(final String title, final boolean complete, final LocalDate startDate, final LocalDate endDate, final String answerWhatActivity, final String answerHowToPrepare, final String answerWhatIsDisappointedThing, final String answerHowToFix, final List<Long> timeBlockIds, final List<Long> documentIds, final long teamId) {
Validator.validateLengthContainEmoji(title, 30);
this.title = title;
this.complete = complete;
this.startDate = startDate;
this.endDate = endDate;
this.answerWhatActivity = answerWhatActivity;
this.answerHowToPrepare = answerHowToPrepare;
this.answerWhatIsDisappointedThing = answerWhatIsDisappointedThing;
this.answerHowToFix = answerHowToFix;
this.timeBlockIds = timeBlockIds;
this.documentIds = documentIds;
this.teamId = teamId;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.tiki.server.team.dto.request;

import com.tiki.server.common.util.Validator;
import com.tiki.server.team.entity.Category;

import jakarta.validation.constraints.NotNull;
import lombok.NonNull;

public record TeamCreateRequest(
@NotNull String name,
@NotNull Category category,
@NotNull String iconImageUrl
@NotNull String name,
@NotNull Category category,
@NotNull String iconImageUrl
) {
public TeamCreateRequest(final String name, final Category category, final String iconImageUrl) {
Validator.validateLength(name, 30);
this.name = name;
this.category = category;
this.iconImageUrl = iconImageUrl;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.tiki.server.team.dto.request;

import com.tiki.server.common.util.Validator;
import jakarta.validation.constraints.NotNull;

public record TeamInformUpdateRequest(
@NotNull String teamName,
@NotNull String teamUrl
) {
public TeamInformUpdateRequest(final String teamName, final String teamUrl) {
Validator.validateLength(teamName, 30);
this.teamName = teamName;
this.teamUrl = teamUrl;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tiki.server.timeblock.dto.request;

import com.tiki.server.common.util.Validator;
import java.time.LocalDate;
import java.util.List;

Expand All @@ -15,4 +16,13 @@ public record TimeBlockCreateRequest(
@NotNull BlockType blockType,
@NotNull List<Long> documentIds
) {
public TimeBlockCreateRequest(final String name, final String color, final LocalDate startDate, final LocalDate endDate, final BlockType blockType, final List<Long> documentIds) {
Validator.validateLengthContainEmoji(name, 25);
this.name = name;
this.color = color;
this.startDate = startDate;
this.endDate = endDate;
this.blockType = blockType;
this.documentIds = documentIds;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tiki.server.timeblock.dto.request;

import com.tiki.server.common.util.Validator;
import java.time.LocalDate;

import jakarta.validation.constraints.NotNull;
Expand All @@ -9,4 +10,10 @@ public record TimeBlockUpdateRequest(
@NotNull LocalDate startDate,
@NotNull LocalDate endDate
) {
public TimeBlockUpdateRequest(final String name, final LocalDate startDate, final LocalDate endDate) {
Validator.validateLengthContainEmoji(name, 25);
this.name = name;
this.startDate = startDate;
this.endDate = endDate;
}
}