File: core.rb

package info (click to toggle)
ruby-json-schemer 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: ruby: 7,428; makefile: 4; sh: 4
file content (45 lines) | stat: -rw-r--r-- 1,318 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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