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 unknown validator exception when using requires/optional with Entity #2338

Merged
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
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 5000`
# on 2023-07-01 15:43:53 UTC using RuboCop version 1.50.2.
# on 2023-07-04 00:22:04 UTC using RuboCop version 1.50.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [#2333](https://github.com/ruby-grape/grape/pull/2333): Use custom messages in parameter validation with arity 1 - [@thedevjoao](https://github.com/TheDevJoao).
* [#2341](https://github.com/ruby-grape/grape/pull/2341): Stop yielding skip value - [@ericproulx](https://github.com/ericproulx).
* [#2342](https://github.com/ruby-grape/grape/pull/2342): Allow specifying a handler for grape_exceptions - [@mscrivo](https://github.com/mscrivo).
* [#2338](https://github.com/ruby-grape/grape/pull/2338): Fix unknown validator when using requires/optional with entity - [@mscrivo](https://github.com/mscrivo).
* Your contribution here.

#### Fixes
Expand Down
8 changes: 7 additions & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class ParamsScope

include Grape::DSL::Parameters

# There are a number of documentation options on entities that don't have
# corresponding validators. Since there is nowhere that enumerates them all,
# we maintain a list of them here and skip looking up validators for them.
RESERVED_DOCUMENTATION_KEYWORDS = %i[as required param_type is_array format example].freeze

class Attr
attr_accessor :key, :scope

Expand Down Expand Up @@ -359,7 +364,8 @@ def validates(attrs, validations)
coerce_type validations, attrs, doc, opts

validations.each do |type, options|
next if type == :as
# Don't try to look up validators for documentation params that don't have one.
next if RESERVED_DOCUMENTATION_KEYWORDS.include?(type)

validate(type, options, attrs, doc, opts)
end
Expand Down
13 changes: 7 additions & 6 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,12 @@ def define_optional_using
context 'requires :all using Grape::Entity documentation' do
def define_requires_all
documentation = {
required_field: { type: String },
optional_field: { type: String }
required_field: { type: String, required: true, param_type: 'query' },
optional_field: { type: String },
optional_array_field: { type: Array[String], is_array: true }
}
subject.params do
requires :all, except: :optional_field, using: documentation
requires :all, except: %i[optional_field optional_array_field], using: documentation
end
end
before do
Expand All @@ -195,7 +196,7 @@ def define_requires_all

it 'adds entity documentation to declared params' do
define_requires_all
expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq(%i[required_field optional_field])
expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq(%i[required_field optional_field optional_array_field])
end

it 'errors when required_field is not present' do
Expand All @@ -214,8 +215,8 @@ def define_requires_all
context 'requires :none using Grape::Entity documentation' do
def define_requires_none
documentation = {
required_field: { type: String },
optional_field: { type: String }
required_field: { type: String, example: 'Foo' },
optional_field: { type: Integer, format: 'int64' }
}
subject.params do
requires :none, except: :required_field, using: documentation
Expand Down