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.



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

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.



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

def hash_params_ordered
  @hash_params_ordered
end

Class Method Details

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



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

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



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

def description
  "Has to be hash."
end

#errorObject



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

def error
  "Has to be hash."
end

#expected_typeObject



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

def expected_type
  'hash'
end

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



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

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



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

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