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: false) ⇒ Object



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

def check_input_type(validatable, building_node: false)
  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 = nil) ⇒ Object



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

def default(factory = nil)
  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 = nil) ⇒ Object



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

def initialize_factory(context, parent_factory = nil)
  case given_factory
  when Class
    given_factory.new(context)
  when Symbol
    parent_factory.send(given_factory, context)
  else
    given_factory.call(context)
  end
end

#required?Boolean

Returns:

  • (Boolean)


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

def required?
  given_required
end

#validate_field(validatable, building_node: false) ⇒ Object

Raises:



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

def validate_field(validatable, building_node: false)
  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