Last active
November 8, 2019 08:07
-
-
Save arahansa/48ae468930d2744c94d8fb28fe237356 to your computer and use it in GitHub Desktop.
빈밸리데이션 크로스 필드 체크
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
import org.springframework.beans.BeanWrapperImpl | |
import javax.validation.Constraint | |
import javax.validation.ConstraintValidator | |
import javax.validation.ConstraintValidatorContext | |
import javax.validation.Payload | |
import kotlin.reflect.KClass | |
@Target(AnnotationTarget.CLASS) | |
@Constraint(validatedBy = [StringMatchValidator::class]) | |
annotation class StringMatch( | |
val message : String = "", | |
val groups: Array<KClass<*>> = [], | |
val payload: Array<KClass<out Payload>> = [], | |
val first: String = "first", | |
val second: String = "second" | |
) | |
class StringMatchValidator : ConstraintValidator<StringMatch, Any> { | |
var message: String? = null | |
var first: String = "first" | |
var second: String = "second" | |
override fun initialize(constraintAnnotation: StringMatch) { | |
this.message = constraintAnnotation.message | |
this.first = constraintAnnotation.first | |
this.second = constraintAnnotation.second | |
} | |
override fun isValid(value: Any, context: ConstraintValidatorContext): Boolean { | |
var firtValue: String? = BeanWrapperImpl(value).getPropertyValue(first) as String? | |
var secondValue : String? = BeanWrapperImpl(value).getPropertyValue(second) as String? | |
if(firtValue == null || secondValue == null) | |
return false | |
val isValid = firtValue.equals(secondValue) | |
if(!isValid){ | |
context.disableDefaultConstraintViolation() | |
context.buildConstraintViolationWithTemplate(this.message) | |
.addPropertyNode(this.first) | |
.addConstraintViolation() | |
} | |
return isValid | |
} | |
} | |
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
import org.assertj.core.api.Assertions.assertThat | |
import org.junit.jupiter.api.Test | |
import javax.validation.ConstraintViolation | |
import javax.validation.Validation | |
class ValidStringMatchTest{ | |
@StringMatch(message = "test", first="a", second="b") | |
class TestClass(val a: String, val b: String) | |
private val validator = Validation.buildDefaultValidatorFactory().validator | |
@Test | |
fun matchTestSuccess(){ | |
var t = TestClass("arahan", "arahan") | |
var validate: MutableSet<ConstraintViolation<TestClass>> = validator.validate(t) | |
assertThat(validate.isNullOrEmpty()).isTrue() | |
} | |
@Test | |
fun matchTestFailed() { | |
var t = TestClass("arahan", "arahansa") | |
var validate: MutableSet<ConstraintViolation<TestClass>> = validator.validate(t) | |
assertThat(validate.isNullOrEmpty()).isFalse() | |
assertThat(validate.count()).isEqualTo(1) | |
val violation = validate.first() | |
assertThat(violation.messageTemplate).isEqualTo("test") | |
assertThat(violation.propertyPath.toString()).isEqualTo("a") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment