Class: Apipie::Validator::TypeValidator

Inherits:
BaseValidator show all
Defined in:
lib/apipie/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

#==, #error, find, inherited, #inspect, #inspected_fields, #merge_with, #param_name, #params_ordered, #to_json, #to_s, #valid?

Constructor Details

#initialize(param_description, argument) ⇒ TypeValidator

Returns a new instance of TypeValidator.



101
102
103
104
# File 'lib/apipie/validator.rb', line 101

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

Class Method Details

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



111
112
113
114
115
# File 'lib/apipie/validator.rb', line 111

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



117
118
119
# File 'lib/apipie/validator.rb', line 117

def description
  "Must be a #{@type}"
end

#expected_typeObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/apipie/validator.rb', line 121

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

#validate(value) ⇒ Object



106
107
108
109
# File 'lib/apipie/validator.rb', line 106

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