Skip to content

Instantly share code, notes, and snippets.

@sesharim
Created January 6, 2020 18:50
Show Gist options
  • Save sesharim/c595825c6aa3de66f29f6b14f02742af to your computer and use it in GitHub Desktop.
Save sesharim/c595825c6aa3de66f29f6b14f02742af to your computer and use it in GitHub Desktop.
dry
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