class Openapi3Parser::NodeFactory::ObjectFactory::FieldConfig

Attributes

given_default[R]
given_factory[R]
given_input_type[R]
given_required[R]
given_validate[R]

Public Class Methods

new( input_type: nil, factory: nil, required: false, default: nil, validate: nil ) click to toggle source
# 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

Public Instance Methods

check_input_type(validatable, building_node: false) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/openapi3_parser/node_factory/object_factory/field_config.rb, line 36
def required?
  given_required
end
validate_field(validatable, building_node: false) click to toggle source
# 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

Private Instance Methods

run_validation(validatable) click to toggle source
# File lib/openapi3_parser/node_factory/object_factory/field_config.rb, line 77
def run_validation(validatable)
  if given_validate.is_a?(Symbol)
    validatable.factory.send(given_validate, validatable)
  else
    given_validate.call(validatable)
  end
end