I was wondering if anyone else has found a need to validate against the presence of a key in a hash.
Right now dry-validation has a macro for required(...)
and optional(...)
. I think it would be nice to have another macro like forbidden(...)
(or some other word, like exclude
or refute
) that would fail validation if the key given was present in the input hash.
Here’s a use-case:
I am validating a settings hash. The hash can be updated but some keys are restricted.
If the input hash contains a restricted key I want to return an error letting the user know that they cannot modify that key.
Right now the closest I can get is optional(:key).value(:none?)
but this isn’t the desired behavior because passing restricted_key: nil
should also be forbidden.
I am going to implement a custom rule, but I was curious to see if there is a cleaner way to validate against the presence of a key.