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)


100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/jsi/schema.rb', line 100

def anchor
  if id
    id_uri = Util.uri(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)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jsi/schema.rb', line 68

def id_without_fragment
  if id
    id_uri = Util.uri(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).freeze
    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).freeze
    end
  else
    nil
  end
end