• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-grape / grape / 20116829501

10 Dec 2025 11:39PM UTC coverage: 96.293% (-0.2%) from 96.472%
20116829501

push

github

web-flow
Merge pull request #2635 from ruby-grape/refactor/dsl-group-validators-dry

Refactor: Rename mutual_exclusion to mutually_exclusive and consolidate DSL validators

1054 of 1151 branches covered (91.57%)

Branch coverage included in aggregate %.

4 of 4 new or added lines in 2 files covered. (100.0%)

3 existing lines in 1 file now uncovered.

3336 of 3408 relevant lines covered (97.89%)

56265.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.44
/lib/grape/validations/params_scope.rb
1
# frozen_string_literal: true
2

3
module Grape
52✔
4
  module Validations
52✔
5
    class ParamsScope
52✔
6
      attr_accessor :element, :parent, :index
52✔
7
      attr_reader :type, :params_meeting_dependency
52✔
8

9
      include Grape::DSL::Parameters
52✔
10
      include Grape::Validations::ParamsDocumentation
52✔
11

12
      # There are a number of documentation options on entities that don't have
13
      # corresponding validators. Since there is nowhere that enumerates them all,
14
      # we maintain a list of them here and skip looking up validators for them.
15
      RESERVED_DOCUMENTATION_KEYWORDS = %i[as required param_type is_array format example].freeze
52✔
16

17
      class Attr
52✔
18
        attr_accessor :key, :scope
52✔
19

20
        # Open up a new ParamsScope::Attr
21
        # @param key [Hash, Symbol] key of attr
22
        # @param scope [Grape::Validations::ParamsScope] scope of attr
23
        def initialize(key, scope)
52✔
24
          @key = key
166,004✔
25
          @scope = scope
166,004✔
26
        end
27

28
        # @return Array[Symbol, Hash[Symbol => Array]] declared_params with symbol instead of Attr
29
        def self.attrs_keys(declared_params)
52✔
30
          declared_params.map do |declared_param_attr|
×
31
            attr_key(declared_param_attr)
×
32
          end
33
        end
34

35
        def self.attr_key(declared_param_attr)
52✔
36
          return attr_key(declared_param_attr.key) if declared_param_attr.is_a?(self)
×
37

38
          if declared_param_attr.is_a?(Hash)
×
39
            declared_param_attr.transform_values { |value| attrs_keys(value) }
×
40
          else
41
            declared_param_attr
×
42
          end
43
        end
44
      end
45

46
      # Open up a new ParamsScope, allowing parameter definitions per
47
      #   Grape::DSL::Params.
48
      # @param opts [Hash] options for this scope
49
      # @option opts :element [Symbol] the element that contains this scope; for
50
      #   this to be relevant, @parent must be set
51
      # @option opts :element_renamed [Symbol, nil] whenever this scope should
52
      #   be renamed and to what, given +nil+ no renaming is done
53
      # @option opts :parent [ParamsScope] the scope containing this scope
54
      # @option opts :api [API] the API endpoint to modify
55
      # @option opts :optional [Boolean] whether or not this scope needs to have
56
      #   any parameters set or not
57
      # @option opts :type [Class] a type meant to govern this scope (deprecated)
58
      # @option opts :type [Hash] group options for this scope
59
      # @option opts :dependent_on [Symbol] if present, this scope should only
60
      #   validate if this param is present in the parent scope
61
      # @yield the instance context, open for parameter definitions
62
      def initialize(opts, &block)
52✔
63
        @element          = opts[:element]
128,178✔
64
        @element_renamed  = opts[:element_renamed]
128,178✔
65
        @parent           = opts[:parent]
128,178✔
66
        @api              = opts[:api]
128,178✔
67
        @optional         = opts[:optional] || false
128,178✔
68
        @type             = opts[:type]
128,178✔
69
        @group            = opts[:group]
128,178✔
70
        @dependent_on     = opts[:dependent_on]
128,178✔
71
        @params_meeting_dependency = []
128,178✔
72
        @declared_params = []
128,178✔
73
        @index = nil
128,178✔
74

75
        instance_eval(&block) if block
128,178✔
76

77
        configure_declared_params
127,053✔
78
      end
79

80
      def configuration
52✔
81
        @api.configuration.respond_to?(:evaluate) ? @api.configuration.evaluate : @api.configuration
675✔
82
      end
83

84
      # @return [Boolean] whether or not this entire scope needs to be
85
      #   validated
86
      def should_validate?(parameters)
52✔
87
        scoped_params = params(parameters)
225,255✔
88

89
        return false if @optional && (scoped_params.blank? || all_element_blank?(scoped_params))
225,255✔
90
        return false unless meets_dependency?(scoped_params, parameters)
216,382✔
91
        return true if parent.nil?
197,752✔
92

93
        parent.should_validate?(parameters)
66,465✔
94
      end
95

96
      def meets_dependency?(params, request_params)
52✔
97
        return true unless @dependent_on
410,309✔
98
        return false if @parent.present? && !@parent.meets_dependency?(@parent.params(request_params), request_params)
49,365✔
99

100
        if params.is_a?(Array)
49,275✔
101
          @params_meeting_dependency = params.flatten.filter { |param| meets_dependency?(param, request_params) }
9,135✔
102
          return @params_meeting_dependency.present?
2,880✔
103
        end
104

105
        meets_hash_dependency?(params)
46,395✔
106
      end
107

108
      def attr_meets_dependency?(params)
52✔
109
        return true unless @dependent_on
5,580✔
110
        return false if @parent.present? && !@parent.attr_meets_dependency?(params)
2,115✔
111

112
        meets_hash_dependency?(params)
1,980✔
113
      end
114

115
      def meets_hash_dependency?(params)
52✔
116
        # params might be anything what looks like a hash, so it must implement a `key?` method
117
        return false unless params.respond_to?(:key?)
48,375✔
118

119
        @dependent_on.each do |dependency|
48,330✔
120
          if dependency.is_a?(Hash)
48,555✔
121
            dependency_key = dependency.keys[0]
42,480✔
122
            proc = dependency.values[0]
42,480✔
123
            return false unless proc.call(params.try(:[], dependency_key))
42,480✔
124
          elsif params.respond_to?(:key?) && params.try(:[], dependency).blank?
6,075✔
125
            return false
1,890✔
126
          end
127
        end
128

129
        true
26,505✔
130
      end
131

132
      # @return [String] the proper attribute name, with nesting considered.
133
      def full_name(name, index: nil)
52✔
134
        if nested?
330,499✔
135
          # Find our containing element's name, and append ours.
136
          "#{@parent.full_name(@element)}#{brackets(index || @index)}#{brackets(name)}"
87,409✔
137
        elsif lateral?
243,090✔
138
          # Find the name of the element as if it was at the same nesting level
139
          # as our parent. We need to forward our index upward to achieve this.
140
          @parent.full_name(name, index: @index)
25,965✔
141
        else
142
          # We must be the root scope, so no prefix needed.
143
          name.to_s
217,125✔
144
        end
145
      end
146

147
      def brackets(val)
52✔
148
        "[#{val}]" if val
174,818✔
149
      end
150

151
      # @return [Boolean] whether or not this scope is the root-level scope
152
      def root?
52✔
153
        !@parent
135✔
154
      end
155

156
      # A nested scope is contained in one of its parent's elements.
157
      # @return [Boolean] whether or not this scope is nested
158
      def nested?
52✔
159
        @parent && @element
467,632✔
160
      end
161

162
      # A lateral scope is subordinate to its parent, but its keys are at the
163
      # same level as its parent and thus is not contained within an element.
164
      # @return [Boolean] whether or not this scope is lateral
165
      def lateral?
52✔
166
        @parent && !@element
438,704✔
167
      end
168

169
      # @return [Boolean] whether or not this scope needs to be present, or can
170
      #   be blank
171
      def required?
52✔
172
        !@optional
131,871✔
173
      end
174

175
      def reset_index
52✔
176
        @index = nil
140,737✔
177
      end
178

179
      protected
52✔
180

181
      # Adds a parameter declaration to our list of validations.
182
      # @param attrs [Array] (see Grape::DSL::Parameters#requires)
183
      def push_declared_params(attrs, opts = {})
52✔
184
        opts[:declared_params_scope] = self unless opts.key?(:declared_params_scope)
179,414✔
185
        return @parent.push_declared_params(attrs, opts) if lateral?
179,414✔
186

187
        push_renamed_param(full_path + [attrs.first], opts[:as]) if opts[:as]
162,809✔
188
        @declared_params.concat(attrs.map { |attr| ::Grape::Validations::ParamsScope::Attr.new(attr, opts[:declared_params_scope]) })
328,813✔
189
      end
190

191
      # Get the full path of the parameter scope in the hierarchy.
192
      #
193
      # @return [Array<Symbol>] the nesting/path of the current parameter scope
194
      def full_path
52✔
195
        if nested?
10,080✔
196
          (@parent.full_path + [@element])
6,480✔
197
        elsif lateral?
3,600✔
198
          @parent.full_path
45✔
199
        else
200
          []
3,555✔
201
        end
202
      end
203

204
      private
52✔
205

206
      # Add a new parameter which should be renamed when using the +#declared+
207
      # method.
208
      #
209
      # @param path [Array<String, Symbol>] the full path of the parameter
210
      #   (including the parameter name as last array element)
211
      # @param new_name [String, Symbol] the new name of the parameter (the
212
      #   renamed name, with the +as: ...+ semantic)
213
      def push_renamed_param(path, new_name)
52✔
214
        api_route_setting = @api.inheritable_setting.route
3,555✔
215
        base = api_route_setting[:renamed_params] || {}
3,555✔
216
        base[Array(path).map(&:to_s)] = new_name.to_s
3,555✔
217
        api_route_setting[:renamed_params] = base
3,555✔
218
      end
219

220
      def require_required_and_optional_fields(context, opts)
52✔
221
        except_fields = Array.wrap(opts[:except])
540✔
222
        using_fields = opts[:using].keys.delete_if { |f| except_fields.include?(f) }
1,710✔
223

224
        if context == :all
540✔
225
          optional_fields = except_fields
315✔
226
          required_fields = using_fields
315✔
227
        else # context == :none
228
          required_fields = except_fields
225✔
229
          optional_fields = using_fields
225✔
230
        end
231
        required_fields.each do |field|
540✔
232
          field_opts = opts[:using][field]
585✔
233
          raise ArgumentError, "required field not exist: #{field}" unless field_opts
585✔
234

235
          requires(field, **field_opts)
540✔
236
        end
237
        optional_fields.each do |field|
495✔
238
          field_opts = opts[:using][field]
585✔
239
          optional(field, **field_opts) if field_opts
585✔
240
        end
241
      end
242

243
      def require_optional_fields(context, opts)
52✔
244
        optional_fields = opts[:using].keys
315✔
245
        unless context == :all
315!
246
          except_fields = Array.wrap(opts[:except])
×
247
          optional_fields.delete_if { |f| except_fields.include?(f) }
×
248
        end
249
        optional_fields.each do |field|
315✔
250
          field_opts = opts[:using][field]
540✔
251
          optional(field, **field_opts) if field_opts
540!
252
        end
253
      end
254

255
      def validate_attributes(attrs, opts, &block)
52✔
256
        validations = opts.clone
163,664✔
257
        validations[:type] ||= Array if block
163,664✔
258
        validates(attrs, validations)
163,664✔
259
      end
260

261
      # Returns a new parameter scope, subordinate to the current one and nested
262
      # under the parameter corresponding to `attrs.first`.
263
      # @param attrs [Array] the attributes passed to the `requires` or
264
      #   `optional` invocation that opened this scope.
265
      # @param optional [Boolean] whether the parameter this are nested under
266
      #   is optional or not (and hence, whether this block's params will be).
267
      # @yield parameter scope
268
      def new_scope(attrs, opts, optional = false, &block)
52✔
269
        # if required params are grouped and no type or unsupported type is provided, raise an error
270
        type = opts[:type]
23,360✔
271
        if attrs.first && !optional
23,360✔
272
          raise Grape::Exceptions::MissingGroupType if type.nil?
7,605✔
273
          raise Grape::Exceptions::UnsupportedGroupType unless Grape::Validations::Types.group?(type)
7,560✔
274
        end
275

276
        self.class.new(
23,270✔
277
          api: @api,
278
          element: attrs.first,
279
          element_renamed: opts[:as],
280
          parent: self,
281
          optional: optional,
282
          type: type || Array,
283
          group: @group,
284
          &block
285
        )
286
      end
287

288
      # Returns a new parameter scope, not nested under any current-level param
289
      # but instead at the same level as the current scope.
290
      # @param options [Hash] options to control how this new scope behaves
291
      # @option options :dependent_on [Symbol] if given, specifies that this
292
      #   scope should only validate if this parameter from the above scope is
293
      #   present
294
      # @yield parameter scope
295
      def new_lateral_scope(options, &block)
52✔
296
        self.class.new(
11,250✔
297
          api: @api,
298
          element: nil,
299
          parent: self,
300
          options: @optional,
301
          type: type == Array ? Array : Hash,
11,250✔
302
          dependent_on: options[:dependent_on],
303
          &block
304
        )
305
      end
306

307
      # Returns a new parameter scope, subordinate to the current one and nested
308
      # under the parameter corresponding to `attrs.first`.
309
      # @param attrs [Array] the attributes passed to the `requires` or
310
      #   `optional` invocation that opened this scope.
311
      # @yield parameter scope
312
      def new_group_scope(attrs, &block)
52✔
313
        self.class.new(api: @api, parent: self, group: attrs.first, &block)
3,240✔
314
      end
315

316
      # Pushes declared params to parent or settings
317
      def configure_declared_params
52✔
318
        push_renamed_param(full_path, @element_renamed) if @element_renamed
127,053✔
319

320
        if nested?
127,053✔
321
          @parent.push_declared_params [element => @declared_params]
23,270✔
322
        else
323
          @api.inheritable_setting.namespace_stackable[:declared_params] = @declared_params
103,783✔
324
        end
325

326
        # params were stored in settings, it can be cleaned from the params scope
327
        @declared_params = nil
127,053✔
328
      end
329

330
      def validates(attrs, validations)
52✔
331
        coerce_type = infer_coercion(validations)
167,669✔
332
        required = validations.key?(:presence)
167,624✔
333
        default = validations[:default]
167,624✔
334
        values = validations[:values].is_a?(Hash) ? validations.dig(:values, :value) : validations[:values]
167,624✔
335
        except_values = validations[:except_values].is_a?(Hash) ? validations.dig(:except_values, :value) : validations[:except_values]
167,624✔
336

337
        # NB. values and excepts should be nil, Proc, Array, or Range.
338
        # Specifically, values should NOT be a Hash
339
        # use values or excepts to guess coerce type when stated type is Array
340
        coerce_type = guess_coerce_type(coerce_type, values, except_values)
167,624✔
341

342
        # default value should be present in values array, if both exist and are not procs
343
        check_incompatible_option_values(default, values, except_values)
167,624✔
344

345
        # type should be compatible with values array, if both exist
346
        validate_value_coercion(coerce_type, values, except_values)
167,444✔
347

348
        document_params attrs, validations, coerce_type, values, except_values
167,039✔
349

350
        opts = derive_validator_options(validations)
167,039✔
351

352
        # Validate for presence before any other validators
353
        validates_presence(validations, attrs, opts)
167,039✔
354

355
        # Before we run the rest of the validators, let's handle
356
        # whatever coercion so that we are working with correctly
357
        # type casted values
358
        coerce_type validations, attrs, required, opts
167,039✔
359

360
        validations.each do |type, options|
166,904✔
361
          # Don't try to look up validators for documentation params that don't have one.
362
          next if RESERVED_DOCUMENTATION_KEYWORDS.include?(type)
99,586✔
363

364
          validate(type, options, attrs, required, opts)
95,131✔
365
        end
366
      end
367

368
      # Validate and comprehend the +:type+, +:types+, and +:coerce_with+
369
      # options that have been supplied to the parameter declaration.
370
      # The +:type+ and +:types+ options will be removed from the
371
      # validations list, replaced appropriately with +:coerce+ and
372
      # +:coerce_with+ options that will later be passed to
373
      # {Validators::CoerceValidator}. The type that is returned may be
374
      # used for documentation and further validation of parameter
375
      # options.
376
      #
377
      # @param validations [Hash] list of validations supplied to the
378
      #   parameter declaration
379
      # @return [class-like] type to which the parameter will be coerced
380
      # @raise [ArgumentError] if the given type options are invalid
381
      def infer_coercion(validations)
52✔
382
        raise ArgumentError, ':type may not be supplied with :types' if validations.key?(:type) && validations.key?(:types)
167,669✔
383

384
        validations[:coerce] = (options_key?(:type, :value, validations) ? validations[:type][:value] : validations[:type]) if validations.key?(:type)
167,624✔
385
        validations[:coerce_message] = (options_key?(:type, :message, validations) ? validations[:type][:message] : nil) if validations.key?(:type)
167,624✔
386
        validations[:coerce] = (options_key?(:types, :value, validations) ? validations[:types][:value] : validations[:types]) if validations.key?(:types)
167,624✔
387
        validations[:coerce_message] = (options_key?(:types, :message, validations) ? validations[:types][:message] : nil) if validations.key?(:types)
167,624✔
388

389
        validations.delete(:types) if validations.key?(:types)
167,624✔
390

391
        coerce_type = validations[:coerce]
167,624✔
392

393
        # Special case - when the argument is a single type that is a
394
        # variant-type collection.
395
        if Types.multiple?(coerce_type) && validations.key?(:type)
167,624✔
396
          validations[:coerce] = Types::VariantCollectionCoercer.new(
498✔
397
            coerce_type,
398
            validations.delete(:coerce_with)
399
          )
400
        end
401
        validations.delete(:type)
167,624✔
402

403
        coerce_type
167,624✔
404
      end
405

406
      # Enforce correct usage of :coerce_with parameter.
407
      # We do not allow coercion without a type, nor with
408
      # +JSON+ as a type since this defines its own coercion
409
      # method.
410
      def check_coerce_with(validations)
52✔
411
        return unless validations.key?(:coerce_with)
167,039✔
412
        # type must be supplied for coerce_with..
413
        raise ArgumentError, 'must supply type for coerce_with' unless validations.key?(:coerce)
1,305✔
414

415
        # but not special JSON types, which
416
        # already imply coercion method
417
        return if [JSON, Array[JSON]].exclude? validations[:coerce]
1,260✔
418

419
        raise ArgumentError, 'coerce_with disallowed for type: JSON'
90✔
420
      end
421

422
      # Add type coercion validation to this scope,
423
      # if any has been specified.
424
      # This validation has special handling since it is
425
      # composited from more than one +requires+/+optional+
426
      # parameter, and needs to be run before most other
427
      # validations.
428
      def coerce_type(validations, attrs, required, opts)
52✔
429
        check_coerce_with(validations)
167,039✔
430

431
        return unless validations.key?(:coerce)
166,904✔
432

433
        coerce_options = {
434
          type: validations[:coerce],
81,935✔
435
          method: validations[:coerce_with],
436
          message: validations[:coerce_message]
437
        }
438
        validate('coerce', coerce_options, attrs, required, opts)
81,935✔
439
        validations.delete(:coerce_with)
81,935✔
440
        validations.delete(:coerce)
81,935✔
441
        validations.delete(:coerce_message)
81,935✔
442
      end
443

444
      def guess_coerce_type(coerce_type, *values_list)
52✔
445
        return coerce_type unless coerce_type == Array
167,624✔
446

447
        values_list.each do |values|
15,619✔
448
          next if !values || values.is_a?(Proc)
31,148✔
449
          return values.first.class if values.is_a?(Range) || !values.empty?
90!
450
        end
451
        coerce_type
15,529✔
452
      end
453

454
      def check_incompatible_option_values(default, values, except_values)
52✔
455
        return unless default && !default.is_a?(Proc)
167,624✔
456

457
        raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :values, values) if values && !values.is_a?(Proc) && !Array(default).all? { |def_val| values.include?(def_val) }
26,371✔
458

459
        return unless except_values && !except_values.is_a?(Proc) && Array(default).any? { |def_val| except_values.include?(def_val) }
18,271✔
460

461
        raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :except, except_values)
90✔
462
      end
463

464
      def validate(type, options, attrs, required, opts)
52✔
465
        validator_options = {
466
          attributes: attrs,
250,963✔
467
          options: options,
468
          required: required,
469
          params_scope: self,
470
          opts: opts,
471
          validator_class: Validations.require_validator(type)
472
        }
473
        @api.inheritable_setting.namespace_stackable[:validations] = validator_options
250,963✔
474
      end
475

476
      def validate_value_coercion(coerce_type, *values_list)
52✔
477
        return unless coerce_type
167,444✔
478

479
        coerce_type = coerce_type.first if coerce_type.is_a?(Enumerable)
82,385✔
480
        values_list.each do |values|
82,385✔
481
          next if !values || values.is_a?(Proc)
164,455✔
482

483
          value_types = values.is_a?(Range) ? [values.begin, values.end].compact : values
20,430✔
484
          value_types = value_types.map { |type| Grape::API::Boolean.build(type) } if coerce_type == Grape::API::Boolean
22,365✔
485
          raise Grape::Exceptions::IncompatibleOptionValues.new(:type, coerce_type, :values, values) unless value_types.all?(coerce_type)
20,430✔
486
        end
487
      end
488

489
      def extract_message_option(attrs)
52✔
UNCOV
490
        return nil unless attrs.is_a?(Array)
×
491

UNCOV
492
        opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
×
UNCOV
493
        opts.key?(:message) && !opts[:message].nil? ? opts.delete(:message) : nil
×
494
      end
495

496
      def options_key?(type, key, validations)
52✔
497
        validations[type].respond_to?(:key?) && validations[type].key?(key) && !validations[type][key].nil?
163,960✔
498
      end
499

500
      def all_element_blank?(scoped_params)
52✔
501
        scoped_params.respond_to?(:all?) && scoped_params.all?(&:blank?)
15,480✔
502
      end
503

504
      # Validators don't have access to each other and they don't need, however,
505
      # some validators might influence others, so their options should be shared
506
      def derive_validator_options(validations)
52✔
507
        allow_blank = validations[:allow_blank]
167,039✔
508

509
        {
510
          allow_blank: allow_blank.is_a?(Hash) ? allow_blank[:value] : allow_blank,
334,078✔
511
          fail_fast: validations.delete(:fail_fast) || false
512
        }
513
      end
514

515
      def validates_presence(validations, attrs, opts)
52✔
516
        return unless validations.key?(:presence) && validations[:presence]
167,039✔
517

518
        validate('presence', validations.delete(:presence), attrs, true, opts)
73,897✔
519
        validations.delete(:message) if validations.key?(:message)
73,897✔
520
      end
521
    end
522
  end
523
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc