Skip to content

Instantly share code, notes, and snippets.

@zhum
Forked from bind-disney/dry-rules.rb
Created February 17, 2018 06:33
Show Gist options
  • Save zhum/17b4242edcd68f1db26391dcfe5ea910 to your computer and use it in GitHub Desktop.
Save zhum/17b4242edcd68f1db26391dcfe5ea910 to your computer and use it in GitHub Desktop.
require 'dry-validation'
schema = Dry::Validation.Schema do
required(:field_1).filled(:str?)
required(:field_2).filled(:int?)
rule(field_2_depends_on_field_1: [:field_1, :field_2]) do |field_1, field_2|
field_1.eql?('Foo').then(field_2.eql?(0)) & field_1.eql?('Bar').then(field_2.eql?(1))
end
end
puts schema.call(field_1: 'Foo', field_2: 0).success?
puts schema.call(field_1: 'Bar', field_2: 0).success?
puts schema.call(field_1: 'Foo', field_2: 1).success?
puts schema.call(field_1: 'Bar', field_2: 1).success?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment