Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* [#2733](https://github.com/ruby-grape/grape/pull/2733): Drop the dead `active_support/core_ext/hash/reverse_merge` require; call `ActiveSupport::HashWithIndifferentAccess.new(...)` directly at call sites - [@ericproulx](https://github.com/ericproulx).
* [#2734](https://github.com/ruby-grape/grape/pull/2734): Extract `options_route_enabled` from the Endpoint options Hash into a dedicated `attr_accessor` - [@ericproulx](https://github.com/ericproulx).
* [#2736](https://github.com/ruby-grape/grape/pull/2736): Collapse `Endpoint#run_validators` rescue branches via `ValidationErrors` flatten; `ValidationErrors#initialize` keyword renamed `errors:` → `exceptions:` - [@ericproulx](https://github.com/ericproulx).
* [#2746](https://github.com/ruby-grape/grape/pull/2746): Hoist `using:` / `except:` from `**opts` to explicit kwargs on `DSL::Parameters#requires` and `#optional` - [@ericproulx](https://github.com/ericproulx).
* [#2742](https://github.com/ruby-grape/grape/pull/2742): Prune unused requires in `lib/grape.rb`; narrow `active_support/inflector` to `core_ext/string/inflections` - [@ericproulx](https://github.com/ericproulx).
* [#2741](https://github.com/ruby-grape/grape/pull/2741): Readability pass: guard clauses and small extractions across `lib/` - [@ericproulx](https://github.com/ericproulx).
* [#2740](https://github.com/ruby-grape/grape/pull/2740): Lazy-allocate `@api_class` and `@point_in_time_copies` on `Grape::Util::InheritableSetting` so unused settings layers don't carry an empty Hash and empty Array each - [@ericproulx](https://github.com/ericproulx).
Expand Down
8 changes: 4 additions & 4 deletions lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ def use(*names, **options)
# requires :name, type: String
# end
# end
def requires(*attrs, **opts, &block)
def requires(*attrs, using: nil, except: nil, **opts, &block)
opts[:presence] = { value: true, message: opts[:message] }
opts = @group.deep_merge(opts) if @group

return require_required_and_optional_fields(attrs.first, using: opts[:using], except: opts[:except]) if opts[:using]
return require_required_and_optional_fields(attrs.first, using:, except:) if using

validate_attributes(attrs, **opts, &block)
block ? new_scope(attrs.first, type: opts[:type], as: opts[:as], &block) : push_declared_params(attrs, as: opts[:as])
Expand All @@ -136,7 +136,7 @@ def requires(*attrs, **opts, &block)
# endpoint.
# @param (see #requires)
# @option (see #requires)
def optional(*attrs, **opts, &block)
def optional(*attrs, using: nil, except: nil, **opts, &block)
type = opts[:type]
opts = @group.deep_merge(opts) if @group

Expand All @@ -146,7 +146,7 @@ def optional(*attrs, **opts, &block)
raise Grape::Exceptions::UnsupportedGroupType unless Grape::Validations::Types.group?(type)
end

return require_optional_fields(attrs.first, using: opts[:using], except: opts[:except]) if opts[:using]
return require_optional_fields(attrs.first, using:, except:) if using

validate_attributes(attrs, **opts, &block)
block ? new_scope(attrs.first, type: opts[:type], as: opts[:as], optional: true, &block) : push_declared_params(attrs, as: opts[:as])
Expand Down
Loading