json_schema_tools 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/schema_tools/modules/read.rb +16 -5
- data/lib/schema_tools/version.rb +1 -1
- data/spec/fixtures/nested_schemas/person.json +23 -0
- data/spec/fixtures/one_of_definition.json +15 -0
- data/spec/schema_tools/hash_spec.rb +26 -10
- data/spec/schema_tools/reader_spec.rb +27 -8
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MDAyZTViOWEwMjk1YzMwNDQwZGYzYjBlODhiZDI5NzQ5OGRkOGY4YQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MTAyYmE1MDAwYTFlZGU2MjcwZDkxZDczODQxZjU3YzM3MWIzMGM3Mg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NTRlNTI2OGVkYWExZTBkODRiNDVlZDNjMjdmMGFkNTM2MDQ3NTljYjNiM2Uw
|
10
|
+
ZDFhNjc2Yzk3OWY4NjIzODg1ZTMwOWU2ZTlkNzZiYWRlMDg2NjAyZmM0YjE2
|
11
|
+
NzZhOTYwNGRhZjI2NWI0MWFiNTkyNTA2ZjE1ODUzMTBjZDVhODI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZGUzOTcxZjJiMTE0MjEwOWJjMTgzNDRlZDNkNWQyYjYxODE0OWQwYWVlNTRm
|
14
|
+
MDIzOGIyOGNkZjQwOGYwM2QxNWQ2ZjkxYjE1Y2MzZGNhMzk3MDBmOWRlYmQ5
|
15
|
+
ODU0MzgzYmE5ZTEwYjRmNTI1YmY0NzlkZWZjY2FmYzNlZTg0ZTA=
|
@@ -31,7 +31,7 @@ module SchemaTools
|
|
31
31
|
# @param [String|Symbol] schema name to be read from schema path directory
|
32
32
|
# @param [String|Hash] either the path to retrieve schema_name from,
|
33
33
|
# or a Schema in Ruby hash form
|
34
|
-
# @return[HashWithIndifferentAccess] schema as hash
|
34
|
+
# @return[HashWithIndifferentAccess|Nil] schema as hash, nil if schema is not an object
|
35
35
|
def read(schema_name, path_or_schema=nil)
|
36
36
|
schema_name = schema_name.to_sym
|
37
37
|
return registry[schema_name] if registry[schema_name]
|
@@ -55,6 +55,8 @@ module SchemaTools
|
|
55
55
|
plain_data ||= File.open(file_path, 'r'){|f| f.read}
|
56
56
|
|
57
57
|
schema = ActiveSupport::JSON.decode(plain_data).with_indifferent_access
|
58
|
+
# only import object definitions, shared property definitions are handled separate
|
59
|
+
return unless schema[:type] == 'object'
|
58
60
|
if schema[:extends]
|
59
61
|
extends = schema[:extends].is_a?(Array) ? schema[:extends] : [ schema[:extends] ]
|
60
62
|
extends.each do |ext_name|
|
@@ -67,18 +69,27 @@ module SchemaTools
|
|
67
69
|
registry[ schema_name ] = schema
|
68
70
|
end
|
69
71
|
|
70
|
-
# Read all available schemas from a given path(folder) and
|
71
|
-
# them as array
|
72
|
+
# Read all available schemas from a given path(folder +subfolders) and
|
73
|
+
# return the found object definitions them as array. Also populates the
|
74
|
+
# registry
|
72
75
|
#
|
73
76
|
# @param [String] path to schema files
|
74
77
|
# @return [Array<HashWithIndifferentAccess>] array of schemas as hash
|
75
78
|
def read_all(path=nil)
|
76
79
|
schemas = []
|
77
|
-
|
78
|
-
|
80
|
+
file_paths = if path
|
81
|
+
[File.join(path, '*.json')]
|
82
|
+
else
|
83
|
+
[ File.join( SchemaTools.schema_path, '*.json'),
|
84
|
+
File.join( SchemaTools.schema_path, '**/*', '*.json')
|
85
|
+
]
|
86
|
+
end
|
87
|
+
|
88
|
+
Dir.glob( file_paths ).each do |file|
|
79
89
|
schema_name = File.basename(file, '.json').to_sym
|
80
90
|
schemas << read(schema_name, path)
|
81
91
|
end
|
92
|
+
schemas.compact!
|
82
93
|
schemas
|
83
94
|
end
|
84
95
|
|
data/lib/schema_tools/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
{ "type":"object",
|
2
|
+
"title": "Person",
|
3
|
+
"name": "person",
|
4
|
+
"description": "A simple person, nested in subfolder",
|
5
|
+
"properties":{
|
6
|
+
"id":{
|
7
|
+
"description":"Unique identifier ",
|
8
|
+
"identity":true,
|
9
|
+
"readonly":true,
|
10
|
+
"type":"number"
|
11
|
+
},
|
12
|
+
"last_name":{
|
13
|
+
"description": "Last name of a person. ",
|
14
|
+
"type":"string",
|
15
|
+
"maxLength": 50
|
16
|
+
},
|
17
|
+
"first_name":{
|
18
|
+
"description": "First name of a person.",
|
19
|
+
"type":"string",
|
20
|
+
"maxLength": 50
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
{
|
2
|
+
"type":"object",
|
3
|
+
"title":"test_oneOf_object",
|
4
|
+
"name":"one_of_definition",
|
5
|
+
"description":"test_object parsing of nested oneOf object definition",
|
6
|
+
"properties": {
|
7
|
+
"person": {
|
8
|
+
"type" : "object",
|
9
|
+
"oneOf" : [
|
10
|
+
{ "$ref" : "./client.json.json#properties"},
|
11
|
+
{ "$ref" : "./contact.json.json#properties"}
|
12
|
+
]
|
13
|
+
}
|
14
|
+
}
|
15
|
+
}
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
5
5
|
# json schema is derived from it
|
6
6
|
################################################################################
|
7
7
|
class Client
|
8
|
-
attr_accessor :first_name, :id, :addresses,
|
8
|
+
attr_accessor :first_name, :id, :addresses, :work_address
|
9
9
|
end
|
10
10
|
class Address
|
11
11
|
attr_accessor :city, :zip
|
@@ -13,6 +13,10 @@ end
|
|
13
13
|
|
14
14
|
class Contact
|
15
15
|
attr_accessor :first_name, :last_name, :addresses, :id
|
16
|
+
end
|
17
|
+
|
18
|
+
class OneOfDefinition
|
19
|
+
attr_accessor :person
|
16
20
|
end
|
17
21
|
|
18
22
|
# see fixtures/lead.json
|
@@ -69,12 +73,12 @@ describe SchemaTools::Hash do
|
|
69
73
|
hash['_class_name'].should == 'contact'
|
70
74
|
end
|
71
75
|
|
72
|
-
it '
|
76
|
+
it 'has _links on object if exclude root' do
|
73
77
|
hash = SchemaTools::Hash.from_schema(contact, exclude_root: true, class_name: :client)
|
74
78
|
hash['_links'].length.should == 7
|
75
79
|
end
|
76
80
|
|
77
|
-
it '
|
81
|
+
it 'has _class_name on object if exclude root' do
|
78
82
|
hash = SchemaTools::Hash.from_schema(contact, exclude_root: true, class_name: :client)
|
79
83
|
hash['_class_name'].should == 'client'
|
80
84
|
end
|
@@ -84,17 +88,17 @@ describe SchemaTools::Hash do
|
|
84
88
|
|
85
89
|
let(:client){Client.new}
|
86
90
|
|
87
|
-
it '
|
91
|
+
it 'has an empty array if values are missing' do
|
88
92
|
hash = SchemaTools::Hash.from_schema(client)
|
89
93
|
hash['client']['addresses'].should == []
|
90
94
|
end
|
91
95
|
|
92
|
-
it '
|
96
|
+
it 'has nil if nested object is missing' do
|
93
97
|
hash = SchemaTools::Hash.from_schema(client)
|
94
98
|
hash['client']['work_address'].should be_nil
|
95
99
|
end
|
96
100
|
|
97
|
-
it '
|
101
|
+
it 'has nested array values' do
|
98
102
|
a1 = Address.new
|
99
103
|
a1.city = 'Cologne'
|
100
104
|
a1.zip = 50733
|
@@ -103,7 +107,7 @@ describe SchemaTools::Hash do
|
|
103
107
|
hash['client']['addresses'].should == [{"address"=>{"city"=>"Cologne", "zip"=>50733}}]
|
104
108
|
end
|
105
109
|
|
106
|
-
it '
|
110
|
+
it 'has nested array values without root' do
|
107
111
|
a1 = Address.new
|
108
112
|
a1.city = 'Cologne'
|
109
113
|
a1.zip = 50733
|
@@ -112,7 +116,7 @@ describe SchemaTools::Hash do
|
|
112
116
|
hash['addresses'].should == [{"city"=>"Cologne", "zip"=>50733, "_class_name"=>"address"}]
|
113
117
|
end
|
114
118
|
|
115
|
-
it '
|
119
|
+
it 'has nested object value' do
|
116
120
|
a1 = Address.new
|
117
121
|
a1.city = 'Cologne'
|
118
122
|
a1.zip = 50733
|
@@ -121,7 +125,7 @@ describe SchemaTools::Hash do
|
|
121
125
|
hash['client']['work_address'].should == {"address"=>{"city"=>"Cologne", "zip"=>50733}}
|
122
126
|
end
|
123
127
|
|
124
|
-
it '
|
128
|
+
it 'has nested object value without root' do
|
125
129
|
a1 = Address.new
|
126
130
|
a1.city = 'Cologne'
|
127
131
|
a1.zip = 50733
|
@@ -130,6 +134,18 @@ describe SchemaTools::Hash do
|
|
130
134
|
hash['work_address'].should == {"city"=>"Cologne", "zip"=>50733, "_class_name"=>"address"}
|
131
135
|
end
|
132
136
|
|
137
|
+
it 'has nested oneOf type object ' do
|
138
|
+
contact = Contact.new
|
139
|
+
contact.first_name = 'Pit'
|
140
|
+
|
141
|
+
i = OneOfDefinition.new
|
142
|
+
i.person = contact
|
143
|
+
|
144
|
+
hash = SchemaTools::Hash.from_schema(i, exclude_root: true)
|
145
|
+
hash['person']['first_name'].should == 'Pit'
|
146
|
+
hash['person']['_class_name'].should == 'contact'
|
147
|
+
end
|
148
|
+
|
133
149
|
end
|
134
150
|
|
135
151
|
context 'with plain nested values' do
|
@@ -168,7 +184,7 @@ describe SchemaTools::Hash do
|
|
168
184
|
SchemaTools::Reader.registry_reset
|
169
185
|
end
|
170
186
|
|
171
|
-
it '
|
187
|
+
it 'has links' do
|
172
188
|
hash = SchemaTools::Hash.from_schema(client)
|
173
189
|
hash['links'].length.should == 7
|
174
190
|
end
|
@@ -2,20 +2,34 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe SchemaTools::Reader do
|
4
4
|
|
5
|
-
context '
|
5
|
+
context '.read_all' do
|
6
|
+
after :each do
|
7
|
+
SchemaTools::Reader.registry_reset
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'reads all schemas' do
|
11
|
+
schemas = SchemaTools::Reader.read_all
|
12
|
+
schemas.length.should == SchemaTools::Reader.registry.length
|
13
|
+
person_schema = schemas.detect{|i| i['name'] = 'person'}
|
14
|
+
person_schema.should be
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
context '.read' do
|
6
20
|
|
7
21
|
after :each do
|
8
22
|
SchemaTools::Reader.registry_reset
|
9
23
|
end
|
10
24
|
|
11
|
-
it '
|
25
|
+
it 'reads a single schema' do
|
12
26
|
schema = SchemaTools::Reader.read(:page)
|
13
27
|
schema[:name].should == 'page'
|
14
28
|
schema[:properties].should_not be_empty
|
15
29
|
SchemaTools::Reader.registry.should_not be_empty
|
16
30
|
end
|
17
31
|
|
18
|
-
it '
|
32
|
+
it 'reads a schema with inheritance' do
|
19
33
|
schema = SchemaTools::Reader.read(:lead) # extends contact
|
20
34
|
|
21
35
|
SchemaTools::Reader.registry[:contact].should_not be_empty
|
@@ -25,7 +39,7 @@ describe SchemaTools::Reader do
|
|
25
39
|
schema[:properties][:lead_source].should_not be_empty
|
26
40
|
end
|
27
41
|
|
28
|
-
it '
|
42
|
+
it 'reads a schema from a Ruby Hash' do
|
29
43
|
schema = SchemaTools::Reader.read(:numbers, schema_as_ruby_object)
|
30
44
|
|
31
45
|
SchemaTools::Reader.registry[:numbers].should_not be_empty
|
@@ -33,7 +47,7 @@ describe SchemaTools::Reader do
|
|
33
47
|
schema[:properties][:numbers].should_not be_empty
|
34
48
|
end
|
35
49
|
|
36
|
-
it '
|
50
|
+
it 'deals with referenced parameters properly' do
|
37
51
|
schema = SchemaTools::Reader.read(:includes_basic_definitions)
|
38
52
|
schema[:properties].should_not be_empty
|
39
53
|
schema[:properties].length.should eq 3
|
@@ -42,23 +56,28 @@ describe SchemaTools::Reader do
|
|
42
56
|
schema[:properties][:id]["$ref"].should be_nil
|
43
57
|
end
|
44
58
|
|
45
|
-
it '
|
59
|
+
it 'enforces correct parameter usage' do
|
46
60
|
expect { SchemaTools::Reader.read(:contact, []) }.to raise_error ArgumentError
|
47
61
|
end
|
62
|
+
|
63
|
+
it 'reads from sub folder' do
|
64
|
+
schema = SchemaTools::Reader.read(:person)
|
65
|
+
schema[:name].should == 'person'
|
66
|
+
end
|
48
67
|
end
|
49
68
|
|
50
69
|
context 'instance methods' do
|
51
70
|
|
52
71
|
let(:reader){ SchemaTools::Reader.new }
|
53
72
|
|
54
|
-
it '
|
73
|
+
it 'reads a single schema' do
|
55
74
|
schema = reader.read(:client)
|
56
75
|
schema[:name].should == 'client'
|
57
76
|
schema[:properties].should_not be_empty
|
58
77
|
reader.registry[:client].should_not be_empty
|
59
78
|
end
|
60
79
|
|
61
|
-
it '
|
80
|
+
it 'reads a single schema from Ruby Hash' do
|
62
81
|
schema = reader.read(:numbers, schema_as_ruby_object)
|
63
82
|
schema[:name].should == 'numbers'
|
64
83
|
schema[:properties].should_not be_empty
|
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.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Georg Leciejewski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -101,6 +101,8 @@ files:
|
|
101
101
|
- spec/fixtures/contact.json
|
102
102
|
- spec/fixtures/includes_basic_definitions.json
|
103
103
|
- spec/fixtures/lead.json
|
104
|
+
- spec/fixtures/nested_schemas/person.json
|
105
|
+
- spec/fixtures/one_of_definition.json
|
104
106
|
- spec/fixtures/page.json
|
105
107
|
- spec/schema_tools/cleaner_spec.rb
|
106
108
|
- spec/schema_tools/hash_spec.rb
|