Skip to content

Commit

Permalink
Feature: Allows procs with arity 1 to validate and use custom messages (
Browse files Browse the repository at this point in the history
#2333)

* refactor: validate_param! method

- now allows a proc to validate and use custom messages
- adds @proc_message instance variable
- creates the skip_validation? method to pass rubocops cyclomatic error

* Adds changelog entry

* Adds PR # to the changelog

* Properly formats the changelog line

* Places the changelog line in the correct order

* Adds default changelog message for future use

* Edits the changelog with a better description

* refactor: allows procs with an arity of 1

* Adds a test for when arity is > 1
  • Loading branch information
TheDevJoao committed Jun 5, 2023
1 parent 3b7901c commit d1dfdcc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
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'
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

0 comments on commit d1dfdcc

Please sign in to comment.