Skip to content

Instantly share code, notes, and snippets.

@bind-disney
Created February 16, 2018 17:33
Show Gist options
  • Save bind-disney/5a10b8803c79995e91f32797d440cddf to your computer and use it in GitHub Desktop.
Save bind-disney/5a10b8803c79995e91f32797d440cddf 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