I am using Spring Boot (2.7.6) and javax.validation and swagger v3.
My request JSON body of this form
{
"itemDescription":"vehicle",
"itemVersion":0,
"itemCode":{
"codeType":"1",
"codeValue":"1111"
}
}
The above JSON body is represented by 2 models ItemRequest
and ItemCode
as:
import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ItemRequest {
@NotNull
@Schema(description = "Item description"
, requiredMode = Schema.RequiredMode.REQUIRED)
private String itemDescription;
@NotNull
@Schema(description = "Unique Item Code consisting of codeType integer and codeValue string"
, requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private ItemCode itemCode;
@Min(value = 0)
@Schema(description = "Item version'
, requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private int itemVersion;
}
, and its nested JSON model ItemCode
:
import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.NotNull;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class ItemCode {
@NotNull
@Schema(description="codeType can be 1, 2, or 3"
, requiredMode = Schema.RequiredMode.REQUIRED))
private int codeType;
@NotNull
@Schema(description="Unique itemCode identifier. Can start with 0. If codeType=1, 3-digits long, if codeType=2, 4-digits long, if codeTpye=3, 5-digits long.",
, requiredMode = Schema.RequiredMode.REQUIRED)
private String codeValue; // for example 021, 5455, 05324
}
How do I validate ItemCode
field in ItemRequest
model so that validation does what descriptions for ItemCode
state?