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

Values Validator => Proc's arity < 2 ? #2401

Open
ericproulx opened this issue Jan 3, 2024 · 3 comments
Open

Values Validator => Proc's arity < 2 ? #2401

ericproulx opened this issue Jan 3, 2024 · 3 comments
Labels

Comments

@ericproulx
Copy link
Contributor

ericproulx commented Jan 3, 2024

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_one and :input_two even 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.

@ericproulx ericproulx changed the title Values Validator arity > 1 ? Values Validator => Proc.arity < 2 ? Jan 3, 2024
@ericproulx ericproulx changed the title Values Validator => Proc.arity < 2 ? Values Validator => Proc's arity < 2 ? Jan 3, 2024
@dblock
Copy link
Member

dblock commented Jan 4, 2024

Change the code to be v1 + v2 == 5, does it pass? If not it's a bug ;)

@dblock dblock added the bug? label Jan 4, 2024
@ericproulx
Copy link
Contributor Author

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

@dblock
Copy link
Member

dblock commented Jan 5, 2024

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants