'Spring validation using dynamic values (perhaps read from a db)

our code base has DTOs with fields which are validated using Spring validations. These DTO's and validations are shared between several different projects/micro services as a library jar.

We have a new use case which requires one of the validations to check against a list of valid string values.

This lists, however, changes all the time so I can't just be written into the code and we are hoping it is something we can read dynamically, from a DB or something.

If the validation were only used within one project this would be pretty easy. Just add the data to that projects db.

However, is there a pattern/technique to doing this when the validations are a shared lib?

Thank you!



Solution 1:[1]

If the data comes from a DB it can not be hardcoded in a lib.

I would code a normal Spring Boot validator that uses JPA to retrieve the data.

Code example (without the @Entity): https://www.baeldung.com/spring-boot-bean-validation

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Raul Lapeira Herrero