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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZWY5YzJhODNmZTc5NWFhNmI0NTM3N2I2Mzg3MzcwNGI0MDkxMTQzNg==
4
+ NDI3MjBjMGQ2NjA2YjJkN2Y1ZjFiN2NjZTM2NTMzMTJlYmEwNzE5Mg==
5
5
  data.tar.gz: !binary |-
6
- Mzk5MmQxZGMyM2E2MDQ3MTU2MzE0ZWY3NmU1ODVmN2JjMDlhNGMzMQ==
6
+ YTk0Mjg5NTU4MjlkNzA5YzJlYzExNDAyNWVlNDI0MDM5ZmQ1YzI3Nw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YmYzNWEzYzI2ZjQ5ZWFmYzNhM2I5ZjA2MTk1MTZhYmJlNzlmMmU2Njk1Yzc3
10
- NGVlYzFkYjJhMzc2NWQxZmVhY2E2NTgyMDUwYmU4ODljNTExOGQ5YjJiMTI0
11
- MWQ3NGJmZTZmNTY1NzFmMTMzZWJmOWNkY2NkODZkMTlmYjQ3ZmQ=
9
+ YWZlOGQyODY4MTg0ZDBkMGYzZGFhMDlmZWMyOTE0ZTA1OTY2YTk4NWUxNTA1
10
+ OTIzYWYxYWEwYzJkMjI5YjY0NmM2MjIzNzczNzViMGZiODgzMTcyZTIwNTNi
11
+ OWM3YTdmNWRkMTRkYzBkYWJlZjMyNzJhOGY1YTA4ZjJiNmMwOTI=
12
12
  data.tar.gz: !binary |-
13
- MTEwM2Q4MTU5ZWE0N2MxN2VhNTY4YWNlYTZkODdkYzZmNGZkMzZlOTk3MmM2
14
- ZmM3ODE4ZjE4ZTU4ODhlNTdmZWQxMzYxYTJhOWRlODg3ZmM4NzVhZDk5ZWVh
15
- NDI1ZDY5N2I1MmNkNjBiYzQ0MzdiYTY1ODQxYThhOWM5ZmRkZTQ=
13
+ ZDJiZGQ2NWQ5YmIxN2FjMTVmMTVhOGQxYWNhOWU5NDM5NDkxYjY2OWE3ZjJl
14
+ YTNkMmQ2ODFjNzI2NGU4M2U0YmIyODgzNTBlZGU4MDZlMDJmY2RhZWE3MzJk
15
+ MGFmZGVlNDlkMmQ1ZjA1YmEzYTAwOTAzMzYxZDdiZjgzY2NlNzI=
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog JSON Schema Tools
2
2
 
3
+ ##2015-03
4
+
5
+ * allow $refs to include plain schema file name
6
+
3
7
  ##2015-01
4
8
 
5
9
  * add :reader option for Obj.as_schema_hash to use a custom reader instance(with its own schema registry), instead of the global one
@@ -6,72 +6,83 @@ require 'uri'
6
6
  module SchemaTools
7
7
  class RefResolver
8
8
 
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
- #
16
- # @param [String] json_pointer the JSON Pointer expression to evaluate
17
- # @param [Schema] relative_to if the pointer refers to a local schema, this is this
18
- # the hash to evaluate it against. If the pointer contains a uri to a
19
- # referenced schema, an attempt is made to load
20
- def self.load_json_pointer json_pointer, relative_to = nil
21
- # JSON Pointer is a big, abandoned WIP and we're going to
22
- # start by only implementing the part's we need ...
23
- if nil == (json_pointer =~ /^(.*)#(.*)/ )
24
- raise "invalid json pointer: #{json_pointer}"
25
- end
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
- uri = $1.strip
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 locale tools instance or base path from global SchemaTools.schema_path
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 = File.join(base_dir, uri.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
- def self._retrieve_pointer_from_object pointer, object
58
- # assume path to be the JSONPointer expression:
59
- # json/pointer/expression
60
- # and obj to be the ruby hash representation of the json
61
- path = pointer.is_a?(Array) ? pointer : pointer.split("/")
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
- while object != nil && component = path.shift
64
- prev = object
65
- component = component.to_i if object.is_a?(Array) && component =~ /^\d+$/
66
- object = object[component]
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
@@ -1,3 +1,3 @@
1
1
  module SchemaTools
2
- VERSION = '0.6.0'
2
+ VERSION = '0.6.1'
3
3
  end
@@ -6,5 +6,8 @@
6
6
  },
7
7
  "two": {
8
8
  "$ref":"./address.json#properties"
9
+ },
10
+ "three": {
11
+ "$ref":"./address.json"
9
12
  }
10
13
  }
@@ -73,7 +73,7 @@
73
73
  "work_address":{
74
74
  "description": "The work address as an example for a single nested/related object",
75
75
  "type":"object",
76
- "properties" : {"$ref":"./address.json#properties"}
76
+ "$ref":"./address.json"
77
77
  }
78
78
  },
79
79
  "links":[
@@ -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 'should load local ref' do
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 'should load the entire document' do
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.0
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-02-26 00:00:00.000000000 Z
11
+ date: 2015-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json