Class: Grape::Validations::Validator

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

Overview

We define Validator::inherited here so SingleOptionValidator will not be considered a validator.

Direct Known Subclasses

PresenceValidator, SingleOptionValidator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, options, required, scope) ⇒ Validator

Returns a new instance of Validator.



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

def initialize(attrs, options, required, scope)
  @attrs = Array(attrs)
  @required = required
  @scope = scope

  if options.is_a?(Hash) && !options.empty?
    raise "unknown options: #{options.keys}"
  end
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



11
12
13
# File 'lib/grape/validations.rb', line 11

def attrs
  @attrs
end

Class Method Details

.inherited(klass) ⇒ Object



58
59
60
61
# File 'lib/grape/validations.rb', line 58

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

Instance Method Details

#validate!(params) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/grape/validations.rb', line 23

def validate!(params)
  params = @scope.params(params)

  @attrs.each do |attr_name|
    if @required || params.has_key?(attr_name)
      validate_param!(attr_name, params)
    end
  end
end