Sha256: 7a2d1ec009cda56cfc3651abac8c97fd75f7932bd601b6a98ce1b718aa9dc3b8

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module Schemacop
  module V3
    class ReferenceNode < Node
      def self.allowed_options
        super + %i[path]
      end

      def self.create(path, **options, &block)
        options[:path] = path
        super(**options, &block)
      end

      def as_json
        if context.swagger_json?
          process_json([], '$ref': "#/components/schemas/#{@path}")
        else
          process_json([], '$ref': "#/definitions/#{@path}")
        end
      end

      def _validate(data, result:)
        super_data = super
        return if super_data.nil?

        # Lookup schema #
        node = target
        fail "Schema #{@path.to_s.inspect} not found." unless node

        # Validate schema #
        node._validate(super_data, result: result)
      end

      def target
        schemas[@path] || Schemacop.context.schemas[@path] || GlobalContext.schema_for(@path)
      end

      def cast(data)
        data = default if data.nil?
        return target.cast(data)
      end

      def used_external_schemas(encountered_nodes = Set.new)
        if encountered_nodes.include?(self)
          return []
        end

        target_children_schema = target.used_external_schemas(encountered_nodes + [self])

        if schemas.include?(@path)
          return target_children_schema
        else
          return [@path] + target_children_schema
        end
      end

      protected

      def init
        @path = options.delete(:path)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
schemacop-3.0.30 lib/schemacop/v3/reference_node.rb