Skip to content

Instantly share code, notes, and snippets.

@katafrakt
Created November 26, 2019 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katafrakt/9e09ca7447371996e9c4ae205813b330 to your computer and use it in GitHub Desktop.
Save katafrakt/9e09ca7447371996e9c4ae205813b330 to your computer and use it in GitHub Desktop.
dynamic dry-v contract
require 'dry/validation'
# fake dynamic fields
dynamic_fields = ->() { %i[email name] }
contract = Class.new(Dry::Validation::Contract) do
params do
dynamic_fields.().each do |f|
required(f).filled(:string)
end
end
end
contract.new.call("email" => 1, "name" => "john").success? # => false
contract.new.call("name" => "john").success? # => false
contract.new.call("name" => "john", "email" => "test@email.com").success? # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment