Created
January 6, 2020 18:50
-
-
Save sesharim/c595825c6aa3de66f29f6b14f02742af to your computer and use it in GitHub Desktop.
dry
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' | |
class NewUserContract < Dry::Validation::Contract | |
params do | |
required(:email).filled(:string) | |
required(:age).value(:integer) | |
end | |
rule(:email) do | |
key.failure('oi mate') if false | |
end | |
rule(:age) do | |
key.failure('must be greater than 18') if value < 18 | |
end | |
end | |
contract = NewUserContract.new | |
pp contract.call(email: 'jane@doe.org', age: '17') | |
# #<Dry::Validation::Result{:email=>"jane@doe.org", :age=>17} errors={:age=>["must be greater than 18"]}> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment