Skip to content

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

Description

@ericproulx

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions