Hibernate Validator 布尔逻辑
Hibernate Validator boolean logic
我正在研究在 AND'ing 约束不够的情况下,使用 Hibernate Validator 对我的 bean 验证使用布尔逻辑。我发现可以通过使用 @ConstraintComposition
注释作为 described in the documentation 创建新注释来更改此默认行为。该文档提供了以下示例。
@ConstraintComposition(OR)
@Pattern(regexp = "[a-z]")
@Size(min = 2, max = 3)
@ReportAsSingleViolation
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface PatternOrSize {
String message() default "{org.hibernate.validator.referenceguide.chapter11." +
"booleancomposition.PatternOrSize.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
使用此 @PatternOrSize
验证约束意味着输入字符串 或者 小写 或 的大小介于 2 和 3 之间. 现在这提出了几个问题:
- 我相信 必须创建一个新注释来更改默认的布尔逻辑行为。这个对吗?如果可以在 class 字段中定义布尔逻辑,那就太好了,但这可能不可能吗?
- 是否可以在不创建自定义验证器的情况下进一步自定义布尔逻辑行为,例如同时定义
AND
和 OR
?我找不到任何迹象表明这是可能的,但也许我错过了什么。
- 最重要的是: 是否可以使
@Pattern
和 @Size
约束的参数动态化?因为否则我想我将不得不为我需要的每个参数组合定义一个新的注释。定义一个新的注释只是为了将参数更改为 @Size
似乎不可行,但可能是必要的?
提前致谢。
I believe one has to create a new annotation to change the default boolean logic behavior. Is this correct?
是的,没错。
Is it possible to further customize the boolean logic behavior without creating a custom validator, e.g. defining AND and OR at the same time?
您可以尝试创建一个分层组合的约束(即由其他约束组成的约束),它在不同级别使用 AND 和 OR。我还没有尝试过(我不认为我们有测试)但它可能值得一试。不过,根据所需的布尔逻辑,它可能不适合您的用例。
is it possible to make the arguments to the @Pattern and @Size constraints dynamic?
是的,您可以通过 @OverridesAttribute
:
@ConstraintComposition(OR)
@Pattern(regexp = "[a-z]")
@Size(min = 2, max = 3)
@ReportAsSingleViolation
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface PatternOrSize {
String message() default "...";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
@OverridesAttribute(constraint=Size.class, name="min")
int min() default 0;
@OverridesAttribute(constraint=Size.class, name="max")
int max() default Integer.MAX_VALUE;
@OverridesAttribute(constraint=Pattern.class, name="regexp")
String regexp();
}
我正在研究在 AND'ing 约束不够的情况下,使用 Hibernate Validator 对我的 bean 验证使用布尔逻辑。我发现可以通过使用 @ConstraintComposition
注释作为 described in the documentation 创建新注释来更改此默认行为。该文档提供了以下示例。
@ConstraintComposition(OR)
@Pattern(regexp = "[a-z]")
@Size(min = 2, max = 3)
@ReportAsSingleViolation
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface PatternOrSize {
String message() default "{org.hibernate.validator.referenceguide.chapter11." +
"booleancomposition.PatternOrSize.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
使用此 @PatternOrSize
验证约束意味着输入字符串 或者 小写 或 的大小介于 2 和 3 之间. 现在这提出了几个问题:
- 我相信 必须创建一个新注释来更改默认的布尔逻辑行为。这个对吗?如果可以在 class 字段中定义布尔逻辑,那就太好了,但这可能不可能吗?
- 是否可以在不创建自定义验证器的情况下进一步自定义布尔逻辑行为,例如同时定义
AND
和OR
?我找不到任何迹象表明这是可能的,但也许我错过了什么。 - 最重要的是: 是否可以使
@Pattern
和@Size
约束的参数动态化?因为否则我想我将不得不为我需要的每个参数组合定义一个新的注释。定义一个新的注释只是为了将参数更改为@Size
似乎不可行,但可能是必要的?
提前致谢。
I believe one has to create a new annotation to change the default boolean logic behavior. Is this correct?
是的,没错。
Is it possible to further customize the boolean logic behavior without creating a custom validator, e.g. defining AND and OR at the same time?
您可以尝试创建一个分层组合的约束(即由其他约束组成的约束),它在不同级别使用 AND 和 OR。我还没有尝试过(我不认为我们有测试)但它可能值得一试。不过,根据所需的布尔逻辑,它可能不适合您的用例。
is it possible to make the arguments to the @Pattern and @Size constraints dynamic?
是的,您可以通过 @OverridesAttribute
:
@ConstraintComposition(OR)
@Pattern(regexp = "[a-z]")
@Size(min = 2, max = 3)
@ReportAsSingleViolation
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface PatternOrSize {
String message() default "...";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
@OverridesAttribute(constraint=Size.class, name="min")
int min() default 0;
@OverridesAttribute(constraint=Size.class, name="max")
int max() default Integer.MAX_VALUE;
@OverridesAttribute(constraint=Pattern.class, name="regexp")
String regexp();
}