Class: Openapi3Parser::NodeFactory::ObjectFactory::FieldConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi3_parser/node_factory/object_factory/field_config.rb

Instance Method Summary collapse

Constructor Details

#initialize(input_type: nil, factory: nil, required: false, default: nil, validate: nil) ⇒ FieldConfig

Returns a new instance of FieldConfig.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/openapi3_parser/node_factory/object_factory/field_config.rb', line 7

def initialize(
  input_type: nil,
  factory: nil,
  required: false,
  default: nil,
  validate: nil
)
  @given_input_type = input_type
  @given_factory = factory
  @given_required = required
  @given_default = default
  @given_validate = validate
end

Instance Method Details

#check_input_type(validatable, building_node) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/openapi3_parser/node_factory/object_factory/field_config.rb', line 39

def check_input_type(validatable, building_node)
  return true if !given_input_type || validatable.input.nil?

  if building_node
    TypeChecker.raise_on_invalid_type(validatable.context,
                                      type: given_input_type)
  else
    TypeChecker.validate_type(validatable, type: given_input_type)
  end
end

#default(factory) ⇒ Object



64
65
66
67
68
# File 'lib/openapi3_parser/node_factory/object_factory/field_config.rb', line 64

def default(factory)
  return given_default.call if given_default.is_a?(Proc)
  return factory.send(given_default) if given_default.is_a?(Symbol)
  given_default
end

#factory?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/openapi3_parser/node_factory/object_factory/field_config.rb', line 21

def factory?
  !given_factory.nil?
end

#initialize_factory(context, parent_factory) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/openapi3_parser/node_factory/object_factory/field_config.rb', line 25

def initialize_factory(context, parent_factory)
  if given_factory.is_a?(Class)
    given_factory.new(context)
  elsif given_factory.is_a?(Symbol)
    parent_factory.send(given_factory, context)
  else
    given_factory.call(context)
  end
end

#required?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/openapi3_parser/node_factory/object_factory/field_config.rb', line 35

def required?
  given_required
end

#validate_field(validatable, building_node) ⇒ Object

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/openapi3_parser/node_factory/object_factory/field_config.rb', line 50

def validate_field(validatable, building_node)
  return true if !given_validate || validatable.input.nil?

  run_validation(validatable)

  return validatable.errors.empty? unless building_node
  return true if validatable.errors.empty?

  error = validatable.errors.first
  location_summary = error.context.location_summary
  raise Error::InvalidData,
        "Invalid data for #{location_summary}: #{error.message}"
end