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_description

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseValidator

find, inherited, #param_name, #to_json, #to_s, #valid?

Constructor Details

#initialize(param_description, argument) ⇒ TypeValidator

Returns a new instance of TypeValidator.



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

def initialize(param_description, argument)
  super(param_description)
  @type = argument
end

Class Method Details

.build(param_description, argument, options, block) ⇒ Object



80
81
82
83
84
# File 'lib/restapi/validator.rb', line 80

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

Instance Method Details

#descriptionObject



90
91
92
# File 'lib/restapi/validator.rb', line 90

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

#errorObject



86
87
88
# File 'lib/restapi/validator.rb', line 86

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

#expected_typeObject



94
95
96
97
98
99
100
101
102
# File 'lib/restapi/validator.rb', line 94

def expected_type
  if @type.ancestors.include? Hash
    'hash'
  elsif @type.ancestors.include? Numeric
    'numeric'
  else
    'string'
  end
end

#validate(value) ⇒ Object



75
76
77
78
# File 'lib/restapi/validator.rb', line 75

def validate(value)
  return false if value.nil?
  value.is_a? @type
end