Estou usando a validação de dados do Spring e quero validar Set
ou List
de Date
, mas todos os exemplos que vi antes estavam validando um único valor. Aqui está a representação da classe de solicitação
CircuitJurisdictionRequest.java
package eg.intercom.ppo.revamp.model.to.request;
import com.fasterxml.jackson.annotation.JsonFormat;
import eg.intercom.ppo.revamp.model.enums.AppliedLawEnum;
import eg.intercom.ppo.revamp.model.enums.TimeStatusEnum;
import eg.intercom.ppo.revamp.model.to.RequestActivationStatus;
import jakarta.validation.constraints.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.util.Date;
import java.util.Set;
@Data
@NoArgsConstructor
public class CircuitJurisdictionRequest {
@NotNull(message = "Circuit ID is required")
@Positive(message = "Circuit ID Must Be >= 1")
private Long circuitId;
@NotNull(message = "Table type ID is required")
@Positive(message = "Table type ID Must Be >= 1")
private Long tableTypeId;
@NotNull(message = "Court room ID is required")
@Positive(message = "Court room ID Must Be >= 1")
private Long courtRoomId;
@NotNull(message = "Applied law is required")
private AppliedLawEnum appliedLaw;
@NotNull(message = "Time status is required")
private TimeStatusEnum timeStatus;
@Size(max = 10, message = "Session day should not exceed 10 characters")
@Pattern(
regexp = "^(SUNDAY|MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY)$",
message = "Session day must be a valid day of the week (e.g., SUNDAY, MONDAY, etc.)"
)
private String sessionDay;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private Set<@NotNull(message = "Each date in Session Scheduler must not be null") Date> sessionScheduler;
@NotNull(message = "new Cases Capacity is required")
@Min(value = 0, message = "New cases capacity must be zero or positive")
@Max(value = 1000, message = "New cases capacity should not exceed 1000")
private Integer newCasesCapacity;
@NotNull(message = "postponed Cases Capacity is required")
@Min(value = 0, message = "Postponed cases capacity must be zero or positive")
@Max(value = 1000, message = "Postponed cases capacity should not exceed 1000")
private Integer postponedCasesCapacity;
@NotNull(message = "first Session Date is required")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private LocalDate firstSessionDate; // Optionally add @Future or @NotNull if required
@NotEmpty(message = "At least one digital Jurisdiction is required")
private Set<@Min(value = 0, message = "Digital Jurisdiction Must be >= 0")
@Max(value = 9, message = "Digital Jurisdiction Must be <= 9") Integer> digitalJurisdiction;
private RequestActivationStatus activationStatus;
}
O que eu quero
quero validar todas as datas sessionScheduler
para serem enviadas na solicitação como string json com formatodd-MM-yyyy
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private Set<@NotNull(message = "Each date in Session Scheduler must not be null") Date> sessionScheduler;
Para validar uma coleção de datas com um formato específico (
dd-MM-yyyy
) no Spring Boot, você precisará combinar@JsonFormat
for datedeserialization
e acustom validator
para garantir que cada data siga o formato desejado1) Defina a anotação ValidDateFormat para validação
2) Defina DateFormatValidator
3) Aplicar
Validator
ao sessionScheduler