feature/creation #2
|
|
@ -0,0 +1,41 @@
|
||||||
|
package org.octopus.internal.web.utils.dtos.validators
|
||||||
|
|
||||||
|
import jakarta.validation.Constraint
|
||||||
|
import jakarta.validation.ConstraintValidator
|
||||||
|
import jakarta.validation.ConstraintValidatorContext
|
||||||
|
import jakarta.validation.Payload
|
||||||
|
import org.octopus.internal.common.enums.EEntryType
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
|
|
||||||
|
class EntryHexColorValidator : ConstraintValidator<EntryHexColorValidator.Validate, String> {
|
||||||
|
companion object {
|
||||||
|
private val HEX_COLOR_PATTERN = Regex("^0x([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isValid(value: String?, context: ConstraintValidatorContext): Boolean {
|
||||||
|
if (value.isNullOrBlank()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
val isMatched = HEX_COLOR_PATTERN.matches(value)
|
||||||
|
|
||||||
|
if (!isMatched) {
|
||||||
|
context.disableDefaultConstraintViolation()
|
||||||
|
context.buildConstraintViolationWithTemplate("Invalid hex color format. Value must be in 0xRRGGBB format.")
|
||||||
|
.addConstraintViolation()
|
||||||
|
}
|
||||||
|
|
||||||
|
return isMatched
|
||||||
|
}
|
||||||
|
|
||||||
|
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER)
|
||||||
|
@Retention(AnnotationRetention.RUNTIME)
|
||||||
|
@MustBeDocumented
|
||||||
|
@Constraint(validatedBy = [EntryHexColorValidator::class])
|
||||||
|
annotation class Validate(
|
||||||
|
val message: String = "",
|
||||||
|
val groups: Array<KClass<*>> = [],
|
||||||
|
val payload: Array<KClass<out Payload>> = []
|
||||||
|
)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue