Module: Grape::DSL::Parameters
- Extended by:
- ActiveSupport::Concern
- Included in:
- Validations::ParamsScope
- Defined in:
- lib/grape/dsl/parameters.rb
Overview
Defines DSL methods, meant to be applied to a ParamsScope, which define and describe the parameters accepted by an endpoint, or all endpoints within a namespace.
Instance Method Summary collapse
-
#all_or_none_of(*attrs) ⇒ Object
Require that either all given params are present, or none are.
-
#at_least_one_of(*attrs) ⇒ Object
Require at least one of the given parameters to be present.
-
#build_with(build_with = nil) ⇒ Object
Set the module used to build the request.params.
-
#declared_param?(param) ⇒ Boolean
Test for whether a certain parameter has been defined in this params block yet.
-
#exactly_one_of(*attrs) ⇒ Object
Require exactly one of the given parameters to be present.
-
#given(*attrs) { ... } ⇒ Object
Define a block of validations which should be applied if and only if the given parameter is present.
- #map_params(params, element) ⇒ Object
-
#mutually_exclusive(*attrs) ⇒ Object
Disallow the given parameters to be present in the same request.
-
#optional(*attrs, &block) ⇒ Object
Allow, but don’t require, one or more parameters for the current endpoint.
-
#params(params) ⇒ Object
private
Hash of parameters relevant for the current scope.
-
#requires(*attrs, &block) ⇒ Object
(also: #group)
Require one or more parameters for the current endpoint.
-
#use(*names) ⇒ Object
(also: #use_scope, #includes)
Include reusable params rules among current.
-
#with(*attrs, &block) ⇒ Object
Define common settings for one or more parameters.
Instance Method Details
#all_or_none_of(*attrs) ⇒ Object
Require that either all given params are present, or none are.
191 192 193 |
# File 'lib/grape/dsl/parameters.rb', line 191 def all_or_none_of(*attrs) validates(attrs, all_or_none_of: { value: true, message: (attrs) }) end |
#at_least_one_of(*attrs) ⇒ Object
Require at least one of the given parameters to be present.
185 186 187 |
# File 'lib/grape/dsl/parameters.rb', line 185 def at_least_one_of(*attrs) validates(attrs, at_least_one_of: { value: true, message: (attrs) }) end |
#build_with(build_with = nil) ⇒ Object
Set the module used to build the request.params.
33 34 35 |
# File 'lib/grape/dsl/parameters.rb', line 33 def build_with(build_with = nil) @api.namespace_inheritable(:build_params_with, build_with) end |
#declared_param?(param) ⇒ Boolean
Test for whether a certain parameter has been defined in this params block yet.
213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/grape/dsl/parameters.rb', line 213 def declared_param?(param) if lateral? # Elements of @declared_params of lateral scope are pushed in @parent. So check them in @parent. @parent.declared_param?(param) else # @declared_params also includes hashes of options and such, but those # won't be flattened out. @declared_params.flatten.any? do |declared_param| first_hash_key_or_param(declared_param) == param end end end |
#exactly_one_of(*attrs) ⇒ Object
Require exactly one of the given parameters to be present.
179 180 181 |
# File 'lib/grape/dsl/parameters.rb', line 179 def exactly_one_of(*attrs) validates(attrs, exactly_one_of: { value: true, message: (attrs) }) end |
#given(*attrs) { ... } ⇒ Object
Define a block of validations which should be applied if and only if the given parameter is present. The parameters are not nested.
202 203 204 205 206 207 208 |
# File 'lib/grape/dsl/parameters.rb', line 202 def given(*attrs, &block) attrs.each do |attr| proxy_attr = first_hash_key_or_param(attr) raise Grape::Exceptions::UnknownParameter.new(proxy_attr) unless declared_param?(proxy_attr) end new_lateral_scope(dependent_on: attrs, &block) end |
#map_params(params, element) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/grape/dsl/parameters.rb', line 228 def map_params(params, element) if params.is_a?(Array) params.map do |el| map_params(el, element) end elsif params.is_a?(Hash) params[element] || {} else {} end end |
#mutually_exclusive(*attrs) ⇒ Object
Disallow the given parameters to be present in the same request.
173 174 175 |
# File 'lib/grape/dsl/parameters.rb', line 173 def mutually_exclusive(*attrs) validates(attrs, mutual_exclusion: { value: true, message: (attrs) }) end |
#optional(*attrs, &block) ⇒ Object
Allow, but don’t require, one or more parameters for the current
endpoint.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/grape/dsl/parameters.rb', line 142 def optional(*attrs, &block) orig_attrs = attrs.clone opts = attrs..clone type = opts[:type] opts = @group.merge(opts) if @group # check type for optional parameter group if attrs && block_given? raise Grape::Exceptions::MissingGroupTypeError.new if type.nil? raise Grape::Exceptions::UnsupportedGroupTypeError.new unless Grape::Validations::Types.group?(type) end if opts[:using] require_optional_fields(attrs.first, opts) else validate_attributes(attrs, opts, &block) block_given? ? new_scope(orig_attrs, true, &block) : push_declared_params(attrs, opts.slice(:as)) end end |
#params(params) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns hash of parameters relevant for the current scope.
243 244 245 246 247 |
# File 'lib/grape/dsl/parameters.rb', line 243 def params(params) params = @parent.params(params) if @parent params = map_params(params, @element) if @element params end |
#requires(*attrs, &block) ⇒ Object Also known as: group
Require one or more parameters for the current endpoint.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/grape/dsl/parameters.rb', line 123 def requires(*attrs, &block) orig_attrs = attrs.clone opts = attrs..clone opts[:presence] = { value: true, message: opts[:message] } opts = @group.merge(opts) if @group if opts[:using] require_required_and_optional_fields(attrs.first, opts) else validate_attributes(attrs, opts, &block) block_given? ? new_scope(orig_attrs, &block) : push_declared_params(attrs, opts.slice(:as)) end end |
#use(*names) ⇒ Object Also known as: use_scope, includes
Include reusable params rules among current. You can define reusable params with helpers method.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/grape/dsl/parameters.rb', line 58 def use(*names) named_params = @api.namespace_stackable_with_hash(:named_params) || {} = names. names.each do |name| params_block = named_params.fetch(name) do raise "Params :#{name} not found!" end instance_exec(, ¶ms_block) end end |
#with(*attrs, &block) ⇒ Object
Define common settings for one or more parameters
167 168 169 |
# File 'lib/grape/dsl/parameters.rb', line 167 def with(*attrs, &block) new_group_scope(attrs.clone, &block) end |