Skip to content

Commit

Permalink
Fix ValuesValidator for params wrapped in with block (ruby-grape#2382)
Browse files Browse the repository at this point in the history
* Fix values validator when params wrapped by with block
* Update CHANGELOG
  • Loading branch information
numbata authored Dec 6, 2023
1 parent 3901bf4 commit 4e3b5fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [#2370](https://github.com/ruby-grape/grape/pull/2370): Remove route_xyz method_missing deprecation - [@ericproulx](https://github.com/ericproulx).
* [#2372](https://github.com/ruby-grape/grape/pull/2372): Fix `declared` method for hash params with overlapping names - [@jcagarcia](https://github.com/jcagarcia).
* [#2373](https://github.com/ruby-grape/grape/pull/2373): Fix markdown files for following 1-line format - [@jcagarcia](https://github.com/jcagarcia).
* [#2382](https://github.com/ruby-grape/grape/pull/2382): Fix values validator for params wrapped in `with` block - [@numbata](https://github.com/numbata).
* Your contribution here.

### 2.0.0 (2023/11/11)
Expand Down
7 changes: 6 additions & 1 deletion lib/grape/validations/validators/values_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ def except_message
end

def required_for_root_scope?
@required && @scope.root?
return false unless @required

scope = @scope
scope = scope.parent while scope.lateral?

scope.root?
end

def validation_exception(attr_name, message)
Expand Down
16 changes: 16 additions & 0 deletions spec/grape/validations/validators/values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ def even?(value)
optional :name, type: String, values: %w[a b], allow_blank: true
end
get '/allow_blank'

params do
with(type: String) do
requires :type, values: ValuesModel.values
end
end
get 'values_wrapped_by_with_block'
end
end

Expand Down Expand Up @@ -730,4 +737,13 @@ def app
end
end
end

context 'when wrapped by with block' do
it 'rejects an invalid value' do
get 'values_wrapped_by_with_block'

expect(last_response.status).to eq 400
expect(last_response.body).to eq({ error: 'type is missing, type does not have a valid value' }.to_json)
end
end
end

0 comments on commit 4e3b5fd

Please sign in to comment.