Excluding invalid key value pairs in dry-validation

Is there a way to configure a contract to exclude invalid key-value pairs from a result hash? Currently, the invalid keys are always included. Something like this would be ideal:

Contract = Class.new(Dry::Validation::Contract) do
  params do
    required(:id).filled(:int?)
    optional(:email).filled(:str?, format?: /@/)
  end
end

Contract.new.call(email: 'bademail', id: 1).to_h # => { id: 1 }
1 Like

I came here for the same problem, on 0.13 it happened by default. How to do it happen on newer versions?

@ianks I won’t argue but IIRC this never worked like that because why would it? :smile: It rejects unspecified keys but it leaves everything that the schema defines. This could be done via a plugin though.

It’s a special use case, so it’s fine for it not to be part of the gem. I was able to implement this functionality by enumerating through the errors and deleting keys in the input hash.