Skip to content

Instantly share code, notes, and snippets.

@dsisnero
Forked from solnic/dry_v_either_filled.rb
Created March 15, 2017 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsisnero/fa2cf9af6b35a3bd67501f733c2c97b8 to your computer and use it in GitHub Desktop.
Save dsisnero/fa2cf9af6b35a3bd67501f733c2c97b8 to your computer and use it in GitHub Desktop.
require 'dry-validation'
schema = Dry::Validation.Schema do
configure do
def self.messages
super.merge(en: { errors: { foo: 'either a or b can be filled' } })
end
end
required(:a).maybe
required(:b).maybe
rule(foo: %i[a b]) do |a, b|
a.filled? ^ b.filled?
end
end
puts schema.(a: 'huh', b: 'heh').errors.inspect
# {:foo=>["either a or b can be filled"]}
puts schema.(a: 'huh', b: nil).errors.inspect
# {}
puts schema.(a: nil, b: 'heh').errors.inspect
# {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment