Skip to content

Commit

Permalink
Fix macros expecting a range as option
Browse files Browse the repository at this point in the history
References #608
  • Loading branch information
waiting-for-dev committed Jan 15, 2020
1 parent eaaee30 commit a3b32af
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/dry/validation/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ def parse_macros(*args)
args.each_with_object([]) do |spec, macros|
case spec
when Hash
spec.each { |k, v| macros << [k, Array(v)] }
spec.each do |k, v|
macros << [
k,
v.is_a?(Array) ? v : [v]
]
end
else
macros << Array(spec)
end
Expand Down
25 changes: 25 additions & 0 deletions spec/integration/macros/custom_macros_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,29 @@ class Test::BaseContract < Dry::Validation::Contract; end
.to eql(numbers: ['must have at least 3 items'])
end
end

context 'using a macro with a range option' do
before do
Test::BaseContract.register_macro(:in_range) do |macro:|
range = macro.args[0]

all_included_in_range = value.all? { |elem| range.include?(elem) }
key.failure("every item must be included in #{range}") unless all_included_in_range
end

contract_class.rule(:numbers).validate(in_range: 1..3)
end

after do
Test::BaseContract.macros._container.delete('in_range')
end

it 'succeeds with valid input' do
expect(contract.(numbers: [1, 2, 3])).to be_success
end

it 'fails with invalid input' do
expect(contract.(numbers: [1, 2, 6])).to be_failure
end
end
end

0 comments on commit a3b32af

Please sign in to comment.