Class: Grape::Validations::Validators::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/validations/validators/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, options, required, scope, *opts) ⇒ Base

Creates a new Validator from options specified by a requires or optional directive during parameter definition.

Parameters:

  • attrs (Array)

    names of attributes to which the Validator applies

  • options (Object)

    implementation-dependent Validator options

  • required (Boolean)

    attribute(s) are required or optional

  • scope (ParamsScope)

    parent scope for this Validator

  • opts (Array)

    additional validation options



17
18
19
20
21
22
23
24
25
# File 'lib/grape/validations/validators/base.rb', line 17

def initialize(attrs, options, required, scope, *opts)
  @attrs = Array(attrs)
  @option = options
  @required = required
  @scope = scope
  opts = opts.any? ? opts.shift : {}
  @fail_fast = opts.fetch(:fail_fast, false)
  @allow_blank = opts.fetch(:allow_blank, false)
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



7
8
9
# File 'lib/grape/validations/validators/base.rb', line 7

def attrs
  @attrs
end

Class Method Details

.convert_to_short_name(klass) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/grape/validations/validators/base.rb', line 64

def self.convert_to_short_name(klass)
  ret = klass.name.gsub(/::/, '/')
  ret.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  ret.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  ret.tr!('-', '_')
  ret.downcase!
  File.basename(ret, '_validator')
end

.inherited(klass) ⇒ Object



73
74
75
76
77
# File 'lib/grape/validations/validators/base.rb', line 73

def self.inherited(klass)
  return unless klass.name.present?

  Validations.register_validator(convert_to_short_name(klass), klass)
end

Instance Method Details

#fail_fast?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/grape/validations/validators/base.rb', line 89

def fail_fast?
  @fail_fast
end

#message(default_key = nil) ⇒ Object



79
80
81
82
# File 'lib/grape/validations/validators/base.rb', line 79

def message(default_key = nil)
  options = instance_variable_get(:@option)
  options_key?(:message) ? options[:message] : default_key
end

#options_key?(key, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/grape/validations/validators/base.rb', line 84

def options_key?(key, options = nil)
  options = instance_variable_get(:@option) if options.nil?
  options.respond_to?(:key?) && options.key?(key) && !options[key].nil?
end

#validate(request) ⇒ void

Note:

Override #validate! unless you need to access the entire request.

This method returns an undefined value.

Validates a given request.

Parameters:

Raises:



32
33
34
35
36
# File 'lib/grape/validations/validators/base.rb', line 32

def validate(request)
  return unless @scope.should_validate?(request.params)

  validate!(request.params)
end

#validate!(params) ⇒ void

Note:

Override #validate if you need to access the entire request.

This method returns an undefined value.

Validates a given parameter hash.

Parameters:

  • params (Hash)

    parameters to validate

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/grape/validations/validators/base.rb', line 43

def validate!(params)
  attributes = SingleAttributeIterator.new(self, @scope, params)
  # we collect errors inside array because
  # there may be more than one error per field
  array_errors = []

  attributes.each do |val, attr_name, empty_val, skip_value|
    next if skip_value
    next if !@scope.required? && empty_val
    next unless @scope.meets_dependency?(val, params)

    begin
      validate_param!(attr_name, val) if @required || (val.respond_to?(:key?) && val.key?(attr_name))
    rescue Grape::Exceptions::Validation => e
      array_errors << e
    end
  end

  raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any?
end