I have an HTML form, thus params sent to server can contain empty strings.
I would like to validate that first or second attribute have to be filled (to do not allow both be empty).
And I cannot find a clear way how to do it.
class Search < Dry::Validation::Contract params do optional(:name).value(:string) optional(:email).value(:string) end rule(:name, :email) do if !key?(:name) && !key?(:email) base.failure("name or email have to be filled in") end end end
The “key?” method returns true for empty strings. How to treat empty strings as nils?