Skip to content

Commit 26b7da8

Browse files
committed
[BOOK-79] refactor: apis,global-utils validation 강화 (#15)
1 parent 9bbebe6 commit 26b7da8

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

apis/src/main/kotlin/org/yapp/apis/book/controller/BookController.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.yapp.apis.book.controller
33
import jakarta.validation.Valid
44
import org.springframework.http.ResponseEntity
55
import org.springframework.web.bind.annotation.GetMapping
6+
import org.springframework.web.bind.annotation.ModelAttribute
67
import org.springframework.web.bind.annotation.RequestMapping
78
import org.springframework.web.bind.annotation.RestController
89
import org.yapp.apis.book.dto.request.BookDetailRequest
@@ -19,14 +20,14 @@ class BookController(
1920
) : BookControllerApi {
2021

2122
@GetMapping("/search")
22-
override fun searchBooks(@Valid request: BookSearchRequest): ResponseEntity<BookSearchResponse> {
23+
override fun searchBooks(@Valid @ModelAttribute request: BookSearchRequest): ResponseEntity<BookSearchResponse> {
2324
val response = bookUseCase.searchBooks(request)
2425
return ResponseEntity.ok(response)
2526
}
2627

2728
@GetMapping("/detail")
2829
override fun getBookDetail(
29-
@Valid request: BookDetailRequest
30+
@Valid @ModelAttribute request: BookDetailRequest
3031
): ResponseEntity<BookDetailResponse> {
3132
val response = bookUseCase.getBookDetail(request)
3233
return ResponseEntity.ok(response)

apis/src/main/kotlin/org/yapp/apis/book/dto/request/BookDetailRequest.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package org.yapp.apis.book.dto.request
22

33
import jakarta.validation.constraints.NotBlank
4+
import jakarta.validation.constraints.Pattern // Pattern 어노테이션 추가
5+
import org.yapp.globalutils.util.RegexUtils
46
import org.yapp.infra.external.aladin.dto.AladinBookLookupRequest
57

68
data class BookDetailRequest private constructor(
79
@field:NotBlank(message = "아이템 ID는 필수입니다.")
10+
@field:Pattern(
11+
regexp = RegexUtils.NOT_BLANK_AND_NOT_NULL_STRING_PATTERN,
12+
message = "아이템 ID는 유효한 ISBN 형식이 아닙니다."
13+
)
814
val itemId: String? = null,
915
val itemIdType: String? = "ISBN",
1016
val optResult: List<String>? = null
1117
) {
1218
fun toAladinRequest(): AladinBookLookupRequest {
13-
require(!itemId.isNullOrBlank()) { "아이템 ID는 비어있을 수 없습니다." }
1419
return AladinBookLookupRequest.create(
15-
itemId = this.itemId,
20+
itemId = this.itemId!!,
1621
itemIdType = this.itemIdType ?: "ISBN",
1722
optResult = this.optResult
1823
)

apis/src/main/kotlin/org/yapp/apis/book/dto/request/BookSearchRequest.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package org.yapp.apis.book.dto.request
22

3-
import jakarta.validation.constraints.NotBlank
43
import org.yapp.infra.external.aladin.dto.AladinBookSearchRequest
54

65

76
data class BookSearchRequest private constructor(
8-
@field:NotBlank(message = "검색어는 필수입니다.")
97
val query: String? = null,
108
val queryType: String? = null,
119
val searchTarget: String? = null,
@@ -16,11 +14,11 @@ data class BookSearchRequest private constructor(
1614
val categoryId: Int? = null
1715
) {
1816

17+
fun validQuery(): String = query!!
1918
fun toAladinRequest(): AladinBookSearchRequest {
20-
require(!query.isNullOrBlank()) { "검색어(query)는 필수입니다." }
2119

2220
return AladinBookSearchRequest.create(
23-
query = this.query,
21+
query = this.validQuery(),
2422
queryType = this.queryType,
2523
searchTarget = this.searchTarget,
2624
maxResults = this.maxResults,

global-utils/src/main/kotlin/org/yapp/globalutils/util/RegexUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.yapp.globalutils.util
22

3-
43
object RegexUtils {
54

65
val EMAIL_PATTERN = Regex("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$")
76

87
val PROFILE_IMAGE_URL_PATTERN = Regex("^https?://[a-zA-Z0-9.-]+(/.*)?$")
98

9+
const val NOT_BLANK_AND_NOT_NULL_STRING_PATTERN = "^(?!null$|NULL$|\\s*$).+" // Removed the old ISBN pattern
1010

1111
fun isValidEmail(email: String): Boolean {
1212
return email.matches(EMAIL_PATTERN)

0 commit comments

Comments
 (0)