Class: Grape::Validations::ParamsScope

Inherits:
Object
  • Object
show all
Includes:
DSL::Parameters
Defined in:
lib/grape/validations/params_scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • opts (Hash)

    options for this scope

Options Hash (opts):

  • :element (Symbol)

    the element that contains this scope; for this to be relevant, @parent must be set

  • :parent (ParamsScope)

    the scope containing this scope

  • :api (API)

    the API endpoint to modify

  • :optional (Boolean)

    whether or not this scope needs to have any parameters set or not

  • :type (Class)

    a type meant to govern this scope (deprecated)

  • :type (Hash)

    group options for this scope

  • :dependent_on (Symbol)

    if present, this scope should only validate if this param is present in the parent scope

Yields:

  • the instance context, open for parameter definitions


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/grape/validations/params_scope.rb', line 25

def initialize(opts, &block)
  @element      = opts[:element]
  @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_given?

  configure_declared_params
end

Instance Attribute Details

#elementObject

Returns the value of attribute element


6
7
8
# File 'lib/grape/validations/params_scope.rb', line 6

def element
  @element
end

#indexObject

Returns the value of attribute index


6
7
8
# File 'lib/grape/validations/params_scope.rb', line 6

def index
  @index
end

#parentObject

Returns the value of attribute parent


6
7
8
# File 'lib/grape/validations/params_scope.rb', line 6

def parent
  @parent
end

#typeObject (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

#brackets(val) ⇒ Object


95
96
97
# File 'lib/grape/validations/params_scope.rb', line 95

def brackets(val)
  "[#{val}]" if val
end

#configurationObject


41
42
43
# File 'lib/grape/validations/params_scope.rb', line 41

def configuration
  @api.configuration.evaluate
end

#full_name(name, index: nil) ⇒ String

Returns the proper attribute name, with nesting considered.

Returns:

  • (String)

    the proper attribute name, with nesting considered.


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/grape/validations/params_scope.rb', line 81

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.

Returns:

  • (Boolean)

    whether or not this scope is lateral


113
114
115
# File 'lib/grape/validations/params_scope.rb', line 113

def lateral?
  @parent && !@element
end

#meets_dependency?(params, request_params) ⇒ Boolean

Returns:

  • (Boolean)

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/grape/validations/params_scope.rb', line 56

def meets_dependency?(params, request_params)
  return true unless @dependent_on

  if @parent.present? && !@parent.meets_dependency?(@parent.params(request_params), request_params)
    return false
  end

  return params.any? { |param| meets_dependency?(param, request_params) } if params.is_a?(Array)
  return false unless params.respond_to?(:with_indifferent_access)
  params = params.with_indifferent_access

  @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.

Returns:

  • (Boolean)

    whether or not this scope is nested


106
107
108
# File 'lib/grape/validations/params_scope.rb', line 106

def nested?
  @parent && @element
end

#required?Boolean

Returns whether or not this scope needs to be present, or can be blank.

Returns:

  • (Boolean)

    whether or not this scope needs to be present, or can be blank


119
120
121
# File 'lib/grape/validations/params_scope.rb', line 119

def required?
  !@optional
end

#root?Boolean

Returns whether or not this scope is the root-level scope.

Returns:

  • (Boolean)

    whether or not this scope is the root-level scope


100
101
102
# File 'lib/grape/validations/params_scope.rb', line 100

def root?
  !@parent
end

#should_validate?(parameters) ⇒ Boolean

Returns whether or not this entire scope needs to be validated.

Returns:

  • (Boolean)

    whether or not this entire scope needs to be validated


47
48
49
50
51
52
53
54
# File 'lib/grape/validations/params_scope.rb', line 47

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