Sha256: 21c3b5102ef014ca50cfaeeed7c5caf6ae032084ed8dd98054c81b85c398c916

Contents?: true

Size: 1.29 KB

Versions: 6

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true
module JSONSchemer
  module Draft201909
    module Vocab
      module Core
        class RecursiveAnchor < Keyword
          def parse
            root.resources[:dynamic][schema.base_uri] = schema if value == true
            value
          end
        end

        class RecursiveRef < Keyword
          def ref_uri
            @ref_uri ||= URI.join(schema.base_uri, value)
          end

          def ref_schema
            @ref_schema ||= root.resolve_ref(ref_uri)
          end

          def recursive_anchor
            return @recursive_anchor if defined?(@recursive_anchor)
            @recursive_anchor = (ref_schema.parsed['$recursiveAnchor']&.parsed == true)
          end

          def validate(instance, instance_location, keyword_location, context)
            schema = ref_schema

            if recursive_anchor
              context.dynamic_scope.each do |ancestor|
                if ancestor.root.resources.fetch(:dynamic).key?(ancestor.base_uri)
                  schema = ancestor.root.resources.fetch(:dynamic).fetch(ancestor.base_uri)
                  break
                end
              end
            end

            schema.validate_instance(instance, instance_location, keyword_location, context)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
json_schemer-2.3.0 lib/json_schemer/draft201909/vocab/core.rb
json_schemer-2.2.1 lib/json_schemer/draft201909/vocab/core.rb
json_schemer-2.2.0 lib/json_schemer/draft201909/vocab/core.rb
json_schemer-2.1.1 lib/json_schemer/draft201909/vocab/core.rb
json_schemer-2.1.0 lib/json_schemer/draft201909/vocab/core.rb
json_schemer-2.0.0 lib/json_schemer/draft201909/vocab/core.rb