Skip to content

Instantly share code, notes, and snippets.

@bind-disney
Created February 16, 2018 17:54
Show Gist options
  • Save bind-disney/661aab7141867551d36560e7bb6780bc to your computer and use it in GitHub Desktop.
Save bind-disney/661aab7141867551d36560e7bb6780bc to your computer and use it in GitHub Desktop.
require 'dry-validation'
ThreeDigits = /^\d{3}$/
FourDigits = /^\d{4}$/
schema = Dry::Validation.Schema do
required(:field_1).filled(:str?)
required(:field_2).filled(:str?)
rule(field_2_depends_on_field_1: [:field_1, :field_2]) do |field_1, field_2|
field_1.eql?('Foo').then(field_2.format?(ThreeDigits)) & field_1.eql?('Bar').then(field_2.format?(FourDigits))
end
end
puts schema.call(field_1: 'Foo', field_2: '999').success?
puts schema.call(field_1: 'Bar', field_2: '999').success?
puts schema.call(field_1: 'Foo', field_2: '9999').success?
puts schema.call(field_1: 'Bar', field_2: '9999').success?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment