Sha256: 59179ab219b25fbd849562d9c40b404871afcf6d37744d3637f386f23ad69e7d

Contents?: true

Size: 1.31 KB

Versions: 23

Compression:

Stored size: 1.31 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
        target_children_schema = target.used_external_schemas

        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

23 entries across 23 versions & 1 rubygems

Version Path
schemacop-3.0.29 lib/schemacop/v3/reference_node.rb
schemacop-3.0.28 lib/schemacop/v3/reference_node.rb
schemacop-3.0.27 lib/schemacop/v3/reference_node.rb
schemacop-3.0.26 lib/schemacop/v3/reference_node.rb
schemacop-3.0.25 lib/schemacop/v3/reference_node.rb
schemacop-3.0.24 lib/schemacop/v3/reference_node.rb
schemacop-3.0.23 lib/schemacop/v3/reference_node.rb
schemacop-3.0.22 lib/schemacop/v3/reference_node.rb
schemacop-3.0.21 lib/schemacop/v3/reference_node.rb
schemacop-3.0.20 lib/schemacop/v3/reference_node.rb
schemacop-3.0.19 lib/schemacop/v3/reference_node.rb
schemacop-3.0.18 lib/schemacop/v3/reference_node.rb
schemacop-3.0.17 lib/schemacop/v3/reference_node.rb
schemacop-3.0.16 lib/schemacop/v3/reference_node.rb
schemacop-3.0.15 lib/schemacop/v3/reference_node.rb
schemacop-3.0.14 lib/schemacop/v3/reference_node.rb
schemacop-3.0.13 lib/schemacop/v3/reference_node.rb
schemacop-3.0.12 lib/schemacop/v3/reference_node.rb
schemacop-3.0.11 lib/schemacop/v3/reference_node.rb
schemacop-3.0.10 lib/schemacop/v3/reference_node.rb