Skip to content

Commit 8cf57b5

Browse files
committed
improve error message when a schema that is not a hash is passed to Schema#initialize, use #to_hash
1 parent c6d82d2 commit 8cf57b5

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/json-schema/pointer.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module JSON
44
class Schema
5+
# a JSON Pointer, as described by RFC 6901 https://tools.ietf.org/html/rfc6901
56
class Pointer
67
class Error < JSON::Schema::SchemaError
78
end
@@ -10,6 +11,15 @@ class PointerSyntaxError < Error
1011
class ReferenceError < Error
1112
end
1213

14+
# parse a fragment to an array of reference tokens
15+
#
16+
# #/foo/bar
17+
#
18+
# => ['foo', 'bar']
19+
#
20+
# #/foo%20bar
21+
#
22+
# => ['foo bar']
1323
def self.parse_fragment(fragment)
1424
fragment = Addressable::URI.unescape(fragment)
1525
match = fragment.match(/\A#/)
@@ -20,6 +30,15 @@ def self.parse_fragment(fragment)
2030
end
2131
end
2232

33+
# parse a pointer to an array of reference tokens
34+
#
35+
# /foo
36+
#
37+
# => ['foo']
38+
#
39+
# /foo~0bar/baz~1qux
40+
#
41+
# => ['foo~bar', 'baz/qux']
2342
def self.parse_pointer(pointer_string)
2443
tokens = pointer_string.split('/', -1).map! do |piece|
2544
piece.gsub('~1', '/').gsub('~0', '~')
@@ -33,6 +52,13 @@ def self.parse_pointer(pointer_string)
3352
end
3453
end
3554

55+
# initializes a JSON::Schema::Pointer from the given representation.
56+
#
57+
# type may be one of:
58+
#
59+
# - :fragment - the representation is a fragment containing a pointer (starting with #)
60+
# - :pointer - the representation is a pointer (starting with /)
61+
# - :reference_tokens - the representation is an array of tokens referencing a path in a document
3662
def initialize(type, representation)
3763
@type = type
3864
if type == :reference_tokens
@@ -49,6 +75,8 @@ def initialize(type, representation)
4975

5076
attr_reader :reference_tokens
5177

78+
# takes a root json document and evaluates this pointer through the document, returning the value
79+
# pointed to by this pointer.
5280
def evaluate(document)
5381
reference_tokens.inject(document) do |value, token|
5482
if value.is_a?(Array)

lib/json-schema/schema.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ class Schema
66
attr_accessor :schema, :uri, :validator
77

88
def initialize(schema,uri,parent_validator=nil)
9-
@schema = schema
9+
unless schema.respond_to?(:to_hash)
10+
raise ArgumentError, "schema must be a hash; got: #{schema.inspect}"
11+
end
12+
13+
@schema = schema.to_hash
1014
@uri = uri
1115

1216
# If there is an ID on this schema, use it to generate the URI

0 commit comments

Comments
 (0)