Class: Restapi::Validator::TypeValidator

Inherits:
BaseValidator show all
Defined in:
lib/restapi/validator.rb

Overview

validate arguments type

Instance Attribute Summary

Attributes inherited from BaseValidator

#param_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseValidator

find, #to_json, #to_s, #valid?

Constructor Details

#initialize(argument) ⇒ TypeValidator

Returns a new instance of TypeValidator.



49
50
51
52
# File 'lib/restapi/validator.rb', line 49

def initialize(argument)
  @type = argument
  @type = Integer if @type == Fixnum
end

Class Method Details

.build(argument, options, block) ⇒ Object



63
64
65
# File 'lib/restapi/validator.rb', line 63

def self.build(argument, options, block)
  self.new(argument) if argument.is_a?(Class) && argument != Hash
end

Instance Method Details

#descriptionObject



71
72
73
# File 'lib/restapi/validator.rb', line 71

def description
  "Parameter has to be #{@type}."
end

#errorObject



67
68
69
# File 'lib/restapi/validator.rb', line 67

def error
  "Parameter #{@param_name} expecting to be #{@type.name}, got: #{@error_value.class.name}"
end

#validate(value) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/restapi/validator.rb', line 54

def validate(value)
  return false if value.nil?
  begin
    Kernel.send(@type.to_s, value)
  rescue ArgumentError
    return false
  end
end