What is the recommended, DRY way to use a contract along with a context?
For example let’s consider a model
Device. Each Device can be a
phone or a
I would like to validate the types differently but they would all share validation as
For example for all types I would like to validate
screen_size, but only for a
pc I would like to validate a
I would like to share (inherit?)
rules as well.
There’s no support for inheritance yet. It’ll be added eventually. For now you can just define rules that are run conditionally.
I’ve tried something like this:
class DeviceContract < Dry::Validation::Contract
key.failure('invalid screen size') if value < 1
class PcContract < DeviceContract
key.failure('invalid keyboard type') if KEYBOARD_TYPES.exclude?(value)
I call the PcContract and it seems to work (it validates
screen_size as well). Further, my experiments shown that it is seems to be possible to overwrite only some of the params defined in the base class and still use the rest.
Is there any contraindication against the above described inheritance? Am I missing something?
Rules are inherited but schemas are not, that’s why in this case it worked for you.