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

[20210904] (Kotlin) Custom Valid Annotation #204

Open
JuHyun419 opened this issue Sep 4, 2021 · 0 comments
Open

[20210904] (Kotlin) Custom Valid Annotation #204

JuHyun419 opened this issue Sep 4, 2021 · 0 comments
Labels

Comments

@JuHyun419
Copy link
Owner

Kotlin - 커스텀 어노테이션(Valid)

  • 기존 코드
import ...

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy::class)
// @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy::class) deprecated
data class UserRequest(

        ...

        var createdAt: String? = null // yyyy-MM-dd HH:mm:ss
)

    /* StringFormatDateTime, Valid로 대체 */
    @AssertTrue(message = "생성일자의 패턴은 yyyy-MM-dd HH:mm:ss 여야 합니다.")
    private fun isValidCreatedAt(): Boolean {
        try {
            LocalDateTime.parse(this.createdAt, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
            return true
        } catch (e: Exception) {
            return false
        }
    }

  • Custom
/* annotation */
import org.juhyun.kotlinspringboot.valid.StringFormatDateTimeValid
import javax.validation.Constraint
import javax.validation.Payload
import kotlin.reflect.KClass

@Constraint(validatedBy = [StringFormatDateTimeValid::class])
@Target(
        AnnotationTarget.FIELD,
        AnnotationTarget.PROPERTY_GETTER,
        AnnotationTarget.PROPERTY_GETTER
)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class StringFormatDateTime(
        val pattern: String = "yyyy-MM-dd HH:mm:ss",
        val message: String = "시간 형식이 유효하지 않습니다",
        val groups: Array<KClass<*>> = [],
        val payload: Array<KClass<out Payload>> = []
)


/* Valid Class */
import org.juhyun.kotlinspringboot.annotation.StringFormatDateTime
import java.lang.Exception
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import javax.validation.ConstraintValidator
import javax.validation.ConstraintValidatorContext

class StringFormatDateTimeValid : ConstraintValidator<StringFormatDateTime, String> {

    private var pattern: String? = null

    override fun initialize(constraintAnnotation: StringFormatDateTime?) {
        this.pattern = constraintAnnotation?.pattern
    }

    // 유효성 검사
    override fun isValid(value: String?, context: ConstraintValidatorContext?): Boolean {
        try {
            LocalDateTime.parse(value, DateTimeFormatter.ofPattern(pattern))
            return true
        } catch (e: Exception) {
            return false
        }
    }

}


/* UserRequest */
@field:StringFormatDateTime(pattern = "yyyy-MM-dd HH:mm:ss", message = "패턴이 올바르지 않습니다.")
var createdAt: String? = null // yyyy-MM-dd HH:mm:ss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant