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

Fix Set type with values #2507

Merged
merged 1 commit into from
Oct 23, 2024
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

* [#2504](https://github.com/ruby-grape/grape/pull/2504): Fix leaky modules in specs - [@ericproulx](https://github.com/ericproulx).
* [#2506](https://github.com/ruby-grape/grape/pull/2506): Fix fetch_formatter api_format - [@ericproulx](https://github.com/ericproulx).
* [#2507](https://github.com/ruby-grape/grape/pull/2507): Fix type: Set with values - [@nikolai-b](https://github.com/nikolai-b).
* Your contribution here.

### 2.2.0 (2024-09-14)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def validate(type, options, attrs, doc, opts)
def validate_value_coercion(coerce_type, *values_list)
return unless coerce_type

coerce_type = coerce_type.first if coerce_type.is_a?(Array)
coerce_type = coerce_type.first if coerce_type.is_a?(Enumerable)
values_list.each do |values|
next if !values || values.is_a?(Proc)

Expand Down
18 changes: 18 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,24 @@ def initialize(value)
end
end

context 'a Set with coerce type explicitly given' do
context 'and the values are allowed' do
it 'does not raise an exception' do
expect do
subject.params { optional :numbers, type: Set[Integer], values: 0..2, default: 0..2 }
end.not_to raise_error
end
end

context 'and the values are not allowed' do
it 'raises exception' do
expect do
subject.params { optional :numbers, type: Set[Integer], values: %w[a b] }
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
end
end
end

context 'with range values' do
context "when left range endpoint isn't #kind_of? the type" do
it 'raises exception' do
Expand Down