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

Allows values to be a kind of the coerced type in range #885

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/grape/validations/validators/values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def validate_param!(attr_name, params)
return unless params[attr_name] || required_for_root_scope?

values = @values.is_a?(Proc) ? @values.call : @values
values = values.to_a if values.is_a? Range
param_array = params[attr_name].nil? ? [nil] : Array.wrap(params[attr_name])
unless (param_array - values).empty?
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :values
Expand Down
13 changes: 13 additions & 0 deletions spec/grape/validations/validators/values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class API < Grape::API
{ type: params[:type] }
end

params do
requires :type, type: Integer, desc: 'An integer', values: 10..11, default: 10
end
get '/range' do
{ type: params[:type] }
end

params do
requires :type, type: Array[Integer], desc: 'An integer', values: [10, 11], default: 10
end
Expand Down Expand Up @@ -180,6 +187,12 @@ def app
expect(last_response.body).to eq({ type: 10 }.to_json)
end

it 'allows values to be a kind of the coerced type not just an instance of it' do
get('/range', type: 10)
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ type: 10 }.to_json)
end

it 'allows values to be a kind of the coerced type in an array' do
get('/values/array_coercion', type: [10])
expect(last_response.status).to eq 200
Expand Down