Class: Restapi::Validator::TypeValidator
Overview
Instance Attribute Summary
#param_description
Class Method Summary
collapse
Instance Method Summary
collapse
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
#description ⇒ Object
90
91
92
|
# File 'lib/restapi/validator.rb', line 90
def description
"Parameter has to be #{@type}."
end
|
#error ⇒ Object
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_type ⇒ Object
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
|