Sha256: 20d769726d37c7d26bd6feb83a889b6e900feb622c46fffa6522fb4a329024fa

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require "openapi3_parser/node_factory/object"

module Openapi3Parser
  module NodeFactory
    class RequestBody < NodeFactory::Object
      allow_extensions
      field "description", input_type: String
      field "content", factory: :content_factory, required: true
      field "required", input_type: :boolean, default: false

      private

      def build_object(data, context)
        Node::RequestBody.new(data, context)
      end

      def content_factory(context)
        NodeFactory::Map.new(
          context,
          value_factory: NodeFactory::MediaType,
          validate: ContentValidator
        )
      end

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

        def call(validatable)
          # This validation isn't actually mentioned in the spec, but it
          # doesn't seem to make sense if this is an empty hash.
          return validatable.add_error("Expected to have at least 1 item") if validatable.input.size.zero?

          validatable.input.each_key do |key|
            message = Validators::MediaType.call(key)
            next unless message

            context = Context.next_field(validatable.context, key)
            validatable.add_error(message, context)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
openapi3_parser-0.9.2 lib/openapi3_parser/node_factory/request_body.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/openapi3_parser-0.9.1/lib/openapi3_parser/node_factory/request_body.rb
openapi3_parser-0.9.1 lib/openapi3_parser/node_factory/request_body.rb
openapi3_parser-0.9.0 lib/openapi3_parser/node_factory/request_body.rb