Class: Apipie::Validator::HashValidator

Inherits:
BaseValidator show all
Includes:
DSL::Base, DSL::Param
Defined in:
lib/apipie/validator.rb

Instance Attribute Summary

Attributes included from DSL::Base

#apipie_resource_descriptions

Attributes inherited from BaseValidator

#param_description

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::Param

#param, #param_group

Methods inherited from BaseValidator

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

Constructor Details

#initialize(param_description, argument, param_group) ⇒ HashValidator

Returns a new instance of HashValidator.



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/apipie/validator.rb', line 185

def initialize(param_description, argument, param_group)
  super(param_description)
  @proc = argument
  @param_group = param_group
  self.instance_exec(&@proc)
  # specifying action_aware on Hash influences the child params,
  # not the hash param itself: assuming it's required when
  # updating as well
  if param_description.options[:action_aware] && param_description.options[:required]
    param_description.required = true
  end
  prepare_hash_params
end

Class Method Details

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



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

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

Instance Method Details

#_default_param_group_scopeObject

where the group definition should be looked up when no scope given. This is expected to return a controller.



231
232
233
# File 'lib/apipie/validator.rb', line 231

def _default_param_group_scope
  @param_group && @param_group[:scope]
end

#descriptionObject



221
222
223
# File 'lib/apipie/validator.rb', line 221

def description
  "Must be a Hash"
end

#expected_typeObject



225
226
227
# File 'lib/apipie/validator.rb', line 225

def expected_type
  'hash'
end

#hash_params_orderedObject



199
200
201
202
203
204
205
# File 'lib/apipie/validator.rb', line 199

def hash_params_ordered
  @hash_params_ordered ||= _apipie_dsl_data[:params].map do |args|
    options = args.find { |arg| arg.is_a? Hash }
    options[:parent] = self.param_description
    Apipie::ParamDescription.from_dsl_data(param_description.method_description, args)
  end
end

#merge_with(other_validator) ⇒ Object



235
236
237
238
239
240
241
242
# File 'lib/apipie/validator.rb', line 235

def merge_with(other_validator)
  if other_validator.is_a? HashValidator
    @hash_params_ordered = ParamDescription.unify(self.hash_params_ordered + other_validator.hash_params_ordered)
    prepare_hash_params
  else
    super
  end
end

#prepare_hash_paramsObject



244
245
246
247
248
# File 'lib/apipie/validator.rb', line 244

def prepare_hash_params
  @hash_params = hash_params_ordered.reduce({}) do |h, param|
    h.update(param.name.to_sym => param)
  end
end

#validate(value) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/apipie/validator.rb', line 207

def validate(value)
  if @hash_params
    @hash_params.each do |k, p|
      if Apipie.configuration.validate_presence?
        raise ParamMissing.new(k) if p.required && !value.has_key?(k)
      end
      if Apipie.configuration.validate_value?
        p.validate(value[k]) if value.has_key?(k)
      end
    end
  end
  return true
end