Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

java - Adding @NotNull or Pattern constraints on List<String>

How can we ensure the individual strings inside a list are not null/blank or follow a specific pattern

@NotNull
List<String> emailIds;

I also want to add a pattern

@Pattern("[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}.")

but I can live without it.But I would definitely like to have a constraint which will check if any strings inside a list are null or blank. Also how would the Json schema look like

"ids": {
      "description": "The  ids associated with this.", 
    "type": "array",
        "minItems": 1,
        "items": {
        "type": "string",
         "required" :true }
 }

"required" :true does not seem to do the job
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Bean validation 2.0 (Hibernate Validator 6.0.1 and above) supports validating container elements by annotating type arguments of parameterized types. Example:

List<@Positive Integer> positiveNumbers;

Or even (although a bit busy):

List<@NotNull @Pattern(regexp="\b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b") String> emails;

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...