Custom Validation using Kotlin

Asked

Viewed 40 times

0

I am trying to create a validation where it is necessary that the phone or mobile phone is required (when one exists the other is not required, if both are empty one or the other needs to be filled), the problem that I can do the validation but it returns me 500 instead of 400 and the Constraintviolationexception I’m not able to get with the Advice controller.

@Documented
@Constraint(validatedBy = [CellphoneOrPhoneIsRequiredValidator::class])
@Target(AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASS)
@Retention(RetentionPolicy.RUNTIME)
annotation class CellphoneOrPhoneIsRequired(
        val message: String = "Cellphone or phone is required",
        val groups: Array<KClass<*>> = [],
        val payload: Array<KClass<out Payload>> = []
)

class CellphoneOrPhoneIsRequiredValidator: ConstraintValidator<CellphoneOrPhoneIsRequired, Debtor> {
    override fun isValid(debtor: Debtor, context: ConstraintValidatorContext): Boolean {
        if(debtor.cellphone.isNullOrEmpty() && debtor.phone.isNullOrEmpty()) {
            return false
        }

        return true
    }
}

@Entity(name = "debtors")
@CellphoneOrPhoneIsRequired
class Debtor(
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.