-
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.
- Loading branch information
Showing
6 changed files
with
72 additions
and
2 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
2 changes: 2 additions & 0 deletions
2
...ain/kotlin/dsm/pick2024/domain/application/presentation/dto/request/ApplicationRequest.kt
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
3 changes: 3 additions & 0 deletions
3
...tlin/dsm/pick2024/domain/earlyreturn/presentation/dto/request/CreateEarlyReturnRequest.kt
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
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/dsm/pick2024/global/annotation/ValidFormat.kt
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,15 @@ | ||
package dsm.pick2024.global.annotation | ||
|
||
import dsm.pick2024.global.annotation.common.FormatValidator | ||
import javax.validation.Constraint | ||
import javax.validation.Payload | ||
import kotlin.reflect.KClass | ||
|
||
@Constraint(validatedBy = [FormatValidator::class]) | ||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class ValidFormat( | ||
val message: String = "형식이 올바르지 않습니다.", | ||
val groups: Array<KClass<*>> = [], // 어떤 타입의 데이터든 포함할 수 있다 | ||
val payload: Array<KClass<out Payload>> = [] //Payload 타입을 상속받는 어떤 클래스든 수용할 수 있음을 의미 | ||
) |
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/dsm/pick2024/global/annotation/common/FormatValidator.kt
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 dsm.pick2024.global.annotation.common | ||
|
||
import dsm.pick2024.domain.application.enums.ApplicationType | ||
import dsm.pick2024.domain.application.presentation.dto.request.ApplicationRequest | ||
import dsm.pick2024.domain.earlyreturn.presentation.dto.request.CreateEarlyReturnRequest | ||
import dsm.pick2024.global.annotation.ValidFormat | ||
import javax.validation.ConstraintValidator | ||
import javax.validation.ConstraintValidatorContext | ||
import java.time.LocalTime | ||
import java.time.format.DateTimeParseException | ||
|
||
class FormatValidator : ConstraintValidator<ValidFormat, Any> { | ||
|
||
override fun isValid(request: Any, context: ConstraintValidatorContext): Boolean { | ||
return when (request) { | ||
is ApplicationRequest -> validateApplicationRequest(request) | ||
is CreateEarlyReturnRequest -> validateCreateEarlyReturnRequest(request) | ||
else -> false | ||
} | ||
} | ||
|
||
private fun validateApplicationRequest(request: ApplicationRequest): Boolean { | ||
return if (request.applicationType == ApplicationType.PERIOD) { | ||
isValidPeriodFormat(request.start) && isValidPeriodFormat(request.end) | ||
} else { | ||
isValidTimeFormat(request.start) && isValidTimeFormat(request.end) | ||
} | ||
} | ||
|
||
private fun validateCreateEarlyReturnRequest(request: CreateEarlyReturnRequest): Boolean { | ||
return isValidTimeFormat(request.start) | ||
} | ||
|
||
private fun isValidPeriodFormat(period: String): Boolean { | ||
return Regex("""\d+교시""").matches(period) | ||
} | ||
|
||
private fun isValidTimeFormat(time: String): Boolean { | ||
return try { | ||
LocalTime.parse(time) | ||
true | ||
} catch (e: DateTimeParseException) { | ||
false | ||
} | ||
} | ||
} |