Skip to content

Commit

Permalink
Merge pull request #260 from Team-Tiki/fix/#259-validator
Browse files Browse the repository at this point in the history
[FIX] 벨리데이터 수정
  • Loading branch information
paragon0107 authored Feb 12, 2025
2 parents eb44a54 + 5b05383 commit e71ace0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public enum ErrorCode {

/* 400 BAD REQUEST : 잘못된 요청 */
UNCAUGHT_EXCEPTION(HttpStatus.BAD_REQUEST, "예상치 못한 오류가 발생했습니다."),
EMOJI_NOT_ALLOWED(HttpStatus.BAD_REQUEST, "이모지는 사용할 수 없습니다."),
INVALID_CHARACTER(HttpStatus.BAD_REQUEST, "한글, 영어, 숫자, 일부 특수문자 만 입력 가능합니다."),
EXCEEDED_MAX_LENGTH(HttpStatus.BAD_REQUEST, "최대 길이를 초과했습니다.");

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.tiki.server.auth.exception.AuthException;
import com.tiki.server.common.dto.ErrorCodeResponse;
import com.tiki.server.common.exception.TikiException;
import com.tiki.server.email.emailsender.exception.EmailSenderException;
import com.tiki.server.email.teaminvitation.exception.TeamInvitationException;
import com.tiki.server.email.verification.exception.EmailVerificationException;
Expand Down Expand Up @@ -33,6 +34,14 @@
@RestControllerAdvice
public class ErrorHandler {

@ExceptionHandler(TikiException.class)
public ResponseEntity<BaseResponse> tikiException(TikiException exception) {
log.error(exception.getMessage());
val errorCode = exception.getErrorCode();
Sentry.captureException(exception);
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.of(errorCode.getMessage()));
}

@ExceptionHandler(MemberException.class)
public ResponseEntity<BaseResponse> memberException(MemberException exception) {
log.error(exception.getMessage());
Expand Down
29 changes: 11 additions & 18 deletions src/main/java/com/tiki/server/common/util/Validator.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
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 static com.tiki.server.common.exception.ErrorCode.INVALID_CHARACTER;

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;

Expand All @@ -18,26 +16,21 @@ public static void validateLengthContainEmoji(final String text, final int maxLe
while (BreakIterator.DONE != iterator.next()) {
count++;
}
if(count > maxLength) {
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();
if (text.length() > maxLength) {
throw new TikiException(EXCEEDED_MAX_LENGTH);
}
}

public static void validText(final String text) {
String regex = "^[a-zA-Z가-힣0-9 !@#$%^&*()\\-_=+\\[\\]{};:'\",.<>?/|\\\\]+$";
if (!text.matches(regex)) {
throw new TikiException(INVALID_CHARACTER);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public record UpdateTeamMemberNameRequest(
) {

public UpdateTeamMemberNameRequest(final String newName) {
Validator.validText(newName);
Validator.validateLength(newName, 32);
this.newName = newName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public record TeamCreateRequest(
@NotNull String iconImageUrl
) {
public TeamCreateRequest(final String name, final Category category, final String iconImageUrl) {
Validator.validText(name);
Validator.validateLength(name, 30);
this.name = name;
this.category = category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public record TeamInformUpdateRequest(
@NotNull String teamUrl
) {
public TeamInformUpdateRequest(final String teamName, final String teamUrl) {
Validator.validText(teamName);
Validator.validateLength(teamName, 30);
this.teamName = teamName;
this.teamUrl = teamUrl;
Expand Down

0 comments on commit e71ace0

Please sign in to comment.