-
-
Save dharamgollapudi/bc5d72bf23b0b781514b10de3aa7d6e8 to your computer and use it in GitHub Desktop.
Custom Spring annotation and validator in Kotlin
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
package io.license.core.validation | |
import javax.validation.Constraint | |
import javax.validation.ConstraintValidator | |
import javax.validation.ConstraintValidatorContext | |
import javax.validation.Payload | |
import kotlin.reflect.KClass | |
@Target(AnnotationTarget.FIELD) | |
@MustBeDocumented | |
@Constraint(validatedBy = [NullableNotBlankValidator::class]) | |
annotation class NullableNotBlank( | |
val message: String = "{javax.validation.constraints.NotBlank.message}", | |
val groups: Array<KClass<*>> = [], | |
val payload: Array<KClass<out Payload>> = [] | |
) | |
class NullableNotBlankValidator : ConstraintValidator<NullableNotBlank, String> { | |
override fun isValid(value: String?, context: ConstraintValidatorContext?): Boolean { | |
if (value == null) return true | |
return value.isNotBlank() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment