# Dynamic validation without annotations

**URL:** <https://discourse.hibernate.org/t/dynamic-validation-without-annotations/1688>\
**Category:** Hibernate Validator\
**Created:** [November 12, 2018, 1:22pm UTC](https://discourse.hibernate.org/t/dynamic-validation-without-annotations/1688 "2018-11-12T13:22:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cen](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/cen/32/534_2.png) [@cen](https://discourse.hibernate.org/u/cen)\
**Post date:** [November 12, 2018, 1:22pm UTC](https://discourse.hibernate.org/t/dynamic-validation-without-annotations/1688/1 "2018-11-12T13:22:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gsmet](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/gsmet/32/9_2.png) [@gsmet](https://discourse.hibernate.org/u/gsmet)\
**Post date:** [November 13, 2018, 1:14pm UTC](https://discourse.hibernate.org/t/dynamic-validation-without-annotations/1688/2 "2018-11-13T13:14:24Z")

</div>

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](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](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](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#chapter-groups) for more information about groups.

---

<div class="post-metadata">

**Author:** ![cen](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/cen/32/534_2.png) [@cen](https://discourse.hibernate.org/u/cen)\
**Post date:** [November 13, 2018, 1:27pm UTC](https://discourse.hibernate.org/t/dynamic-validation-without-annotations/1688/3 "2018-11-13T13:27:49Z")

</div>

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!
