Module: JSI::Schema::IdWithAnchor

Included in:
Draft04, Draft06, Draft07
Defined in:
lib/jsi/schema.rb

Overview

extends any schema which defines an anchor as a URI fragment in the schema id

Instance Method Summary collapse

Instance Method Details

#anchorString

an anchor defined by a non-empty fragment in the id uri

Returns:

  • (String)


95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/jsi/schema.rb', line 95

def anchor
  if id
    id_uri = Addressable::URI.parse(id)
    if id_uri.fragment == ''
      nil
    else
      id_uri.fragment
    end
  else
    nil
  end
end

#id_without_fragmentAddressable::URI?

a URI for the schema's id, unless the id defines an anchor in its fragment. nil if the schema defines no id.

Returns:

  • (Addressable::URI, nil)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/jsi/schema.rb', line 63

def id_without_fragment
  if id
    id_uri = Addressable::URI.parse(id)
    if id_uri.merge(fragment: nil).empty?
      # fragment-only id is just an anchor
      # e.g. #foo
      nil
    elsif id_uri.fragment == nil
      # no fragment
      # e.g. http://localhost:1234/bar
      id_uri
    elsif id_uri.fragment == ''
      # empty fragment
      # e.g. http://json-schema.org/draft-07/schema#
      id_uri.merge(fragment: nil)
    elsif jsi_schema_base_uri && jsi_schema_base_uri.join(id_uri).merge(fragment: nil) == jsi_schema_base_uri
      # the id, resolved against the base uri, consists of the base uri plus an anchor fragment.
      # so there's no non-fragment id.
      # e.g. base uri is http://localhost:1234/bar
      #        and id is http://localhost:1234/bar#foo
      nil
    else
      # e.g. http://localhost:1234/bar#foo
      id_uri.merge(fragment: nil)
    end
  else
    nil
  end
end