Dynamic validation without annotations


I need to do a completely dynamic validation where I can specify to validate each field plus a set of constraints to validate the field against. I cannot use the annotations at all. I know validateProperty() exists but I don’t see how I can tell it which constraints need to be used. I need the following API:

validator.validateProperty(object, “property”, Default, {NotNull.class, CustomCheckAddress.class})

Can this be achieved somehow with current API?

The reason I can’t use annotations:
-validation is needed on a POJO lib which must not pull any other maven dependencies
-same property can be nullable or notnull depending on the API endpoint being called…therefore need for dynamic validation


You can either define the constraints via the programmatic API (https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-programmatic-api) or via XML (https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-mapping-xml-constraints).

You can’t enable or disable constraints so you would have to redefine the metadata each time at runtime, which is not a good idea.

If I were you, I would use groups: e.g. define the constraints once and for all with groups and when validating enable one or the other group depending on what you need.

See https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#chapter-groups for more information about groups.


The programmatic API looks like exactly what I need. I can just make an ApplicationScoped Validator producer for each configuration I need and inject them both via qualifiers. This way the config will only be setup twice on startup and not on each request. I think it will work out… thanks!