Sha256: 70b32ddc112e1cdbaff5f545d2504f92e5098eaa72bf89626c9c8ca2cb74c9bd

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require "openapi3_parser/array_sentence"
require "openapi3_parser/error"

module Openapi3Parser
  module Validators
    class UnexpectedFields
      using ArraySentence
      private_class_method :new

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

      def call(validatable,
               allowed_fields:,
               allow_extensions: true,
               raise_on_invalid: true)
        fields = unexpected_fields(validatable.input,
                                   allowed_fields,
                                   allow_extensions)
        return if fields.empty?

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

      private

      def unexpected_fields(input, allowed_fields, allow_extensions)
        extra_keys = input.keys - allowed_fields
        return extra_keys unless allow_extensions
        extra_keys.reject { |key| key =~ NodeFactory::EXTENSION_REGEX }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
openapi3_parser-0.5.1 lib/openapi3_parser/validators/unexpected_fields.rb