Sha256: f0a2f00ed58e16c7a19ad57c05b10cbbb7b9fe214ae0105b42eaddf9098b47a9

Contents?: true

Size: 1.24 KB

Versions: 4

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

4 entries across 4 versions & 1 rubygems

Version Path
openapi3_parser-0.7.0 lib/openapi3_parser/validators/unexpected_fields.rb
openapi3_parser-0.6.1 lib/openapi3_parser/validators/unexpected_fields.rb
openapi3_parser-0.6.0 lib/openapi3_parser/validators/unexpected_fields.rb
openapi3_parser-0.5.2 lib/openapi3_parser/validators/unexpected_fields.rb