简体   繁体   中英

Custom bean validation with tow types using ConstraintValidator

I am following this tutorial https://www.baeldung.com/spring-mvc-custom-validator

I am facing now a situation where i have two different types, one of them is List<String> and the other one is in other different class is of type Set<String>

And i want to check the validation of the string elements inside of them using regex. ^[a-z0-9]+[-_]*[a-z0-9]*$

So instead of having two bean validation annotation one is with List<String>

implements 
  ConstraintValidator<ContactNumberConstraint, List<String>>

and the other one is with Set<String>

implements 
  ConstraintValidator<ContactNumberConstraint, Set<String>>

Is it possible to have one class accepting both types? If yes a code snippet will be very helpful for me

Since both List and Set are Collection s and you don't care about specifics of its implementations (you only intend to iterate through elements and validate them) you could simply use implements ConstraintValidator<ContactNumberConstraint, Collection<String>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM