json_schema_tools 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/CHANGELOG.md +4 -0
- data/lib/schema_tools/ref_resolver.rb +66 -55
- data/lib/schema_tools/version.rb +1 -1
- data/spec/fixtures/schemata/circular_references_twice.json +3 -0
- data/spec/fixtures/schemata/client.json +1 -1
- data/spec/schema_tools/ref_resolver_spec.rb +8 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDI3MjBjMGQ2NjA2YjJkN2Y1ZjFiN2NjZTM2NTMzMTJlYmEwNzE5Mg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YTk0Mjg5NTU4MjlkNzA5YzJlYzExNDAyNWVlNDI0MDM5ZmQ1YzI3Nw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YWZlOGQyODY4MTg0ZDBkMGYzZGFhMDlmZWMyOTE0ZTA1OTY2YTk4NWUxNTA1
|
10
|
+
OTIzYWYxYWEwYzJkMjI5YjY0NmM2MjIzNzczNzViMGZiODgzMTcyZTIwNTNi
|
11
|
+
OWM3YTdmNWRkMTRkYzBkYWJlZjMyNzJhOGY1YTA4ZjJiNmMwOTI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZDJiZGQ2NWQ5YmIxN2FjMTVmMTVhOGQxYWNhOWU5NDM5NDkxYjY2OWE3ZjJl
|
14
|
+
YTNkMmQ2ODFjNzI2NGU4M2U0YmIyODgzNTBlZGU4MDZlMDJmY2RhZWE3MzJk
|
15
|
+
MGFmZGVlNDlkMmQ1ZjA1YmEzYTAwOTAzMzYxZDdiZjgzY2NlNzI=
|
data/CHANGELOG.md
CHANGED
@@ -6,72 +6,83 @@ require 'uri'
|
|
6
6
|
module SchemaTools
|
7
7
|
class RefResolver
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
#
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
9
|
+
#
|
10
|
+
# super basic resolving of JSON Pointer stuff in order
|
11
|
+
# to be able to load $ref parameter.
|
12
|
+
#
|
13
|
+
# $refs in JSON Schema are defined in JSON Pointer syntax:
|
14
|
+
# http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07
|
15
|
+
# JSON Pointer is a big, abandoned WIP and we're going to start by only
|
16
|
+
# implementing the part's we need ...
|
17
|
+
#
|
18
|
+
# @param [String] json_pointer the JSON Pointer expression to evaluate
|
19
|
+
# @param [Schema] relative_to if the pointer refers to a local schema, this is this
|
20
|
+
# the hash to evaluate it against. If the pointer contains a uri to a
|
21
|
+
# referenced schema, an attempt is made to load
|
22
|
+
def self.load_json_pointer(json_pointer, relative_to = nil)
|
23
|
+
|
24
|
+
if json_pointer[/#/]
|
25
|
+
# hash-symbol syntax pointing to a property of a schema. client.json#properties
|
26
|
+
raise "invalid json pointer: #{json_pointer}" unless json_pointer =~ /^(.*)#(.*)/
|
27
|
+
uri, pointer = json_pointer.match(/^(.*)#(.*)/).captures
|
28
|
+
else
|
29
|
+
uri = json_pointer
|
30
|
+
end
|
31
|
+
|
32
|
+
raise "invalid uri pointer: #{json_pointer}" if uri.empty?
|
26
33
|
|
27
|
-
|
28
|
-
pointer = $2
|
29
|
-
schema = {}
|
30
|
-
unless uri.empty?
|
34
|
+
schema = {}
|
31
35
|
uri = URI.parse(uri)
|
32
36
|
raise "must currently be a relative uri: #{json_pointer}" if uri.absolute?
|
33
|
-
# TODO use
|
37
|
+
# TODO use local tools instance or base path from global SchemaTools.schema_path
|
34
38
|
base_dir = relative_to ? relative_to.absolute_dir : SchemaTools.schema_path
|
35
|
-
path =
|
36
|
-
unless File.exist?(path)
|
37
|
-
# try to find in main-dir and subdirs of global schema path and if
|
38
|
-
# present a schema's absolute dir
|
39
|
-
filename = uri.path.split('/').last
|
40
|
-
search_dirs = [File.join(SchemaTools.schema_path, filename),
|
41
|
-
File.join(SchemaTools.schema_path, '**/*', filename)]
|
42
|
-
if relative_to
|
43
|
-
search_dirs += [ File.join(relative_to.absolute_dir, filename),
|
44
|
-
File.join(relative_to.absolute_dir, '**/*', filename) ]
|
45
|
-
end
|
46
|
-
recursive_search = Dir.glob(search_dirs)[0]
|
47
|
-
# if still not found keep orig path to throw error on open
|
48
|
-
path = recursive_search || path
|
49
|
-
end
|
39
|
+
path = find_local_file_path(base_dir, uri.path, relative_to)
|
50
40
|
open (path) {|f| schema = JSON.parse(f.read) }
|
51
|
-
end
|
52
|
-
|
53
|
-
return self._retrieve_pointer_from_object pointer, schema
|
54
|
-
end
|
55
41
|
|
42
|
+
if pointer
|
43
|
+
self._retrieve_pointer_from_object(pointer, schema)
|
44
|
+
else
|
45
|
+
schema
|
46
|
+
end
|
47
|
+
end
|
56
48
|
|
57
|
-
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
|
49
|
+
# @param [String] base_dir
|
50
|
+
# @param [String] path relative file name with optional sub-path prefix:
|
51
|
+
# contact.json, ./contacts/client.json
|
52
|
+
# @param [Schema] relative_to If the pointer contains a uri to a referenced
|
53
|
+
# schema, an attempt is made to load it from the relatives absolute dir
|
54
|
+
def self.find_local_file_path(base_dir, file_path, relative_to=nil)
|
55
|
+
path = File.join(base_dir, file_path)
|
56
|
+
return path if File.exist?(path)
|
62
57
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
58
|
+
# try to find in main-dir and subdirs of global schema path and if present
|
59
|
+
# a schema's absolute dir
|
60
|
+
filename = file_path.split('/').last
|
61
|
+
search_dirs = [File.join(SchemaTools.schema_path, filename),
|
62
|
+
File.join(SchemaTools.schema_path, '**/*', filename)]
|
63
|
+
if relative_to
|
64
|
+
search_dirs += [ File.join(relative_to.absolute_dir, filename),
|
65
|
+
File.join(relative_to.absolute_dir, '**/*', filename) ]
|
66
|
+
end
|
67
|
+
recursive_search = Dir.glob(search_dirs)[0]
|
68
|
+
# if still not found return orig path to throw error on open
|
69
|
+
recursive_search || path
|
67
70
|
end
|
68
71
|
|
69
|
-
return object
|
70
|
-
end
|
71
|
-
|
72
|
-
end # module
|
73
|
-
end # module
|
74
72
|
|
73
|
+
def self._retrieve_pointer_from_object(pointer, object)
|
74
|
+
# assume path to be the JSONPointer expression:
|
75
|
+
# json/pointer/expression
|
76
|
+
# and obj to be the ruby hash representation of the json
|
77
|
+
path = pointer.is_a?(Array) ? pointer : pointer.split("/")
|
75
78
|
|
79
|
+
while object != nil && component = path.shift
|
80
|
+
component = component.to_i if object.is_a?(Array) && component =~ /^\d+$/
|
81
|
+
object = object[component]
|
82
|
+
end
|
76
83
|
|
84
|
+
return object
|
85
|
+
end
|
77
86
|
|
87
|
+
end
|
88
|
+
end
|
data/lib/schema_tools/version.rb
CHANGED
@@ -9,10 +9,6 @@ describe SchemaTools::RefResolver do
|
|
9
9
|
|
10
10
|
pointer = "bla/blub"
|
11
11
|
found = SchemaTools::RefResolver._retrieve_pointer_from_object pointer, obj
|
12
|
-
# not sure what I am doing wrong here, but:
|
13
|
-
# found.should eq :success
|
14
|
-
# does not work because
|
15
|
-
# undefined method `eq'
|
16
12
|
found.should eq :success
|
17
13
|
|
18
14
|
found = SchemaTools::RefResolver._retrieve_pointer_from_object "non/existant/path", obj
|
@@ -62,13 +58,19 @@ describe SchemaTools::RefResolver do
|
|
62
58
|
}.to raise_error
|
63
59
|
end
|
64
60
|
|
65
|
-
it '
|
61
|
+
it 'loads local ref' do
|
66
62
|
pointer = "./basic_definitions.json#definitions"
|
67
63
|
obj = SchemaTools::RefResolver.load_json_pointer(pointer)
|
68
64
|
obj.length.should eq 3
|
69
65
|
end
|
70
66
|
|
71
|
-
it '
|
67
|
+
it 'loads entire document without # at the end' do
|
68
|
+
pointer = "./basic_definitions.json"
|
69
|
+
obj = SchemaTools::RefResolver.load_json_pointer(pointer)
|
70
|
+
obj["definitions"].keys.length.should eq 3
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'loads the entire document' do
|
72
74
|
pointer = "./basic_definitions.json#"
|
73
75
|
obj = SchemaTools::RefResolver.load_json_pointer(pointer)
|
74
76
|
obj["definitions"].keys.length.should eq 3
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Georg Leciejewski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|