Sha256: 078e64a9fe671322ba5454f9b6602eb564f852eaa96b9de4da20d362b55b3c94

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require "openapi3_parser/array_sentence"

module Openapi3Parser
  module Validators
    class UnexpectedFields
      using ArraySentence
      private_class_method :new

      def self.call(*args, **kwargs)
        new.call(*args, **kwargs)
      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.grep_v(NodeFactory::EXTENSION_REGEX)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
openapi3_parser-0.9.2 lib/openapi3_parser/validators/unexpected_fields.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/openapi3_parser-0.9.1/lib/openapi3_parser/validators/unexpected_fields.rb
openapi3_parser-0.9.1 lib/openapi3_parser/validators/unexpected_fields.rb