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, #merge_with, #param_name, #params_ordered, #to_json, #to_s, #valid?

Constructor Details

#initialize(param_description, argument) ⇒ TypeValidator

Returns a new instance of TypeValidator.



82
83
84
85
# File 'lib/apipie/validator.rb', line 82

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

Class Method Details

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



92
93
94
95
96
# File 'lib/apipie/validator.rb', line 92

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



98
99
100
# File 'lib/apipie/validator.rb', line 98

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

#expected_typeObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/apipie/validator.rb', line 102

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



87
88
89
90
# File 'lib/apipie/validator.rb', line 87

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