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.



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

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.



13
14
15
# File 'lib/grape/validations.rb', line 13

def attrs
  @attrs
end

Class Method Details

.inherited(klass) ⇒ Object



65
66
67
68
# File 'lib/grape/validations.rb', line 65

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

Instance Method Details

#i18n_message(type, attribute) ⇒ Object



35
36
37
38
# File 'lib/grape/validations.rb', line 35

def i18n_message(type, attribute)
  i18n_attr = I18n.t("grape.errors.attributes.#{attribute}", :default => attribute.to_s)
  I18n.t("grape.errors.messages.#{type}", :attribute => i18n_attr)
end

#validate!(params) ⇒ Object



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

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