Skip to content

Commit

Permalink
Cleanup code and add to existing tests that fail without these changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mscrivo committed Jul 3, 2023
1 parent 06e2fe5 commit 01f86c1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
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
7 changes: 5 additions & 2 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,12 @@ def validates(attrs, validations)
# type casted values
coerce_type validations, attrs, doc, opts

excluded_keywords = %i[as required param_type is_array format]
# 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]
validations.each do |type, options|
next if excluded_keywords.include?(type)
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

0 comments on commit 01f86c1

Please sign in to comment.