Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix macros expecting a range as option #616

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/dry/validation/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,18 @@ def parse_macros(*args)
args.each_with_object([]) do |spec, macros|
case spec
when Hash
spec.each { |k, v| macros << [k, Array(v)] }
add_macro_from_hash(macros, spec)
else
macros << Array(spec)
end
end
end

def add_macro_from_hash(macros, spec)
spec.each do |k, v|
macros << [k, v.is_a?(Array) ? v : [v]]
end
end
end
end
end
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