Class: Grape::Validations::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 (Hash) (defaults to: {})

    additional validation options



14
15
16
17
18
19
20
# File 'lib/grape/validations/validators/base.rb', line 14

def initialize(attrs, options, required, scope, opts = {})
  @attrs = Array(attrs)
  @option = options
  @required = required
  @scope = scope
  @fail_fast = opts[:fail_fast] || false
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs



4
5
6
# File 'lib/grape/validations/validators/base.rb', line 4

def attrs
  @attrs
end

Class Method Details

.convert_to_short_name(klass) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/grape/validations/validators/base.rb', line 57

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

.inherited(klass) ⇒ Object



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

def self.inherited(klass)
  short_name = convert_to_short_name(klass)
  Validations.register_validator(short_name, klass)
end

Instance Method Details

#fail_fast?Boolean

Returns:

  • (Boolean)


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

def fail_fast?
  @fail_fast
end

#message(default_key = nil) ⇒ Object



71
72
73
74
# File 'lib/grape/validations/validators/base.rb', line 71

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)


76
77
78
79
# File 'lib/grape/validations/validators/base.rb', line 76

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:

This method must be thread-safe.

Note:

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

This method returns an undefined value.

Validates a given request.

Parameters:

Raises:



28
29
30
31
# File 'lib/grape/validations/validators/base.rb', line 28

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

#validate!(params) ⇒ void

Note:

This method must be thread-safe.

Note:

Override #validate iff 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:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/grape/validations/validators/base.rb', line 39

def validate!(params)
  attributes = AttributesIterator.new(self, @scope, params)
  array_errors = []
  attributes.each do |resource_params, attr_name|
    next unless @required || (resource_params.respond_to?(:key?) && resource_params.key?(attr_name))

    begin
      validate_param!(attr_name, resource_params)
    rescue Grape::Exceptions::Validation => e
      # we collect errors inside array because
      # there may be more than one error per field
      array_errors << e
    end
  end

  raise Grape::Exceptions::ValidationArrayErrors, array_errors if array_errors.any?
end