-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: ์ฌ์ง ์๋น์ค ์์ฒญ Validation ์์
- Loading branch information
Showing
12 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
photo-service/src/main/java/kr/mafoo/photo/annotation/MatchEnum.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,46 @@ | ||
package kr.mafoo.photo.annotation; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Constraint(validatedBy = {MatchEnum.EnumValidator.class}) | ||
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface MatchEnum { | ||
String message() default "ENUM ํ์ ์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค"; | ||
Class<?>[] groups() default {}; | ||
Class<? extends Payload>[] payload() default {}; | ||
Class<? extends java.lang.Enum<?>> enumClass(); | ||
|
||
class EnumValidator implements ConstraintValidator<MatchEnum, String> { | ||
private MatchEnum annotation; | ||
|
||
@Override | ||
public void initialize(MatchEnum constraintAnnotation) { | ||
this.annotation = constraintAnnotation; | ||
} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if (value == null) return false; | ||
boolean result = false; | ||
Object[] enumValues = this.annotation.enumClass().getEnumConstants(); | ||
if (enumValues != null) { | ||
for (Object enumValue : enumValues) { | ||
if (value.equals(enumValue.toString())) { | ||
result = true; | ||
break; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
photo-service/src/main/java/kr/mafoo/photo/annotation/ULID.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,25 @@ | ||
package kr.mafoo.photo.annotation; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = {ULID.ULIDValidator.class}) | ||
public @interface ULID { | ||
String message() default "ULID ํ์์ด ์๋๋๋ค"; | ||
Class[] groups() default {}; | ||
Class[] payload() default {}; | ||
|
||
class ULIDValidator implements ConstraintValidator<ULID, String> { | ||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if (value == null) return false; | ||
return value.matches("[0-7][0-9A-HJKMNP-TV-Z]{25}"); | ||
} | ||
} | ||
|
||
} |
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
32 changes: 30 additions & 2 deletions
32
photo-service/src/main/java/kr/mafoo/photo/config/WebExceptionHandler.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 |
---|---|---|
@@ -1,17 +1,45 @@ | ||
package kr.mafoo.photo.config; | ||
|
||
import jakarta.validation.ConstraintViolationException; | ||
import kr.mafoo.photo.controller.dto.response.ErrorResponse; | ||
import kr.mafoo.photo.exception.DomainException; | ||
import kr.mafoo.photo.exception.ErrorCode; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.bind.support.WebExchangeBindException; | ||
|
||
@ControllerAdvice | ||
@RestControllerAdvice | ||
public class WebExceptionHandler { | ||
@ExceptionHandler(DomainException.class) | ||
public ResponseEntity<ErrorResponse> handleDomainException(DomainException exception) { | ||
return ResponseEntity | ||
.badRequest() | ||
.body(ErrorResponse.fromErrorCode(exception.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler({MethodArgumentNotValidException.class, | ||
ConstraintViolationException.class, | ||
WebExchangeBindException.class}) | ||
public ResponseEntity<ErrorResponse> validException(Exception ex) { | ||
String errorMessage = "์ ๋ ฅ๊ฐ ๊ฒ์ฆ ์ค๋ฅ: "; | ||
if (ex instanceof MethodArgumentNotValidException mex) { | ||
errorMessage += mex.getBindingResult().getAllErrors().get(0).getDefaultMessage(); | ||
} else if (ex instanceof ConstraintViolationException cvex) { | ||
errorMessage += cvex.getConstraintViolations().iterator().next().getMessage(); | ||
} else if (ex instanceof WebExchangeBindException wex) { | ||
errorMessage += wex.getAllErrors().get(0).getDefaultMessage(); | ||
} else { | ||
errorMessage += "์ ์ ์๋ ์ค๋ฅ"; | ||
} | ||
ErrorResponse response = new ErrorResponse( | ||
ErrorCode.REQUEST_INPUT_NOT_VALID.getCode(), | ||
errorMessage | ||
); | ||
|
||
return ResponseEntity | ||
.badRequest() | ||
.body(response); | ||
} | ||
} |
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
9 changes: 8 additions & 1 deletion
9
photo-service/src/main/java/kr/mafoo/photo/controller/dto/request/AlbumCreateRequest.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 |
---|---|---|
@@ -1,13 +1,20 @@ | ||
package kr.mafoo.photo.controller.dto.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
import kr.mafoo.photo.annotation.MatchEnum; | ||
import kr.mafoo.photo.domain.AlbumType; | ||
import org.hibernate.validator.constraints.Length; | ||
|
||
@Schema(description = "์จ๋ฒ ์์ฑ ์์ฒญ") | ||
public record AlbumCreateRequest( | ||
@NotBlank | ||
@Length(min = 1, max = 100) | ||
@Schema(description = "์จ๋ฒ ์ด๋ฆ", example = "์๊ธ์นํ์ทํ") | ||
String name, | ||
|
||
@Schema(description = "์จ๋ฒ ํ์ ", example = "TYPE_A") | ||
@MatchEnum(enumClass = AlbumType.class) | ||
@Schema(description = "์จ๋ฒ ํ์ ") | ||
String type | ||
) { | ||
} |
10 changes: 8 additions & 2 deletions
10
photo-service/src/main/java/kr/mafoo/photo/controller/dto/request/AlbumUpdateRequest.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 |
---|---|---|
@@ -1,14 +1,20 @@ | ||
package kr.mafoo.photo.controller.dto.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
import kr.mafoo.photo.annotation.MatchEnum; | ||
import kr.mafoo.photo.domain.AlbumType; | ||
import org.hibernate.validator.constraints.Length; | ||
|
||
@Schema(description = "์จ๋ฒ ์์ ์์ฒญ") | ||
public record AlbumUpdateRequest( | ||
@NotBlank | ||
@Length(min = 1, max = 100) | ||
@Schema(description = "์จ๋ฒ ์ด๋ฆ", example = "์๊ธ์นํ์ทํ") | ||
String name, | ||
|
||
@Schema(description = "์จ๋ฒ ํ์ ", example = "TYPE_A") | ||
AlbumType type | ||
@MatchEnum(enumClass = AlbumType.class) | ||
@Schema(description = "์จ๋ฒ ํ์ ") | ||
String type | ||
) { | ||
} |
2 changes: 2 additions & 0 deletions
2
photo-service/src/main/java/kr/mafoo/photo/controller/dto/request/PhotoCreateRequest.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
2 changes: 2 additions & 0 deletions
2
...ervice/src/main/java/kr/mafoo/photo/controller/dto/request/PhotoUpdateAlbumIdRequest.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
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