class Grape::Validations::Validator

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

Attributes

attrs[R]

Public Class Methods

inherited(klass) click to toggle source
# File lib/grape/validations.rb, line 73
def self.inherited(klass)
  short_name = convert_to_short_name(klass)
  Validations::register_validator(short_name, klass)
end
new(attrs, options, required, scope) click to toggle source
# File lib/grape/validations.rb, line 11
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

Private Class Methods

convert_to_short_name(klass) click to toggle source
# File lib/grape/validations.rb, line 50
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

Public Instance Methods

validate!(params) click to toggle source
# File lib/grape/validations.rb, line 21
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