Skip to content
Open
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 @@ -43,6 +43,7 @@
* [#2726](https://github.com/ruby-grape/grape/pull/2726): Reuse one `AttributesIterator` per validator and drop the unused `Enumerable` mixin - [@ericproulx](https://github.com/ericproulx).
* [#2728](https://github.com/ruby-grape/grape/pull/2728): Deprecate passing a positional options Hash to `auth`/`http_basic`/`http_digest`; pass keyword arguments instead - [@ericproulx](https://github.com/ericproulx).
* [#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).
* [#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).
* Your contribution here.

#### Fixes
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