Build dry-validations dynamically


Hey guys,

I’ve discovered dry-validation a few days ago and I’m currently trying it out on a project. Now I’ve come to the point where I was thinking about creating schemas dynamically. If I understand correct I can simply create my own schema class with something like:

class Schema < Dry::Validation::Schema::Form
  define! do

Let’s say I would like to pass in which fields of my schema are optional since this differs from case to case. Is there any non hacky way to achieve this?


We have a rather sophisticated mechanism for building schemas dynamically by using a formalized rule AST (provided by dry-logic). The DSL is a front-end for building that AST, but if somebody wants to define a schema dynamically I would recommend building an AST and then it’s a matter of passing it to schema builder. Please let me know if this is something you’d like to do, I can provide some examples.


Thanks for you reply. It’s been a while. But yeah if you would have some examples on how to build an AST, I would appreciate it! I love the idea of dry-validation. Thanks for your amazing work guys!