One feature I’ve notice dry-validation (or rather dry-schema) lacks is the ability to compose a schema from previously defined chunks.
In plain-old Ruby, common functionality can be extracted into modules and added to other objects using
prepend. In a Rails app, Concerns build upon that paradigm.
To give an example of the problem, one of the applications I’m working on has 30 or so instances of this:
params do optional(:page).value(:integer, gteq?: 1) optional(:per_page).value(:integer, included_in?: [10, 25, 50]) # Other parameters ... end
Ideally, this would be pulled out into a
Pagination fragment or similar, and then be reused across all of the contracts.
params do fragment(:pagination) # Other parameters ... end
I’ve hacked out something that does work, the ergonomics are rather suboptimal:
module Pagination def self.extended(object) super(object) object.instance_eval do optional(:page).value(:integer, gteq?: 1) optional(:per_page).value(:integer, included_in?: [10, 25, 50]) end end end
Then, in the contract:
params do extend Pagination # Other parameters ... end
It would be nice for to have a cleaner DSL available to do this since feature extraction plays a major part in DRYing up a codebase.