Skip to content

Commit

Permalink
Fix Set type with values
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolai-b committed Oct 23, 2024
1 parent f4e2af5 commit 6e7595a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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?(Array) || coerce_type.is_a?(Set)
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
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

0 comments on commit 6e7595a

Please sign in to comment.