Adding @NotNull or Pattern constraints on List<String>
Asked Answered
B

4

21

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("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b.")

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
Blest answered 6/3, 2014 at 18:59 Comment(3)
Why not create an Object rather than String, and enforce the pattern within the object creation. Say, List<EMailInfo> eMailIds; Class EMailInfo (String emailid) .. check for valid, return null if bad; and use method get emailIds in that classAltman
Create a derived class of List, whose add() method override does all that?Fat
Check this #4308724Mccants
P
12

You can create a simple wrapper class for the e-mail String:

public class EmailAddress {

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

    //getters and setters
}

Then mark the field @Valid in your existing object:

@NotNull
@Valid
List<EmailAddress> emailIds;

The validator will then validate each object in the list.

Piggery answered 6/3, 2014 at 19:11 Comment(0)
A
64

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:

Appreciation answered 7/6, 2018 at 20:50 Comment(0)
P
12

You can create a simple wrapper class for the e-mail String:

public class EmailAddress {

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

    //getters and setters
}

Then mark the field @Valid in your existing object:

@NotNull
@Valid
List<EmailAddress> emailIds;

The validator will then validate each object in the list.

Piggery answered 6/3, 2014 at 19:11 Comment(0)
S
9

You don’t have to use any wrapper class just to validate a list of strings. Just use @EachPattern constraint from validator-collection:

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

And that’s all. Easy, right? See this SO answer for more information.

Sloganeer answered 3/4, 2014 at 1:8 Comment(2)
not sure i would advocate to use a third party library just to validate a list of strings either.Drennen
especially since it's not even actively updated anymoreEquites
H
0

In my opinion, use a wrapper class for the object, and have your own verification on the methods:

public class ListWrapper<E> {

    private List<E> list = new ArrayList<>();
    private Pattern check = /*pattern*/;

    public boolean add(E obj) {
        if (this.verify(obj)) {
            return list.add(obj);
        }
        return false;
    }

    //etc

    public boolean verify(E obj) {
        //check pattern and for null
    }

Alternatively, just use a custom object for the list

Heulandite answered 6/3, 2014 at 19:2 Comment(2)
I would rather extend arraylist than wrap it. Right now, methods that accept List<T> as parameter cannot accept this classSchiedam
To each their own, would depend on the use-case I supposeHeulandite

© 2022 - 2024 — McMap. All rights reserved.