Values Validator => Proc's arity < 2 ?
While running specs with --warnings I found an interesting API endpoint under spec/grape/validations/validators/values_spec.rb with a successful test but maybe not the way we wanted.
The api is defined like this
params do
requires :input_one, :input_two, type: Integer, values: { value: ->(v1, v2) { v1 + v2 > 10 } }
end
get '/proc/arity2'
And the test goes like this
context 'when arity is > 1' do
it 'returns an error status code' do
get '/proc/arity2', input_one: 2, input_two: 3
expect(last_response.status).to eq 400
end
end
It will pass but it also emits 2 warnings
Error 'wrong number of arguments (given 1, expected 2)' raised while validating attribute 'input_one'
Error 'wrong number of arguments (given 1, expected 2)' raised while validating attribute 'input_two'
If we take a closer look a the code
- Proc's arity is expected to be < 2
- each params a process individually (SingleAttributeIterator) so there's no relation between
:input_oneand:input_twoeven if it seems to be.
I was wondering if its really a bug. Also, I think we should raise more awareness with proper error instead of a warning.
Change the code to be v1 + v2 == 5, does it pass? If not it's a bug ;)
Change the code to be
v1 + v2 == 5, does it pass? If not it's a bug ;)
It just doesn't work since arity is set to 1 and its not a MultipleAttributeIterator
Change the code to be
v1 + v2 == 5, does it pass? If not it's a bug ;)It just doesn't work since arity is set to 1 and its not a MultipleAttributeIterator
Obvious bug then :)