Class: Grape::Validations::ParamsScope
- Inherits:
-
Object
- Object
- Grape::Validations::ParamsScope
- Includes:
- DSL::Parameters
- Defined in:
- lib/grape/validations/params_scope.rb
Defined Under Namespace
Classes: Attr
Constant Summary collapse
- RESERVED_DOCUMENTATION_KEYWORDS =
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.
%i[as required param_type is_array format example].freeze
Instance Attribute Summary collapse
-
#element ⇒ Object
Returns the value of attribute element.
-
#index ⇒ Object
Returns the value of attribute index.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #attr_meets_dependency?(params) ⇒ Boolean
- #brackets(val) ⇒ Object
- #configuration ⇒ Object
-
#full_name(name, index: nil) ⇒ String
The proper attribute name, with nesting considered.
-
#initialize(opts) { ... } ⇒ ParamsScope
constructor
Open up a new ParamsScope, allowing parameter definitions per Grape::DSL::Params.
-
#lateral? ⇒ Boolean
A lateral scope is subordinate to its parent, but its keys are at the same level as its parent and thus is not contained within an element.
- #meets_dependency?(params, request_params) ⇒ Boolean
- #meets_hash_dependency?(params) ⇒ Boolean
-
#nested? ⇒ Boolean
A nested scope is contained in one of its parent’s elements.
-
#required? ⇒ Boolean
Whether or not this scope needs to be present, or can be blank.
- #reset_index ⇒ Object
-
#root? ⇒ Boolean
Whether or not this scope is the root-level scope.
-
#should_validate?(parameters) ⇒ Boolean
Whether or not this entire scope needs to be validated.
Methods included from DSL::Parameters
#all_or_none_of, #at_least_one_of, #build_with, #declared_param?, #exactly_one_of, #given, #map_params, #mutually_exclusive, #optional, #params, #requires, #use, #with
Constructor Details
#initialize(opts) { ... } ⇒ ParamsScope
Open up a new ParamsScope, allowing parameter definitions per
Grape::DSL::Params.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/grape/validations/params_scope.rb', line 61 def initialize(opts, &block) @element = opts[:element] @element_renamed = opts[:element_renamed] @parent = opts[:parent] @api = opts[:api] @optional = opts[:optional] || false @type = opts[:type] @group = opts[:group] @dependent_on = opts[:dependent_on] @declared_params = [] @index = nil instance_eval(&block) if block configure_declared_params end |
Instance Attribute Details
#element ⇒ Object
Returns the value of attribute element.
6 7 8 |
# File 'lib/grape/validations/params_scope.rb', line 6 def element @element end |
#index ⇒ Object
Returns the value of attribute index.
6 7 8 |
# File 'lib/grape/validations/params_scope.rb', line 6 def index @index end |
#parent ⇒ Object
Returns the value of attribute parent.
6 7 8 |
# File 'lib/grape/validations/params_scope.rb', line 6 def parent @parent end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/grape/validations/params_scope.rb', line 7 def type @type end |
Instance Method Details
#attr_meets_dependency?(params) ⇒ Boolean
102 103 104 105 106 107 |
# File 'lib/grape/validations/params_scope.rb', line 102 def attr_meets_dependency?(params) return true unless @dependent_on return false if @parent.present? && !@parent.attr_meets_dependency?(params) meets_hash_dependency?(params) end |
#brackets(val) ⇒ Object
141 142 143 |
# File 'lib/grape/validations/params_scope.rb', line 141 def brackets(val) "[#{val}]" if val end |
#configuration ⇒ Object
78 79 80 |
# File 'lib/grape/validations/params_scope.rb', line 78 def configuration @api.configuration.respond_to?(:evaluate) ? @api.configuration.evaluate : @api.configuration end |
#full_name(name, index: nil) ⇒ String
Returns the proper attribute name, with nesting considered.
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/grape/validations/params_scope.rb', line 127 def full_name(name, index: nil) if nested? # Find our containing element's name, and append ours. "#{@parent.full_name(@element)}#{brackets(@index || index)}#{brackets(name)}" elsif lateral? # Find the name of the element as if it was at the same nesting level # as our parent. We need to forward our index upward to achieve this. @parent.full_name(name, index: @index) else # We must be the root scope, so no prefix needed. name.to_s end end |
#lateral? ⇒ Boolean
A lateral scope is subordinate to its parent, but its keys are at the same level as its parent and thus is not contained within an element.
159 160 161 |
# File 'lib/grape/validations/params_scope.rb', line 159 def lateral? @parent && !@element end |
#meets_dependency?(params, request_params) ⇒ Boolean
94 95 96 97 98 99 100 |
# File 'lib/grape/validations/params_scope.rb', line 94 def meets_dependency?(params, request_params) return true unless @dependent_on return false if @parent.present? && !@parent.meets_dependency?(@parent.params(request_params), request_params) return params.any? { |param| meets_dependency?(param, request_params) } if params.is_a?(Array) meets_hash_dependency?(params) end |
#meets_hash_dependency?(params) ⇒ Boolean
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/grape/validations/params_scope.rb', line 109 def meets_hash_dependency?(params) # params might be anything what looks like a hash, so it must implement a `key?` method return false unless params.respond_to?(:key?) @dependent_on.each do |dependency| if dependency.is_a?(Hash) dependency_key = dependency.keys[0] proc = dependency.values[0] return false unless proc.call(params.try(:[], dependency_key)) elsif params.respond_to?(:key?) && params.try(:[], dependency).blank? return false end end true end |
#nested? ⇒ Boolean
A nested scope is contained in one of its parent’s elements.
152 153 154 |
# File 'lib/grape/validations/params_scope.rb', line 152 def nested? @parent && @element end |
#required? ⇒ Boolean
Returns whether or not this scope needs to be present, or can be blank.
165 166 167 |
# File 'lib/grape/validations/params_scope.rb', line 165 def required? !@optional end |
#reset_index ⇒ Object
169 170 171 |
# File 'lib/grape/validations/params_scope.rb', line 169 def reset_index @index = nil end |
#root? ⇒ Boolean
Returns whether or not this scope is the root-level scope.
146 147 148 |
# File 'lib/grape/validations/params_scope.rb', line 146 def root? !@parent end |
#should_validate?(parameters) ⇒ Boolean
Returns whether or not this entire scope needs to be validated.
84 85 86 87 88 89 90 91 92 |
# File 'lib/grape/validations/params_scope.rb', line 84 def should_validate?(parameters) scoped_params = params(parameters) return false if @optional && (scoped_params.blank? || all_element_blank?(scoped_params)) return false unless meets_dependency?(scoped_params, parameters) return true if parent.nil? parent.should_validate?(parameters) end |