Created
February 16, 2018 17:54
-
-
Save bind-disney/661aab7141867551d36560e7bb6780bc to your computer and use it in GitHub Desktop.
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' | |
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