Class: JSONSchemer::Draft202012::Vocab::Core::DynamicRef

Inherits:
Keyword
  • Object
show all
Defined in:
lib/json_schemer/draft202012/vocab/core.rb

Constant Summary

Constants included from Output

Output::FRAGMENT_ENCODE_REGEX

Instance Attribute Summary

Attributes inherited from Keyword

#parent, #parsed, #root, #value

Attributes included from Output

#keyword, #schema

Instance Method Summary collapse

Methods inherited from Keyword

#absolute_keyword_location, #error_key, #fetch, #initialize, #parsed_schema, #schema_pointer

Methods included from Output

#x_error

Constructor Details

This class inherits a constructor from JSONSchemer::Keyword

Instance Method Details

#dynamic_anchorObject



89
90
91
92
93
# File 'lib/json_schemer/draft202012/vocab/core.rb', line 89

def dynamic_anchor
  return @dynamic_anchor if defined?(@dynamic_anchor)
  fragment = ref_schema.parsed['$dynamicAnchor']&.parsed&.fragment
  @dynamic_anchor = (fragment == ref_uri.fragment ? fragment : nil)
end

#ref_schemaObject



85
86
87
# File 'lib/json_schemer/draft202012/vocab/core.rb', line 85

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

#ref_uriObject



81
82
83
# File 'lib/json_schemer/draft202012/vocab/core.rb', line 81

def ref_uri
  @ref_uri ||= URI.join(schema.base_uri, value)
end

#validate(instance, instance_location, keyword_location, context) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/json_schemer/draft202012/vocab/core.rb', line 95

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

  if dynamic_anchor
    context.dynamic_scope.each do |ancestor|
      dynamic_uri = URI.join(ancestor.base_uri, "##{dynamic_anchor}")
      if ancestor.root.resources.fetch(:dynamic).key?(dynamic_uri)
        schema = ancestor.root.resources.fetch(:dynamic).fetch(dynamic_uri)
        break
      end
    end
  end

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