Created
November 26, 2019 09:22
-
-
Save katafrakt/9e09ca7447371996e9c4ae205813b330 to your computer and use it in GitHub Desktop.
dynamic dry-v contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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