json_schema_tools 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +1 -1
- data/README.md +42 -17
- data/lib/schema_tools.rb +2 -1
- data/lib/schema_tools/klass_factory.rb +33 -22
- data/lib/schema_tools/modules/as_schema.rb +7 -4
- data/lib/schema_tools/modules/attributes.rb +25 -15
- data/lib/schema_tools/modules/read.rb +20 -0
- data/lib/schema_tools/ref_resolver.rb +62 -0
- data/lib/schema_tools/version.rb +1 -1
- data/spec/fixtures/basic_definitions.json +13 -0
- data/spec/fixtures/includes_basic_definitions.json +19 -0
- data/spec/schema_tools/modules/attributes_spec.rb +8 -0
- data/spec/schema_tools/reader_spec.rb +9 -0
- data/spec/schema_tools/ref_resolver_spec.rb +71 -0
- data/spec/spec_helper.rb +1 -0
- metadata +9 -15
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OTJhOGY1MWU1MjhmMjAwMjg2YmNlMzM4MWY0ZjI0YzE1ZDkxYjAxZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NTY2N2ZmNDg4MDc0NzUzYmFmNzYyODg1MjAwMjA1MzcyNjBjMDZmNg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MWI1OGE0MTdlNjM3NDYwMGZjMjc5MThhNjRmMjAwNTJjNWViMGJiNThhMGFh
|
10
|
+
M2ZjM2YxMWYxOTk0Y2ViMTU4YjJkMjE4Y2JmY2M3NjQzMzZjOGRiMTNjODg0
|
11
|
+
MmE3MDM1NzZmNmU0NzEwMjE3Y2MyYjcwZDQ0NDljN2ZiYzE5NDI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YWZlMDgzMDFjZDI1M2IzNDQwNDI5ZTM5MTkzYjg3NzNkMGQwYTExNmZmYzQz
|
14
|
+
ZTdhMDQ4ZmUwMTU2Njg3YTQ4MzI5ZGMwY2VjM2ZhNGVjYzFlNDYyYTA2ZDZk
|
15
|
+
MmE4ZjE3ZDA3ZWM1MThhZDFmOTFmZWI0NmQ2NWI3OTVlNDUzMDc=
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -2,12 +2,18 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/salesking/json_schema_tools.png?branch=master)](https://travis-ci.org/salesking/json_schema_tools)
|
4
4
|
|
5
|
-
|
5
|
+
Toolbox to work with JSON Schema in Ruby:
|
6
6
|
|
7
|
-
*
|
8
|
-
*
|
9
|
-
*
|
10
|
-
|
7
|
+
* blow up classes from schema
|
8
|
+
* object.as_json conversion
|
9
|
+
* object.valid? validations with object.errors
|
10
|
+
|
11
|
+
Simply use full blown classes or customize the bits and pieces:
|
12
|
+
|
13
|
+
* add schema properties to an existing class
|
14
|
+
* add validations to an existing class
|
15
|
+
* clean parameters e.g. in an api controller
|
16
|
+
* customize json output, object namespaces, used schema
|
11
17
|
|
12
18
|
## Usage
|
13
19
|
|
@@ -15,14 +21,33 @@ Hook the gem into your app
|
|
15
21
|
|
16
22
|
gem 'json_schema_tools'
|
17
23
|
|
18
|
-
|
24
|
+
Quickstart assuming you have schema definitions [like those](https://github.com/salesking/sk_api_schema/tree/master/json/v1.0) in place.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
# add schema directory to the search path
|
28
|
+
SchemaTools.schema_path = '/path/to/schema-json-files'
|
29
|
+
# blow up classes for each schema file
|
30
|
+
SchemaTools::KlassFactory.build
|
31
|
+
|
32
|
+
# init with params (assumes a contact.json definition)
|
33
|
+
contact = Contact.new first_name: 'Barry', last_name: 'Cade'
|
34
|
+
# use setters/getters
|
35
|
+
contact.first_name = 'Ahr'
|
36
|
+
# validations derived from property definitions
|
37
|
+
contact.valid?
|
38
|
+
contact.errors.full_messages
|
39
|
+
```
|
40
|
+
|
41
|
+
## Schema handling
|
19
42
|
|
20
43
|
Before the fun begins, with any of the tools, one or multiple JSON schema(files)
|
21
44
|
must be available. A schema is converted into a ruby hash and for convenience is
|
22
|
-
cached into a registry (global or
|
23
|
-
once e.g on program start.
|
45
|
+
cached into a registry (global or per reader object). Globals are initialized
|
46
|
+
once e.g on program start. A reader object allows to handle schemata in dynamic
|
47
|
+
ways.
|
24
48
|
|
25
|
-
|
49
|
+
When using the global SchemaTools reader simply provide a base path to the
|
50
|
+
schema files and you are done.
|
26
51
|
|
27
52
|
```ruby
|
28
53
|
SchemaTools.schema_path = '/path/to/schema-json-files'
|
@@ -69,7 +94,7 @@ reader.registry
|
|
69
94
|
|
70
95
|
## Object to JSON - from Schema
|
71
96
|
|
72
|
-
As you probably know such is done e.g in rails via object.
|
97
|
+
As you probably know such is done e.g in rails via object.as_json. While using
|
73
98
|
this might be simple, it has a damn big drawback: There is no transparent
|
74
99
|
contract about the data-structure, as rails simply uses all fields defined in the
|
75
100
|
database(ActiveRecord model). One side-effect: With each migration you are f***ed
|
@@ -90,10 +115,10 @@ class Client < ActiveRecord::Base
|
|
90
115
|
end
|
91
116
|
|
92
117
|
peter = Client.new name: 'Peter'
|
93
|
-
peter.
|
118
|
+
peter.as_json
|
94
119
|
#=> "client":{"id":12, "name": "Peter", "email":"",..}
|
95
120
|
|
96
|
-
peter.
|
121
|
+
peter.as_hash
|
97
122
|
#=> "client"=>{"id"=>12, "name"=> "Peter", "email"=>"",..}
|
98
123
|
```
|
99
124
|
|
@@ -114,7 +139,7 @@ course they can be combined.
|
|
114
139
|
Only use some fields e.g. to save bandwidth
|
115
140
|
|
116
141
|
```ruby
|
117
|
-
peter.
|
142
|
+
peter.as_json(fields:['id', 'name'])
|
118
143
|
#=> "client":{"id":12, "name": "Peter"}
|
119
144
|
```
|
120
145
|
|
@@ -122,13 +147,13 @@ Use a custom schema name e.g. to represent a client as contact. Assumes you also
|
|
122
147
|
have a schema named contact.json
|
123
148
|
|
124
149
|
```ruby
|
125
|
-
peter.
|
150
|
+
peter.as_json(class_name: 'contact')
|
126
151
|
```
|
127
152
|
|
128
153
|
Set a custom schema path
|
129
154
|
|
130
155
|
```ruby
|
131
|
-
peter.
|
156
|
+
peter.as_json( path: 'path-to/json-files/')
|
132
157
|
```
|
133
158
|
|
134
159
|
By default the object hash has the class name (client) and the link-section on
|
@@ -140,7 +165,7 @@ inline:
|
|
140
165
|
|
141
166
|
```ruby
|
142
167
|
|
143
|
-
peter.
|
168
|
+
peter.as_json( exclude_root: true )
|
144
169
|
|
145
170
|
#=> {"id":12, "name"=> "Peter",
|
146
171
|
# "_class_name":"client",
|
@@ -185,7 +210,7 @@ contact.last_name = 'Rambo'
|
|
185
210
|
# raw access
|
186
211
|
contact.schema_attrs
|
187
212
|
# to json
|
188
|
-
contact.
|
213
|
+
contact.as_json
|
189
214
|
```
|
190
215
|
|
191
216
|
## Classes from Schema - KlassFactory
|
data/lib/schema_tools.rb
CHANGED
@@ -9,6 +9,7 @@ require 'schema_tools/reader'
|
|
9
9
|
require 'schema_tools/cleaner'
|
10
10
|
require 'schema_tools/hash'
|
11
11
|
require 'schema_tools/klass_factory'
|
12
|
+
require 'schema_tools/ref_resolver'
|
12
13
|
|
13
14
|
|
14
15
|
module SchemaTools
|
@@ -22,4 +23,4 @@ module SchemaTools
|
|
22
23
|
@schema_path
|
23
24
|
end
|
24
25
|
end
|
25
|
-
end
|
26
|
+
end
|
@@ -32,34 +32,45 @@ module SchemaTools
|
|
32
32
|
reader = opts[:reader] || SchemaTools::Reader
|
33
33
|
schemata = reader.read_all( opts[:path] || SchemaTools.schema_path )
|
34
34
|
namespace = opts[:namespace] || Object
|
35
|
-
if namespace.is_a?(String) || namespace.is_a?(Symbol)
|
36
|
-
namespace = "#{namespace}".constantize
|
37
|
-
end
|
35
|
+
namespace = "#{namespace}".constantize if namespace.is_a?(String) || namespace.is_a?(Symbol)
|
38
36
|
# bake classes
|
39
37
|
schemata.each do |schema|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
include SchemaTools::Modules::Attributes
|
45
|
-
include ActiveModel::Conversion
|
46
|
-
include SchemaTools::Modules::Validations # +naming + transl + conversion
|
47
|
-
has_schema_attrs schema['name'], reader: reader
|
48
|
-
validate_with schema['name'], reader:reader
|
49
|
-
getter_names = schema['properties'].select{|name,prop| !prop['readonly'] }.keys.map { |name| name.to_sym}
|
50
|
-
attr_accessor *getter_names
|
38
|
+
next if !schema['name'] or namespace.const_defined?(schema['name'].classify, false)
|
39
|
+
build_class(schema, namespace, reader)
|
40
|
+
end
|
41
|
+
end
|
51
42
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
43
|
+
# @param [Object] schema single json schema
|
44
|
+
# @param [Constant] namespace for the new class
|
45
|
+
# @param [SchemaTools::Reader] reader set into new class for validation and attributes
|
46
|
+
def build_class(schema, namespace, reader)
|
47
|
+
klass_name = schema['name'].classify
|
48
|
+
klass = namespace.const_set(klass_name, Class.new)
|
49
|
+
klass.class_eval do
|
50
|
+
include SchemaTools::Modules::Attributes
|
51
|
+
include ActiveModel::Conversion
|
52
|
+
include SchemaTools::Modules::Validations # +naming + transl + conversion
|
53
|
+
has_schema_attrs schema['name'], reader: reader
|
54
|
+
validate_with schema['name'], reader:reader
|
55
|
+
getter_names = schema['properties'].select{|name,prop| !prop['readonly'] }
|
56
|
+
.keys.map { |name| name.to_sym}
|
57
|
+
attr_accessor *getter_names
|
57
58
|
|
58
|
-
|
59
|
+
def initialize(attributes = {})
|
60
|
+
attributes.each do |name, value|
|
61
|
+
send("#{name}=", value)
|
62
|
+
end
|
59
63
|
end
|
60
|
-
|
64
|
+
|
65
|
+
def persisted?; false end
|
66
|
+
end # class
|
67
|
+
end
|
68
|
+
|
69
|
+
# @param [Object] name
|
70
|
+
def namespace(name)
|
71
|
+
|
61
72
|
end
|
62
73
|
|
63
74
|
end
|
64
75
|
end
|
65
|
-
end
|
76
|
+
end
|
@@ -5,16 +5,17 @@ module SchemaTools
|
|
5
5
|
module AsSchema
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
|
-
# convert
|
8
|
+
# convert object to a schema markup.
|
9
9
|
# @param [Hash{Symbol=>Mixed}] opts passed on to #SchemaTools::Hash.from_schema
|
10
10
|
# @return [String] json
|
11
11
|
def as_schema_json(opts={})
|
12
12
|
ActiveSupport::JSON.encode(as_schema_hash(opts))
|
13
13
|
end
|
14
|
+
def as_json(opts={}) as_schema_json(opts); end
|
14
15
|
|
15
|
-
#
|
16
|
-
# The schema is derived from
|
17
|
-
# *
|
16
|
+
# The object as hash with fields detected from its schema.
|
17
|
+
# The schema name is derived from:
|
18
|
+
# * options[:class_name]
|
18
19
|
# * has_schema_attrs definition(if used)
|
19
20
|
# * the underscored class name
|
20
21
|
# @param [Hash{Symbol=>Mixed}] opts passed on to #SchemaTools::Hash.from_schema
|
@@ -26,9 +27,11 @@ module SchemaTools
|
|
26
27
|
end
|
27
28
|
SchemaTools::Hash.from_schema(self, opts)
|
28
29
|
end
|
30
|
+
def as_hash(opts={}) as_schema_hash(opts); end
|
29
31
|
|
30
32
|
module ClassMethods
|
31
33
|
# Get or set the schema name used
|
34
|
+
# @param [Symbol|String] name
|
32
35
|
def schema_name(name=nil)
|
33
36
|
@schema_name = name if name
|
34
37
|
@schema_name
|
@@ -3,6 +3,15 @@ module SchemaTools
|
|
3
3
|
module Modules
|
4
4
|
# Add schema properties to a class by using has_schema_attrs to define from
|
5
5
|
# which schema to inherit attributes.
|
6
|
+
# @example
|
7
|
+
#
|
8
|
+
# class Contact
|
9
|
+
# has_schema_attrs :contact
|
10
|
+
# end
|
11
|
+
# Contact.schema_name #=> contact
|
12
|
+
# Contact.as_json
|
13
|
+
# Contact.as_hash
|
14
|
+
# Contact.schema #=> json schema hash
|
6
15
|
module Attributes
|
7
16
|
extend ActiveSupport::Concern
|
8
17
|
include SchemaTools::Modules::AsSchema
|
@@ -11,6 +20,10 @@ module SchemaTools
|
|
11
20
|
@schema_attrs ||= {}
|
12
21
|
end
|
13
22
|
|
23
|
+
def schema
|
24
|
+
self.class.schema
|
25
|
+
end
|
26
|
+
|
14
27
|
module ClassMethods
|
15
28
|
|
16
29
|
# @param [Symbol|String] schema name
|
@@ -20,27 +33,24 @@ module SchemaTools
|
|
20
33
|
def has_schema_attrs(schema_name, opts={})
|
21
34
|
reader = opts[:reader] || SchemaTools::Reader
|
22
35
|
schema_location = opts[:path] || opts[:schema]
|
23
|
-
schema
|
24
|
-
|
36
|
+
# remember schema + name on class level
|
37
|
+
self.schema( reader.read(schema_name, schema_location) )
|
25
38
|
self.schema_name(schema_name)
|
26
39
|
# make getter / setter
|
27
|
-
schema[:properties].each do |key,
|
28
|
-
|
29
|
-
define_method key
|
30
|
-
schema_attrs[key]
|
31
|
-
end
|
32
|
-
#setter
|
33
|
-
unless val[:readonly]
|
34
|
-
define_method "#{key}=" do |value|
|
35
|
-
#TODO validations?
|
36
|
-
schema_attrs[key] = value
|
37
|
-
end
|
38
|
-
end
|
40
|
+
self.schema[:properties].each do |key, prop|
|
41
|
+
define_method(key) { schema_attrs[key] }
|
42
|
+
define_method("#{key}=") { |value| schema_attrs[key] = value } unless prop[:readonly]
|
39
43
|
end
|
44
|
+
#TODO parse links ?? or do it in resource module
|
40
45
|
end
|
41
46
|
|
42
|
-
|
47
|
+
# @param [Hash] schema_hash
|
48
|
+
def schema(schema_hash=nil)
|
49
|
+
@schema = schema_hash if schema_hash
|
50
|
+
@schema
|
51
|
+
end
|
43
52
|
|
53
|
+
end
|
44
54
|
end
|
45
55
|
end
|
46
56
|
end
|
@@ -57,6 +57,7 @@ module SchemaTools
|
|
57
57
|
schema[:properties] = ext[:properties].merge(schema[:properties])
|
58
58
|
end
|
59
59
|
end
|
60
|
+
_handle_reference_properties schema
|
60
61
|
registry[ schema_name ] = schema
|
61
62
|
end
|
62
63
|
|
@@ -74,6 +75,25 @@ module SchemaTools
|
|
74
75
|
end
|
75
76
|
schemas
|
76
77
|
end
|
78
|
+
|
79
|
+
# Merge referenced property definitions into the given schema.
|
80
|
+
# e.g. each object has an updated_at field which we define in a single
|
81
|
+
# location(external file) instead of repeating the property def in each
|
82
|
+
# schema.
|
83
|
+
# @param [HashWithIndifferentAccess] schema - single schema
|
84
|
+
def _handle_reference_properties(schema)
|
85
|
+
return unless schema["properties"]
|
86
|
+
schema["properties"].each { |key, value|
|
87
|
+
next unless value["$ref"]
|
88
|
+
|
89
|
+
json_pointer = value["$ref"]
|
90
|
+
values_from_pointer = RefResolver.load_json_pointer json_pointer
|
91
|
+
schema["properties"][key].merge!(values_from_pointer) {|key, old, new|
|
92
|
+
old
|
93
|
+
}
|
94
|
+
schema["properties"][key].delete("$ref")
|
95
|
+
}
|
96
|
+
end
|
77
97
|
end
|
78
98
|
end
|
79
99
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module SchemaTools
|
6
|
+
class RefResolver
|
7
|
+
|
8
|
+
#
|
9
|
+
# super basic resolving of JSON Pointer stuff in order
|
10
|
+
# to be able to load $ref parameter.
|
11
|
+
#
|
12
|
+
# $refs in JSON Schema are defined in JSON Pointer syntax:
|
13
|
+
# http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07
|
14
|
+
#
|
15
|
+
# @param [String] json_pointer the JSON Pointer expression to evaluate
|
16
|
+
# @param [Hash] schema if the pointer refers to a local schema, this is this
|
17
|
+
# the hash to evaluate it against. If the pointer contains a uri to a
|
18
|
+
# referenced schema, an attempt is made to load
|
19
|
+
def self.load_json_pointer json_pointer, schema = {}
|
20
|
+
# JSON Pointer is a big, abandoned WIP and we're going to
|
21
|
+
# start by only implementing the part's we need ...
|
22
|
+
if nil == (json_pointer =~ /^(.*)#(.*)/ )
|
23
|
+
raise "invalid json pointer: #{json_pointer}"
|
24
|
+
end
|
25
|
+
|
26
|
+
uri = $1.strip
|
27
|
+
pointer = $2
|
28
|
+
|
29
|
+
if ! uri.empty?
|
30
|
+
uri = URI.parse(uri)
|
31
|
+
raise "must currently be a relative uri: #{json_pointer}" if uri.absolute?
|
32
|
+
path = SchemaTools.schema_path + "/" + uri.path
|
33
|
+
open (path) {|f|
|
34
|
+
schema = JSON.parse(f.read)
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
return self._retrieve_pointer_from_object pointer, schema
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def self._retrieve_pointer_from_object pointer, object
|
43
|
+
# assume path to be the JSONPointer expression:
|
44
|
+
# json/pointer/expression
|
45
|
+
# and obj to be the ruby hash representation of the json
|
46
|
+
path = pointer.is_a?(Array) ? pointer : pointer.split("/")
|
47
|
+
|
48
|
+
while object != nil && component = path.shift
|
49
|
+
prev = object
|
50
|
+
component = component.to_i if object.is_a?(Array) && component =~ /^\d+$/
|
51
|
+
object = object[component]
|
52
|
+
end
|
53
|
+
|
54
|
+
return object
|
55
|
+
end
|
56
|
+
|
57
|
+
end # module
|
58
|
+
end # module
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
data/lib/schema_tools/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"type":"object",
|
3
|
+
"title":"test_object",
|
4
|
+
"name":"includes_basic_definitions",
|
5
|
+
"description":"test_object",
|
6
|
+
"properties": {
|
7
|
+
"id":{
|
8
|
+
"$ref" : "./basic_definitions.json#definitions/id"
|
9
|
+
},
|
10
|
+
"id_adding_param": {
|
11
|
+
"$ref" : "./basic_definitions.json#definitions/id",
|
12
|
+
"maxLength" : 10
|
13
|
+
},
|
14
|
+
"id_overriding_param" : {
|
15
|
+
"$ref" : "./basic_definitions.json#definitions/id",
|
16
|
+
"description" : "some other description"
|
17
|
+
}
|
18
|
+
}
|
19
|
+
}
|
@@ -31,6 +31,14 @@ describe SchemaTools::Modules::Attributes do
|
|
31
31
|
it 'should add schema_name to class' do
|
32
32
|
subject.class.schema_name.should == :client
|
33
33
|
end
|
34
|
+
|
35
|
+
it 'should add schema to class' do
|
36
|
+
subject.class.schema.should == SchemaTools::Reader.read(:client)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should add schema to object' do
|
40
|
+
subject.schema.should == SchemaTools::Reader.read(:client)
|
41
|
+
end
|
34
42
|
end
|
35
43
|
|
36
44
|
context 'attributes from dynamic schema' do
|
@@ -33,6 +33,15 @@ describe SchemaTools::Reader do
|
|
33
33
|
schema[:properties][:numbers].should_not be_empty
|
34
34
|
end
|
35
35
|
|
36
|
+
it 'should deal with referenced parameters properly' do
|
37
|
+
schema = SchemaTools::Reader.read(:includes_basic_definitions)
|
38
|
+
schema[:properties].should_not be_empty
|
39
|
+
schema[:properties].length.should eq 3
|
40
|
+
schema[:properties][:id][:description].should eq "some description"
|
41
|
+
|
42
|
+
schema[:properties][:id]["$ref"].should be_nil
|
43
|
+
end
|
44
|
+
|
36
45
|
it 'should enforce correct parameter usage' do
|
37
46
|
expect { SchemaTools::Reader.read(:contact, []) }.to raise_error ArgumentError
|
38
47
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SchemaTools::RefResolver do
|
4
|
+
context 'class methods' do
|
5
|
+
it 'should handle simple json pointer' do
|
6
|
+
obj = { "bla" => {
|
7
|
+
"blub" => :success
|
8
|
+
}}
|
9
|
+
|
10
|
+
pointer = "bla/blub"
|
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
|
+
found.should eq :success
|
17
|
+
|
18
|
+
found = SchemaTools::RefResolver._retrieve_pointer_from_object "non/existant/path", obj
|
19
|
+
found.should be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should handle json pointer arrays' do
|
23
|
+
obj = { "bla" => {
|
24
|
+
"blub" => :success,
|
25
|
+
"bling" => [3,2,1]
|
26
|
+
}}
|
27
|
+
pointer = "bla/bling/2"
|
28
|
+
found = SchemaTools::RefResolver._retrieve_pointer_from_object pointer, obj
|
29
|
+
found.should eq 1
|
30
|
+
|
31
|
+
pointer = "bla/bling/3"
|
32
|
+
found = SchemaTools::RefResolver._retrieve_pointer_from_object pointer, obj
|
33
|
+
found.should be_nil
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should handle embedded json pointer arrays' do
|
38
|
+
obj = { "bla" => {
|
39
|
+
"blub" => :success,
|
40
|
+
"bling" => [
|
41
|
+
{"num" => 3},
|
42
|
+
{"num" => 2},
|
43
|
+
{"num" => 1}
|
44
|
+
]
|
45
|
+
}}
|
46
|
+
pointer = "bla/bling/1/num"
|
47
|
+
found = SchemaTools::RefResolver._retrieve_pointer_from_object pointer, obj
|
48
|
+
found.should eq 2
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should throw an exception on an invalid path' do
|
53
|
+
obj = {}
|
54
|
+
expect {
|
55
|
+
SchemaTools::RefResolver.load_json_pointer("bla")
|
56
|
+
}.to raise_error
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should reject absolute URI part' do
|
60
|
+
obj = {}
|
61
|
+
expect {
|
62
|
+
SchemaTools::RefResolver.load_json_pointer("http://www.example.com/#some/stuff")
|
63
|
+
}.to raise_error
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should load local ref' do
|
67
|
+
pointer = "./basic_definitions.json#definitions"
|
68
|
+
obj = SchemaTools::RefResolver.load_json_pointer(pointer)
|
69
|
+
obj.length.should eq 2
|
70
|
+
end
|
71
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -16,6 +16,7 @@ require 'test_helpers'
|
|
16
16
|
RSpec.configure do |config|
|
17
17
|
end
|
18
18
|
|
19
|
+
I18n.enforce_available_locales = false if I18n.respond_to?('enforce_available_locales=')
|
19
20
|
# set global json schema path for examples
|
20
21
|
SchemaTools.schema_path = File.expand_path('../fixtures', __FILE__)
|
21
22
|
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.4
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Georg Leciejewski
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-08-21 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: json
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ! '>='
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ! '>='
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ! '>='
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: simplecov
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ! '>='
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ! '>='
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -62,7 +55,6 @@ dependencies:
|
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: rspec
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
59
|
- - ! '>='
|
68
60
|
- !ruby/object:Gem::Version
|
@@ -70,7 +62,6 @@ dependencies:
|
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
66
|
- - ! '>='
|
76
67
|
- !ruby/object:Gem::Version
|
@@ -102,10 +93,13 @@ files:
|
|
102
93
|
- lib/schema_tools/modules/read.rb
|
103
94
|
- lib/schema_tools/modules/validations.rb
|
104
95
|
- lib/schema_tools/reader.rb
|
96
|
+
- lib/schema_tools/ref_resolver.rb
|
105
97
|
- lib/schema_tools/version.rb
|
106
98
|
- spec/fixtures/address.json
|
99
|
+
- spec/fixtures/basic_definitions.json
|
107
100
|
- spec/fixtures/client.json
|
108
101
|
- spec/fixtures/contact.json
|
102
|
+
- spec/fixtures/includes_basic_definitions.json
|
109
103
|
- spec/fixtures/lead.json
|
110
104
|
- spec/fixtures/page.json
|
111
105
|
- spec/schema_tools/cleaner_spec.rb
|
@@ -114,30 +108,30 @@ files:
|
|
114
108
|
- spec/schema_tools/modules/as_schema_spec.rb
|
115
109
|
- spec/schema_tools/modules/attributes_spec.rb
|
116
110
|
- spec/schema_tools/reader_spec.rb
|
111
|
+
- spec/schema_tools/ref_resolver_spec.rb
|
117
112
|
- spec/spec_helper.rb
|
118
113
|
- spec/test_helpers.rb
|
119
114
|
homepage: https://github.com/salesking/json_schema_tools
|
120
115
|
licenses: []
|
116
|
+
metadata: {}
|
121
117
|
post_install_message:
|
122
118
|
rdoc_options: []
|
123
119
|
require_paths:
|
124
120
|
- lib
|
125
121
|
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
-
none: false
|
127
122
|
requirements:
|
128
123
|
- - ! '>='
|
129
124
|
- !ruby/object:Gem::Version
|
130
125
|
version: '0'
|
131
126
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
-
none: false
|
133
127
|
requirements:
|
134
128
|
- - ! '>='
|
135
129
|
- !ruby/object:Gem::Version
|
136
130
|
version: '0'
|
137
131
|
requirements: []
|
138
132
|
rubyforge_project:
|
139
|
-
rubygems_version:
|
133
|
+
rubygems_version: 2.2.2
|
140
134
|
signing_key:
|
141
|
-
specification_version:
|
135
|
+
specification_version: 4
|
142
136
|
summary: JSON Schema API tools for server and client side
|
143
137
|
test_files: []
|