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, raise_if_missing_params, #to_json, #to_s, #valid?

Constructor Details

#initialize(param_description, argument) ⇒ TypeValidator

Returns a new instance of TypeValidator.



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

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

Class Method Details

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



130
131
132
133
134
# File 'lib/apipie/validator.rb', line 130

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



136
137
138
# File 'lib/apipie/validator.rb', line 136

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

#expected_typeObject



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/apipie/validator.rb', line 140

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



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

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