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.



27
28
29
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 27

def initialize(type)
  @type = type
end

Class Method Details

.raise_on_invalid_keys(context, type:) ⇒ Object



21
22
23
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 21

def self.raise_on_invalid_keys(context, type:)
  new(type).raise_on_invalid_keys(context)
end

.raise_on_invalid_type(context, type:) ⇒ Object



13
14
15
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 13

def self.raise_on_invalid_type(context, type:)
  new(type).raise_on_invalid_type(context)
end

.validate_keys(validatable, type:, context: nil) ⇒ Object



17
18
19
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 17

def self.validate_keys(validatable, type:, context: nil)
  new(type).validate_keys(validatable, context)
end

.validate_type(validatable, type:, context: nil) ⇒ Object



9
10
11
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 9

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:



58
59
60
61
62
63
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 58

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:



51
52
53
54
55
56
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 51

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



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

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



31
32
33
34
35
36
37
38
39
# File 'lib/openapi3_parser/node_factory/type_checker.rb', line 31

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