Michael
05/30/2018, 3:22 PMannotation class
instead - but is it otherwise the same, a separate class that the annotation class points to? at first I thought maybe I would just put the behavior in the annotation class
- but it seems problematic to extend ConstraintValidator
since that needs to be typed as the annotation type. This is not my usual area, so I’m just kind of trying things.leolima
05/30/2018, 3:26 PM@Target(AnnotationTarget.FIELD, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@Constraint(validatedBy = [CnpjAnnotationValidator::class])
annotation class Cnpj(
val message: String = "",
val groups: Array<KClass<*>> = [],
val payload: Array<KClass<out Payload>> = []
)
class CnpjAnnotationValidator : ConstraintValidator<Cnpj, String> {
override fun initialize(constraintAnnotation: Cnpj) {
}
override fun isValid(value: String?, context: ConstraintValidatorContext?): Boolean {
return try {
CNPJ_VALIDATOR.assertValid(value)
true
} catch (ex: InvalidStateException) {
false
}
}
companion object {
private val CNPJ_VALIDATOR = CNPJValidator()
}
}
Michael
05/30/2018, 3:29 PM@Constraint
error?leolima
05/30/2018, 3:30 PMMichael
05/30/2018, 4:00 PMMichael
05/30/2018, 4:00 PM