Skip to content

Feature: Allows procs with arity 1 to validate and use custom messages #2333

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

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#2330](https://github.com/ruby-grape/grape/pull/2330): Use ActiveSupport inflector - [@ericproulx](https://github.com/ericproulx).
* [#2331](https://github.com/ruby-grape/grape/pull/2331): Memory optimization when running validators - [@ericproulx](https://github.com/ericproulx).
* [#2332](https://github.com/ruby-grape/grape/pull/2332): Use ActiveSupport configurable - [@ericproulx](https://github.com/ericproulx).
* [#2333](https://github.com/ruby-grape/grape/pull/2333): Use custom messages in parameter validation with arity 1 - [@thedevjoao](https://github.com/TheDevJoao).
* Your contribution here.

#### Fixes
Expand Down
13 changes: 12 additions & 1 deletion lib/grape/validations/validators/values_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def validate_param!(attr_name, params)
unless check_values(param_array, attr_name)

raise validation_exception(attr_name, message(:values)) \
if @proc && !param_array.all? { |param| @proc.call(param) }
if @proc && !validate_proc(@proc, param_array)
end

private
Expand All @@ -68,6 +68,17 @@ def check_excepts(param_array)
param_array.none? { |param| excepts.include?(param) }
end

def validate_proc(proc, param_array)
case proc.arity
when 0
param_array.all? { |_param| proc.call }
when 1
param_array.all? { |param| proc.call(param) }
else
raise ArgumentError, 'proc arity must be 0 or 1'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we get some kind of useful call stack with a line number such as that the developer knows where the error comes from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, considering the ruby and rails versions that grape supports, the stack trace will show every sequence leading to this raise statement.

end
end

def except_message
options = instance_variable_get(:@option)
options_key?(:except_message) ? options[:except_message] : message(:except_values)
Expand Down
37 changes: 37 additions & 0 deletions spec/grape/validations/validators/values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def add_except(except)
def include?(value)
values.include?(value)
end

def even?(value)
value.to_i.even?
end
end
end
end
Expand Down Expand Up @@ -241,6 +245,18 @@ def include?(value)
end
get '/proc/message'

params do
requires :number, values: { value: ->(v) { ValuesModel.even? v }, message: 'must be even' }
end
get '/proc/custom_message' do
{ message: 'success' }
end

params do
requires :input_one, :input_two, values: { value: ->(v1, v2) { v1 + v2 > 10 } }
end
get '/proc/arity2'

params do
optional :name, type: String, values: %w[a b], allow_blank: true
end
Expand Down Expand Up @@ -692,5 +708,26 @@ def app
expect(last_response.status).to eq 400
expect(last_response.body).to eq({ error: 'type failed check' }.to_json)
end

context 'when proc has an arity of 1' do
it 'accepts a valid value' do
get '/proc/custom_message', number: 4
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ message: 'success' }.to_json)
end

it 'rejects an invalid value' do
get '/proc/custom_message', number: 5
expect(last_response.status).to eq 400
expect(last_response.body).to eq({ error: 'number must be even' }.to_json)
end
end

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
end
end