Class: Openapi3Parser::Validators::RequiredFields

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi3_parser/validators/required_fields.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(*args, **kwargs) ⇒ Object


11
12
13
# File 'lib/openapi3_parser/validators/required_fields.rb', line 11

def self.call(*args, **kwargs)
  new.call(*args, **kwargs)
end

Instance Method Details

#call(validatable, required_fields:, raise_on_invalid: true) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/openapi3_parser/validators/required_fields.rb', line 15

def call(validatable,
         required_fields:,
         raise_on_invalid: true)
  input = validatable.input
  missing_fields = required_fields.select { |name| input[name].nil? }

  return if missing_fields.empty?

  if raise_on_invalid
    location_summary = validatable.context.location_summary
    raise Openapi3Parser::Error::MissingFields,
          "Missing required fields for "\
          "#{location_summary}: #{missing_fields.sentence_join}"
  else
    validatable.add_error(
      "Missing required fields: #{missing_fields.sentence_join}"
    )
  end
end