Class: ApipieDSL::Validator::TypeValidator

Inherits:
BaseValidator show all
Defined in:
lib/apipie_dsl/validator.rb

Instance Attribute Summary

Attributes inherited from BaseValidator

#param_description

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseValidator

#==, #docs, find, inherited, #inspect, #inspected_fields, #merge_with, #sub_params, #to_s, #valid?

Constructor Details

#initialize(param_description, argument) ⇒ TypeValidator

Returns a new instance of TypeValidator.



102
103
104
105
# File 'lib/apipie_dsl/validator.rb', line 102

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

Class Method Details

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



107
108
109
110
111
112
# File 'lib/apipie_dsl/validator.rb', line 107

def self.build(param_description, argument, _options, block)
  return unless argument.is_a?(::Class)
  return if argument == Hash && !block.nil?

  new(param_description, argument)
end

Instance Method Details

#descriptionObject



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

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

#expected_typeObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/apipie_dsl/validator.rb', line 124

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



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

def validate(value)
  return false if value.nil?

  value.is_a?(@type)
end