
com.breuninger.boot.validation.constraintvalidators.EnumValidator.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of validation Show documentation
Show all versions of validation Show documentation
spring-boot-starter-breuninger
package com.breuninger.boot.validation.constraintvalidators
import com.breuninger.boot.validation.constraints.IsEnum
import javax.validation.ConstraintValidator
import javax.validation.ConstraintValidatorContext
class EnumValidator : ConstraintValidator {
private var availableEnumNames: Set = HashSet()
private var ignoreCase = false
private var allowNull = false
override fun initialize(annotation: IsEnum) {
availableEnumNames = annotation.enumClass.java.enumConstants.map { it.name }.toSet()
ignoreCase = annotation.ignoreCase
allowNull = annotation.allowNull
}
override fun isValid(value: String?, context: ConstraintValidatorContext) = value?.let {
availableEnumNames.any {
return if (ignoreCase) {
it.equals(value, ignoreCase = true)
} else {
it == value
}
}
} ?: allowNull
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy