Class: Openapi3Parser::NodeFactory::TypeChecker

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ TypeChecker

Returns a new instance of TypeChecker.



24
25
26
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 24

def initialize(type)
  @type = type
end

Class Method Details

.raise_on_invalid_keys(context, type:) ⇒ Object



18
19
20
# 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:) ⇒ Object



10
11
12
# 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) ⇒ Object



14
15
16
# 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) ⇒ Object



6
7
8
# 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

Instance Method Details

#raise_on_invalid_keys(context) ⇒ Object

Raises:



55
56
57
58
59
60
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 55

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

Raises:



48
49
50
51
52
53
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 48

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



38
39
40
41
42
43
44
45
46
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 38

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



28
29
30
31
32
33
34
35
36
# 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