Annotation Interface LimitNonNullGroup
@Repeatable(List.class)
@Documented
@Constraint(validatedBy=LimitNonNullGroupValidator.class)
@Target(TYPE)
@Retention(RUNTIME)
public @interface LimitNonNullGroup
Validation annotation to ensure that exactly a specified number of fields are non-null.
This is useful in scenarios where only one (or a specific number of) mutually exclusive fields should be provided in a request or form, for example: providing exactly one contact method.
Usage Example:
@LimitNonNullGroup(
fields = { "field1", "field2" },
limit = 1,
message = "Only one of filed1 or field2 must be provided"
)
@LimitNonNullGroup(
fields = { "field3", "field4" },
limit = 2,
message = "Both filed3 and filed3 must be provided"
)
public class MyObject {
private String field1;
private String field2;
private String field3;
private String field4;
}
Validation Rule:
- Passes if limit
Nof the specified fields are non-null - Fails if fewer or more than
Nare non-null
This annotation must be applied at the class level. It uses reflection to count how many of the specified fields are non-null during validation.
- Author:
- ChatGPT
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interfaceContainer annotation for repeatable use ofLimitNonNullGroup. -
Required Element Summary
Required Elements -
Optional Element Summary
Optional Elements