Sha256: f088c50a5760c2471bcf6a0b32de8ee2dfedd21525aec0a0c3fb677aabdfaee7

Contents?: true

Size: 969 Bytes

Versions: 7

Compression:

Stored size: 969 Bytes

Contents

# frozen_string_literal: true

require "openapi3_parser/array_sentence"

module Openapi3Parser
  module Validators
    class RequiredFields
      using ArraySentence
      private_class_method :new

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

      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
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
openapi3_parser-0.9.2 lib/openapi3_parser/validators/required_fields.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/openapi3_parser-0.9.1/lib/openapi3_parser/validators/required_fields.rb
openapi3_parser-0.9.1 lib/openapi3_parser/validators/required_fields.rb
openapi3_parser-0.9.0 lib/openapi3_parser/validators/required_fields.rb
openapi3_parser-0.8.2 lib/openapi3_parser/validators/required_fields.rb
openapi3_parser-0.8.1 lib/openapi3_parser/validators/required_fields.rb
openapi3_parser-0.8.0 lib/openapi3_parser/validators/required_fields.rb