Class: Restapi::Validator::HashValidator

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

Instance Attribute Summary collapse

Attributes inherited from BaseValidator

#param_description

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseValidator

find, inherited, #param_name, #to_json, #to_s, #valid?

Constructor Details

#initialize(param_description, argument) ⇒ HashValidator

Returns a new instance of HashValidator.



187
188
189
190
191
192
193
194
# File 'lib/restapi/validator.rb', line 187

def initialize(param_description, argument)
  super(param_description)
  @proc = argument
  @hash_params_ordered = []
  @hash_params = {}

  self.instance_exec(&@proc)
end

Instance Attribute Details

#hash_params_orderedObject (readonly)

Returns the value of attribute hash_params_ordered.



181
182
183
# File 'lib/restapi/validator.rb', line 181

def hash_params_ordered
  @hash_params_ordered
end

Class Method Details

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



183
184
185
# File 'lib/restapi/validator.rb', line 183

def self.build(param_description, argument, options, block)
  self.new(param_description, block) if block.is_a?(Proc) && block.arity <= 0 && argument == Hash
end

Instance Method Details

#descriptionObject



209
210
211
# File 'lib/restapi/validator.rb', line 209

def description
  "Has to be hash."
end

#errorObject



205
206
207
# File 'lib/restapi/validator.rb', line 205

def error
  "Has to be hash."
end

#expected_typeObject



220
221
222
# File 'lib/restapi/validator.rb', line 220

def expected_type
  'hash'
end

#param(param_name, *args, &block) ⇒ Object



213
214
215
216
217
218
# File 'lib/restapi/validator.rb', line 213

def param(param_name, *args, &block)
  param_description = Restapi::ParamDescription.new(param_name, *args, &block)
  param_description.parent = self.param_description
  @hash_params_ordered << param_description
  @hash_params[param_name.to_sym] = param_description
end

#validate(value) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/restapi/validator.rb', line 196

def validate(value)
  if @hash_params
    @hash_params.each do |k, p|
      p.validate(value[k]) if value.has_key?(k) || p.required
    end
  end
  return true
end