class Openapi3Parser::NodeFactory::Map::ValidNodeBuilder

Attributes

factory[R]
validatable[R]

Public Class Methods

data(factory, parent_context) click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 107
def self.data(factory, parent_context)
  new(factory).data(parent_context)
end
errors(factory) click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 103
def self.errors(factory)
  new(factory).errors
end

Private Class Methods

new(factory) click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 111
def initialize(factory)
  @factory = factory
  @validatable = Validation::Validatable.new(factory)
end

Public Instance Methods

data(parent_context) click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 126
def data(parent_context)
  return default_value if factory.nil_input?

  TypeChecker.raise_on_invalid_type(factory.context, type: ::Hash)
  check_keys(raise_on_invalid: true)
  check_values(raise_on_invalid: true)
  validate(raise_on_invalid: true)

  factory.data.each_with_object({}) do |(key, value), memo|
    memo[key] = if value.respond_to?(:node)
                  Node::Placeholder.new(value, key, parent_context)
                else
                  value
                end
  end
end
errors() click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 116
def errors
  return validatable.collection if factory.nil_input?

  TypeChecker.validate_type(validatable, type: ::Hash)
  return validatable.collection if validatable.errors.any?

  collate_errors
  validatable.collection
end

Private Instance Methods

check_field_type(context, raise_on_invalid) click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 189
def check_field_type(context, raise_on_invalid)
  if raise_on_invalid
    TypeChecker.raise_on_invalid_type(context,
                                      type: factory.value_input_type)
  else
    TypeChecker.validate_type(validatable,
                              type: factory.value_input_type,
                              context: context)
  end
end
check_keys(raise_on_invalid: false) click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 167
def check_keys(raise_on_invalid: false)
  if raise_on_invalid
    TypeChecker.raise_on_invalid_keys(factory.context,
                                      type: ::String)
  else
    TypeChecker.validate_keys(validatable,
                              type: ::String,
                              context: factory.context)
  end
end
check_values(raise_on_invalid: false) click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 178
def check_values(raise_on_invalid: false)
  return unless factory.value_input_type

  factory.context.input.each do |key, value|
    next if factory.allow_extensions && key.to_s =~ EXTENSION_REGEX

    check_field_type(Context.next_field(factory.context, key, value),
                     raise_on_invalid)
  end
end
collate_errors() click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 149
def collate_errors
  check_keys(raise_on_invalid: false)
  check_values(raise_on_invalid: false)
  validate(raise_on_invalid: false)

  factory.data.each_value do |value|
    validatable.add_errors(value.errors) if value.respond_to?(:errors)
  end
end
default_value() click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 159
def default_value
  if factory.nil_input? && factory.default.nil?
    nil
  else
    factory.data
  end
end
run_validation() click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 211
def run_validation
  if factory.validation.is_a?(Symbol)
    factory.send(factory.validation, validatable)
  else
    factory.validation&.call(validatable)
  end
end
validate(raise_on_invalid: false) click to toggle source
# File lib/openapi3_parser/node_factory/map.rb, line 200
def validate(raise_on_invalid: false)
  run_validation

  return if !raise_on_invalid || validatable.errors.empty?

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