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 2 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
8 changes: 4 additions & 4 deletions .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-05-27 20:47:15 UTC using RuboCop version 1.50.2.
# on 2023-07-03 21:04:28 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 Expand Up @@ -324,7 +324,7 @@ RSpec/MessageChain:
Exclude:
- 'spec/grape/middleware/formatter_spec.rb'

# Offense count: 136
# Offense count: 139
# Configuration parameters: .
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
Expand All @@ -335,7 +335,7 @@ RSpec/MissingExampleGroupArgument:
Exclude:
- 'spec/grape/middleware/exception_spec.rb'

# Offense count: 765
# Offense count: 767
# Configuration parameters: Max.
RSpec/MultipleExpectations:
Exclude:
Expand Down Expand Up @@ -478,7 +478,7 @@ RSpec/NamedSubject:
- 'spec/grape/validations/validators/presence_spec.rb'
- 'spec/grape/validations_spec.rb'

# Offense count: 174
# Offense count: 171
# Configuration parameters: Max, AllowedGroups.
RSpec/NestedGroups:
Exclude:
Expand Down
6 changes: 5 additions & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,12 @@ def validates(attrs, validations)
# type casted values
coerce_type validations, attrs, doc, opts

# 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_keywords = %i[as required param_type is_array format example]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a constant RESERVED_KEYWORDS. Maybe we should also rename it? These are documentation options so RESERVED_DOCUMENTATION_KEYWORDS? WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me!

validations.each do |type, options|
next if type == :as
next if reserved_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
Loading