Kotlin 数据 class 和具有容器元素约束的 bean 验证
Kotlin data class and bean validation with container element constraints
With Bean Validation 2.0 it is possible to also put constraints on container elements.
我无法让它与 Kotlin 数据一起使用 类:
data class Some(val someMap: Map<String, @Length(max = 255) String>)
这没有任何效果。有什么想法吗?
我创建了一个包含示例项目的存储库来重现案例:https://github.com/mduesterhoeft/bean-validation-container-constraints
从 Kotlin 1.3.70 和 1.4 开始,这应该可以设置特定的编译器选项:https://kotlinlang.org/docs/reference/whatsnew14.html#type-annotations-in-the-jvm-bytecode .
在任何以前的版本或任何这种支持不充分的情况下,你必须编写一个自定义验证器。
验证集合仅包含十六进制字符串的示例一:
@Target(
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.FIELD,
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.VALUE_PARAMETER
)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
@Constraint(validatedBy = [HexStringElementsValidator::class])
annotation class HexStringElements(
val message: String = "must only contain hex values",
val groups: Array<KClass<*>> = [],
val payload: Array<KClass<out Any>> = []
)
class HexStringElementsValidator : ConstraintValidator<HexStringElements, Collection<Any>> {
companion object {
val pattern = "^[a-fA-F0-9]+$".toRegex()
}
override fun isValid(value: Collection<Any>?, context: ConstraintValidatorContext?) =
value == null || value.all { it is String && pattern.matches(it) }
}
将此配置添加到您的 build.gradle
(注意 ... 表示已经存在的任何内容):
Groovy:
compileKotlin {
kotlinOptions {
freeCompilerArgs = [..., "-Xemit-jvm-type-annotations"]
...
}
}
科特林 DSL:
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf(..., "-Xemit-jvm-type-annotations")
...
}
}
With Bean Validation 2.0 it is possible to also put constraints on container elements.
我无法让它与 Kotlin 数据一起使用 类:
data class Some(val someMap: Map<String, @Length(max = 255) String>)
这没有任何效果。有什么想法吗?
我创建了一个包含示例项目的存储库来重现案例:https://github.com/mduesterhoeft/bean-validation-container-constraints
从 Kotlin 1.3.70 和 1.4 开始,这应该可以设置特定的编译器选项:https://kotlinlang.org/docs/reference/whatsnew14.html#type-annotations-in-the-jvm-bytecode .
在任何以前的版本或任何这种支持不充分的情况下,你必须编写一个自定义验证器。
验证集合仅包含十六进制字符串的示例一:
@Target(
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.FIELD,
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.VALUE_PARAMETER
)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
@Constraint(validatedBy = [HexStringElementsValidator::class])
annotation class HexStringElements(
val message: String = "must only contain hex values",
val groups: Array<KClass<*>> = [],
val payload: Array<KClass<out Any>> = []
)
class HexStringElementsValidator : ConstraintValidator<HexStringElements, Collection<Any>> {
companion object {
val pattern = "^[a-fA-F0-9]+$".toRegex()
}
override fun isValid(value: Collection<Any>?, context: ConstraintValidatorContext?) =
value == null || value.all { it is String && pattern.matches(it) }
}
将此配置添加到您的 build.gradle
(注意 ... 表示已经存在的任何内容):
Groovy:
compileKotlin {
kotlinOptions {
freeCompilerArgs = [..., "-Xemit-jvm-type-annotations"]
...
}
}
科特林 DSL:
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf(..., "-Xemit-jvm-type-annotations")
...
}
}