How can I validate a collection is empty?
// should be empty!
@JsonbTransient
@JsonIgnore
@XmlAnyAttribute
private Map<QName, Object> unknownAttributes;
// should be empty!
@JsonbTransient
@JsonIgnore
@XmlAnyElement
private List<Element> unknownElements;
I know @NotEmpty can help for apposite validation.
I currently doing this.
@AssertTrue(message = "unknown attributes should be empty")
private boolean isUnknownAttributesEmpty() {
if (unknownAttributes == null) {
return true;
}
return unknownAttributes.isEmpty();
}
@AssertTrue(message = "unknown elements should be empty")
private boolean isUnknownElementsEmpty() {
if (unknownElements == null) {
return true;
}
return unknownElements.isEmpty();
}
Is there any good way to do this? Why there is no @Empty, or is there?
jakarta.validation.constraints.Size
You can use @Size as Jakarta Validation annotation to validate the size of a collection, map, array or charSequence.