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, #format_description_value, #ignore_allow_blank?, 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.



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

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

Class Method Details

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



120
121
122
123
124
# File 'lib/apipie/validator.rb', line 120

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



126
127
128
# File 'lib/apipie/validator.rb', line 126

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

#expected_typeObject



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/apipie/validator.rb', line 130

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

#validate(value) ⇒ Object



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

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