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

DefaultValidator, PresenceValidator, SingleOptionValidator, ValuesValidator

Defined Under Namespace

Classes: AttributesIterator

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Validator) initialize(attrs, options, required, scope)



9
10
11
12
13
14
15
16
17
# File 'lib/grape/validations.rb', line 9

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

  if options.is_a?(Hash) && !options.empty?
    raise Grape::Exceptions.UnknownOptions.new(options.keys)
  end
end

Instance Attribute Details

- (Object) attrs (readonly)

Returns the value of attribute attrs



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

def attrs
  @attrs
end

Class Method Details

+ (Object) inherited(klass)



70
71
72
73
# File 'lib/grape/validations.rb', line 70

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

Instance Method Details

- (Object) validate!(params)



19
20
21
22
23
24
25
26
# File 'lib/grape/validations.rb', line 19

def validate!(params)
  attributes = AttributesIterator.new(self, @scope, params)
  attributes.each do |resource_params, attr_name|
    if @required || resource_params.has_key?(attr_name)
      validate_param!(attr_name, resource_params)
    end
  end
end