class Openapi3Parser::NodeFactory::TypeChecker

Attributes

type[R]

Public Class Methods

new(type) click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 24
def initialize(type)
  @type = type
end
raise_on_invalid_keys(context, type:) click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 18
def self.raise_on_invalid_keys(context, type:)
  new(type).raise_on_invalid_keys(context)
end
raise_on_invalid_type(context, type:) click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 10
def self.raise_on_invalid_type(context, type:)
  new(type).raise_on_invalid_type(context)
end
validate_keys(validatable, type:, context: nil) click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 14
def self.validate_keys(validatable, type:, context: nil)
  new(type).validate_keys(validatable, context)
end
validate_type(validatable, type:, context: nil) click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 6
def self.validate_type(validatable, type:, context: nil)
  new(type).validate_type(validatable, context)
end

Public Instance Methods

raise_on_invalid_keys(context) click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 60
def raise_on_invalid_keys(context)
  return true if !type || valid_keys?(context.input)

  raise Error::InvalidType,
        "Invalid keys for #{context.location_summary}: "\
        "#{keys_error_message}"
end
raise_on_invalid_type(context) click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 52
def raise_on_invalid_type(context)
  return true if !type || valid_type?(context.input)

  raise Error::InvalidType,
        "Invalid type for #{context.location_summary}: "\
        "#{field_error_message}"
end
validate_keys(validatable, context) click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 40
def validate_keys(validatable, context)
  return true unless type

  context ||= validatable.context
  valid_keys?(context.input).tap do |valid|
    next if valid

    validatable.add_error("Invalid keys. #{keys_error_message}",
                          context)
  end
end
validate_type(validatable, context) click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 28
def validate_type(validatable, context)
  return true unless type

  context ||= validatable.context
  valid_type?(context.input).tap do |valid|
    next if valid

    validatable.add_error("Invalid type. #{field_error_message}",
                          context)
  end
end

Private Instance Methods

field_error_message() click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 83
def field_error_message
  "Expected #{type_name_for_error}"
end
keys_error_message() click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 87
def keys_error_message
  "Expected keys to be of type #{type_name_for_error}"
end
type_name_for_error() click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 91
def type_name_for_error
  if type == Hash
    "Object"
  elsif type == :boolean
    "Boolean"
  else
    type.to_s
  end
end
valid_keys?(input) click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 101
def valid_keys?(input)
  input.keys.all? { |key| valid_type?(key) }
end
valid_type?(input) click to toggle source
# File lib/openapi3_parser/node_factory/type_checker.rb, line 72
def valid_type?(input)
  return [true, false].include?(input) if type == :boolean

  unless type.is_a?(Class)
    raise Error::UnvalidatableType,
          "Expected #{type} to be a Class not a #{type.class}"
  end

  input.is_a?(type)
end