Home > Mobile >  How to add length restriction in List entries for API model
How to add length restriction in List entries for API model

Time:09-16

I'm implementing an API model and I need to use a List, but I need the elements in the List to have a max size. Is it possible to create this kind of restriction ? How I can make sure that also each entry in the list will have max size 10 ? Should I do the check in the service before adding in the List or is there some other way ?

  public class User{     
       
        @Size(max = 10)
        @Schema(description = "User phone number.")
        private String phoneNumber;
    
        //@Size(max = 10)  I think this will not work for the entries
        @Schema(description = "List - User phone numbers if more than one.")
        private List<String> phoneNumberList;
    }

CodePudding user response:

just use @Size(max)
follow document, it support Collection (collection size is evaluated)

CodePudding user response:

You can use such code:

public class User{     
       
        @Size(max = 10)
        @Schema(description = "User phone number.")
        private String phoneNumber;
    
        @Schema(description = "List - User phone numbers if more than one.")
        private List<@Size(max = 10) String> phoneNumberList;
    }
  • Related