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.
-
#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(attr) { ... } ⇒ Object
Define a block of validations which should be applied if and only if the given parameter is present.
-
#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.
Instance Method Details
#all_or_none_of(*attrs) ⇒ Object
Require that either all given params are present, or none are.
157 158 159 |
# File 'lib/grape/dsl/parameters.rb', line 157 def all_or_none_of(*attrs) validates(attrs, all_or_none_of: true) end |
#at_least_one_of(*attrs) ⇒ Object
Require at least one of the given parameters to be present.
151 152 153 |
# File 'lib/grape/dsl/parameters.rb', line 151 def at_least_one_of(*attrs) validates(attrs, at_least_one_of: true) end |
#declared_param?(param) ⇒ Boolean
Test for whether a certain parameter has been defined in this params block yet.
176 177 178 179 180 |
# File 'lib/grape/dsl/parameters.rb', line 176 def declared_param?(param) # @declared_params also includes hashes of options and such, but those # won't be flattened out. @declared_params.flatten.include?(param) end |
#exactly_one_of(*attrs) ⇒ Object
Require exactly one of the given parameters to be present.
145 146 147 |
# File 'lib/grape/dsl/parameters.rb', line 145 def exactly_one_of(*attrs) validates(attrs, exactly_one_of: true) end |
#given(attr) { ... } ⇒ Object
Define a block of validations which should be applied if and only if the given parameter is present. The parameters are not nested.
168 169 170 171 |
# File 'lib/grape/dsl/parameters.rb', line 168 def given(attr, &block) fail Grape::Exceptions::UnknownParameter.new(attr) unless declared_param?(attr) new_lateral_scope(dependent_on: attr, &block) end |
#mutually_exclusive(*attrs) ⇒ Object
Disallow the given parameters to be present in the same request.
139 140 141 |
# File 'lib/grape/dsl/parameters.rb', line 139 def mutually_exclusive(*attrs) validates(attrs, mutual_exclusion: true) end |
#optional(*attrs, &block) ⇒ Object
Allow, but don’t require, one or more parameters for the current
endpoint.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/grape/dsl/parameters.rb', line 116 def optional(*attrs, &block) orig_attrs = attrs.clone opts = attrs..clone type = opts[:type] # check type for optional parameter group if attrs && block_given? fail Grape::Exceptions::MissingGroupTypeError.new if type.nil? fail Grape::Exceptions::UnsupportedGroupTypeError.new unless [Array, Hash].include?(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) 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.
187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/grape/dsl/parameters.rb', line 187 def params(params) params = @parent.params(params) if @parent if @element if params.is_a?(Array) params = params.flat_map { |el| el[@element] || {} } elsif params.is_a?(Hash) params = params[@element] || {} else params = {} end end params end |
#requires(*attrs, &block) ⇒ Object Also known as: group
Require one or more parameters for the current endpoint.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/grape/dsl/parameters.rb', line 97 def requires(*attrs, &block) orig_attrs = attrs.clone opts = attrs..clone opts[:presence] = true 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) 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.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/grape/dsl/parameters.rb', line 32 def use(*names) named_params = Grape::DSL::Configuration.stacked_hash_to_hash(@api.namespace_stackable(:named_params)) || {} = names. names.each do |name| params_block = named_params.fetch(name) do fail "Params :#{name} not found!" end instance_exec(, ¶ms_block) end end |