json_schema_tools 0.6.5 → 0.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/schema_tools/modules/attributes.rb +31 -9
- data/lib/schema_tools/modules/validations.rb +2 -0
- data/lib/schema_tools/version.rb +1 -1
- data/spec/fixtures/schemata/item_collection.json +6 -3
- data/spec/fixtures/schemata/line_item.json +7 -0
- data/spec/schema_tools/modules/attributes_spec.rb +35 -4
- metadata +3 -3
- data/spec/fixtures/schemata/item.json +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2117356ee68c91b5dc2fab298dd331852f706050
|
4
|
+
data.tar.gz: 1871def586732a52a657ce2e703170090f79a3ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63bb2363a46cb8daae284574050138ae4c33217fa00ecfed11ce08ff1199601b3e5f5fdf9eea08824d105a7e6e2fc42a3e4f8e86e0ece32475dc89ef15cb2285
|
7
|
+
data.tar.gz: b51afad29cf08a7dd75d08e2f39dac0b9561b298a7bf7e8143a43abf666486da2522ba3d1369ecb0203a149a70076ea9c99fee880dc7264491e202396a835d83
|
@@ -37,9 +37,10 @@ module SchemaTools
|
|
37
37
|
|
38
38
|
self.schema= reader.read(schema_name, schema_location)
|
39
39
|
self.schema_name(schema_name)
|
40
|
-
#
|
40
|
+
# create getter/setter methods, reading/writing to schema_attrs hash
|
41
41
|
self.schema[:properties].each do |key, prop|
|
42
42
|
define_method(key) { schema_attrs[key] }
|
43
|
+
# TODO convert string values to int/date/datetime?? or use from_hash for it?
|
43
44
|
define_method("#{key}=") { |value| schema_attrs[key] = value } unless prop['readOnly']
|
44
45
|
end
|
45
46
|
end
|
@@ -75,7 +76,8 @@ module SchemaTools
|
|
75
76
|
#
|
76
77
|
# @param [Hash{String=>Mixed}] json string or hash
|
77
78
|
# @param [Object] obj if you want to update an existing objects
|
78
|
-
# attributes. e.g during an update
|
79
|
+
# attributes. e.g during an update beware that this also updates read-only
|
80
|
+
# properties
|
79
81
|
def from_hash(hash, obj=nil)
|
80
82
|
# test if hash is nested and shift up
|
81
83
|
if hash.length == 1 && hash["#{schema_name}"]
|
@@ -98,7 +100,7 @@ module SchemaTools
|
|
98
100
|
when 'object'
|
99
101
|
conv_val = process_object_type(key, val)
|
100
102
|
when 'array'
|
101
|
-
conv_val = process_array_type(key, val)
|
103
|
+
conv_val = process_array_type(key, val, obj)
|
102
104
|
else
|
103
105
|
conv_val = val
|
104
106
|
end
|
@@ -137,14 +139,34 @@ module SchemaTools
|
|
137
139
|
end
|
138
140
|
end
|
139
141
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
142
|
+
# @param [Symbol|String] field_name
|
143
|
+
# @param [Array<Hash>] values
|
144
|
+
# @param [Object] field_name
|
145
|
+
# @param [Object] obj the current object containing the nested values
|
146
|
+
def process_array_type(field_name, values, obj)
|
147
|
+
nested_klass = nested_class(field_name.to_s.singularize)
|
148
|
+
return values unless nested_klass
|
149
|
+
res = []
|
150
|
+
# check for existing nested objects and update them
|
151
|
+
if schema['properties']["#{field_name}"]['items'] && schema['properties']["#{field_name}"]['items']['type'] == 'object'
|
152
|
+
# detect an update of existing which have an ID
|
153
|
+
existing_objs = obj.public_send(field_name)
|
154
|
+
values.each do |new_hash|
|
155
|
+
if new_hash['id'].present? || new_hash[:id].present?
|
156
|
+
# update existing if found
|
157
|
+
existing_obj = existing_objs && existing_objs.detect{|i| "#{i.id}" == "#{new_hash[:id] || new_hash['id']}" }
|
158
|
+
# existing_obj can be nil in this case a new object is created
|
159
|
+
res << nested_klass.from_hash(new_hash, existing_obj)
|
160
|
+
else # create new
|
161
|
+
res << nested_klass.from_hash(new_hash)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
else #create new
|
165
|
+
values.map do |element|
|
166
|
+
res << nested_klass.from_hash(element)
|
144
167
|
end
|
145
|
-
else
|
146
|
-
value
|
147
168
|
end
|
169
|
+
res
|
148
170
|
end
|
149
171
|
|
150
172
|
def nested_class(field_name)
|
@@ -19,6 +19,7 @@ module SchemaTools
|
|
19
19
|
# Runs all the validations within the specified context.
|
20
20
|
# @return [Boolean] true if no errors are found, false otherwise
|
21
21
|
def valid?
|
22
|
+
# TODO validate nested objects
|
22
23
|
output = super
|
23
24
|
errors.empty? && output
|
24
25
|
end
|
@@ -40,6 +41,7 @@ module SchemaTools
|
|
40
41
|
validates_numericality_of key, validate_number_opts(val, is_required) if val['type'] == 'number' || val['type'] == 'integer'
|
41
42
|
#TODO array minItems, max unique, null, string
|
42
43
|
# format: date-time, regex color style, email,uri, ..
|
44
|
+
#TODO validate nested objects, ary of nested objs
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
data/lib/schema_tools/version.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
{
|
2
2
|
"type" : "object",
|
3
3
|
"properties" : {
|
4
|
-
"
|
4
|
+
"line_items" : {
|
5
5
|
"type" : "array",
|
6
|
-
"
|
7
|
-
"
|
6
|
+
"items" : {
|
7
|
+
"type" : "object",
|
8
|
+
"properties":{
|
9
|
+
"$ref": "line_item.json#properties"
|
10
|
+
}
|
8
11
|
}
|
9
12
|
}
|
10
13
|
}
|
@@ -25,9 +25,9 @@ class ItemCollection
|
|
25
25
|
has_schema_attrs :item_collection
|
26
26
|
end
|
27
27
|
|
28
|
-
class
|
28
|
+
class LineItem
|
29
29
|
include SchemaTools::Modules::Attributes
|
30
|
-
has_schema_attrs :
|
30
|
+
has_schema_attrs :line_item
|
31
31
|
end
|
32
32
|
|
33
33
|
describe SchemaTools::Modules::Attributes do
|
@@ -142,9 +142,9 @@ describe SchemaTools::Modules::Attributes do
|
|
142
142
|
end
|
143
143
|
|
144
144
|
it 'makes nested array of objects if there are nested arrays of hashes' do
|
145
|
-
hash = {
|
145
|
+
hash = {line_items: [{}, {}]}
|
146
146
|
obj = ItemCollection.from_hash(hash)
|
147
|
-
expect(obj.
|
147
|
+
expect(obj.line_items.first).to be_an_instance_of(LineItem)
|
148
148
|
end
|
149
149
|
|
150
150
|
it 'updates an object' do
|
@@ -157,6 +157,37 @@ describe SchemaTools::Modules::Attributes do
|
|
157
157
|
expect(obj.last_name).to eq 'Hulk'
|
158
158
|
end
|
159
159
|
|
160
|
+
it 'updates nested array of objects' do
|
161
|
+
obj = ItemCollection.from_hash(
|
162
|
+
{
|
163
|
+
line_items:[
|
164
|
+
{id: 1},
|
165
|
+
{id: '2'}
|
166
|
+
]
|
167
|
+
} )
|
168
|
+
expect(obj.line_items[0].id).to eq "1"
|
169
|
+
|
170
|
+
update_params = {
|
171
|
+
line_items:[
|
172
|
+
{
|
173
|
+
id: 1,
|
174
|
+
name: 'Item one'
|
175
|
+
},
|
176
|
+
{
|
177
|
+
id: '2',
|
178
|
+
name: 'Item two'
|
179
|
+
},
|
180
|
+
{
|
181
|
+
name: 'New item'
|
182
|
+
},
|
183
|
+
]
|
184
|
+
}
|
185
|
+
ItemCollection.from_hash( update_params, obj)
|
186
|
+
expect(obj.line_items[0].name).to eq 'Item one'
|
187
|
+
expect(obj.line_items[1].name).to eq 'Item two'
|
188
|
+
expect(obj.line_items[2].name).to eq 'New item'
|
189
|
+
end
|
190
|
+
|
160
191
|
end
|
161
192
|
|
162
193
|
|
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.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: 2015-
|
11
|
+
date: 2015-09-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -105,9 +105,9 @@ files:
|
|
105
105
|
- spec/fixtures/schemata/contact.json
|
106
106
|
- spec/fixtures/schemata/includes_basic_definitions.json
|
107
107
|
- spec/fixtures/schemata/includes_deep_nested_refs.json
|
108
|
-
- spec/fixtures/schemata/item.json
|
109
108
|
- spec/fixtures/schemata/item_collection.json
|
110
109
|
- spec/fixtures/schemata/lead.json
|
110
|
+
- spec/fixtures/schemata/line_item.json
|
111
111
|
- spec/fixtures/schemata/nested_object_no_properties.json
|
112
112
|
- spec/fixtures/schemata/nested_schemas/person.json
|
113
113
|
- spec/fixtures/schemata/one_of_definition.json
|