Class: ApipieDSL::Validator::HashValidator

Inherits:
BaseValidator show all
Includes:
Base, Klass, Parameter
Defined in:
lib/apipie_dsl/validator.rb

Constant Summary

Constants included from Parameter

Parameter::SUPPORTED_TYPES

Instance Attribute Summary

Attributes inherited from BaseValidator

#param_description

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Klass

#app_info, #class_description, #default_prop_group_scope, #define_prop_group, #name, #prop_group, #property, #refs, #sections

Methods included from Parameter

#block, #define_param_group, #keyword, #kwlist, #list, #optional, #param, #param_group, #required

Methods included from Base

#apipie_eval_dsl, #dsl_data, #dsl_data_clear

Methods inherited from BaseValidator

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

Constructor Details

#initialize(param_description, argument, param_group) ⇒ HashValidator

Returns a new instance of HashValidator.



305
306
307
308
309
310
# File 'lib/apipie_dsl/validator.rb', line 305

def initialize(param_description, argument, param_group)
  super(param_description)
  @param_group = param_group
  instance_exec(&argument)
  prepare_hash_params
end

Class Method Details

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



312
313
314
315
316
# File 'lib/apipie_dsl/validator.rb', line 312

def self.build(param_description, argument, options, block)
  return if argument != Hash || !block.is_a?(Proc) || block.arity.positive?

  new(param_description, block, options[:param_group])
end

Instance Method Details

#default_param_group_scopeObject



343
344
345
# File 'lib/apipie_dsl/validator.rb', line 343

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

#descriptionObject



335
336
337
# File 'lib/apipie_dsl/validator.rb', line 335

def description
  'Must be a Hash'
end

#expected_typeObject



339
340
341
# File 'lib/apipie_dsl/validator.rb', line 339

def expected_type
  'hash'
end

#merge_with(other_validator) ⇒ Object



347
348
349
350
351
352
353
354
# File 'lib/apipie_dsl/validator.rb', line 347

def merge_with(other_validator)
  if other_validator.is_a?(HashValidator)
    @sub_params = ApipieDSL::ParameterDescription.unify(sub_params + other_validator.sub_params)
    prepare_hash_params
  else
    super
  end
end

#sub_paramsObject



318
319
320
321
322
323
324
# File 'lib/apipie_dsl/validator.rb', line 318

def sub_params
  @sub_params ||= dsl_data[:params].map do |args|
    options = args.find { |arg| arg.is_a?(Hash) }
    options[:parent] = param_description
    ApipieDSL::ParameterDescription.from_dsl_data(param_description.method_description, args)
  end
end

#validate(value) ⇒ Object



326
327
328
329
330
331
332
333
# File 'lib/apipie_dsl/validator.rb', line 326

def validate(value)
  return false unless value.is_a?(Hash)

  @hash_params&.each do |name, param|
    param.validate(value[name]) if ApipieDSL.configuration.validate_value? && value.key?(name)
  end
  true
end