blumquist 0.4.1 → 0.4.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a56519a62c8cda5595755e7e4a02c59ceaf06fdb
4
- data.tar.gz: 35fe0e245587b62219342a67541f86fa7f0cbf5a
3
+ metadata.gz: 29ba6e431b5d179ef49358e17f117d29a1d05615
4
+ data.tar.gz: 7c91510b9be03ebf29b43206e10dc69b4fb43e5d
5
5
  SHA512:
6
- metadata.gz: 4d34430761c31b38a0800a3cb8c7f38e3d4ad673560f8a6fc5e2fbd195e1f76dc3db14730146e8c60ac25ba93a5387a5d1c8a55b5bae301f8599cb97032bf95e
7
- data.tar.gz: 3ca561664c837fb74a0f2eea5e5de158dd263a0eaf2c4a17496a141cfaa49c7d00201d213dcdd2dbd3cbc415b78180f73702a126f503609384825b0110ca71f5
6
+ metadata.gz: 4c8285345e463ee65590ed825b82cd9c4583828191d80d5541521becc48e2bf3f31b1347cc81ecef0d467d1de5daa9c66d61fb844760173b9d00efa7313071ec
7
+ data.tar.gz: dd6ac867efcef1d457aa13db890c5d433b28766d68c6d38f1e7ec9924ba0530d89bba7e056f48c3e119f2bbbe37dff6199e54192ee386e7bdc3981970af944d1
@@ -1,3 +1,6 @@
1
+ # 0.4.2
2
+ - Allow type arrays with just one element
3
+
1
4
  # 0.4.1
2
5
  - Fix broken arrays of objects
3
6
 
@@ -64,7 +64,7 @@ class Blumquist
64
64
 
65
65
  # Wrap objects recursively
66
66
  if type == 'object' || type_def[:oneOf]
67
- blumquistify_object(property)
67
+ @data[property] = blumquistify_property(property)
68
68
 
69
69
  # Turn array elements into Blumquists
70
70
  elsif type == 'array'
@@ -91,19 +91,47 @@ class Blumquist
91
91
  end
92
92
  end
93
93
 
94
- def blumquistify_object(property)
94
+ def blumquistify_property(property)
95
95
  sub_schema = @schema[:properties][property].merge(
96
96
  definitions: @schema[:definitions]
97
97
  )
98
+ data = @data[property]
99
+ blumquistify_object(schema: sub_schema, data: data)
100
+ end
101
+
102
+ def blumquistify_object(options)
103
+ sub_schema = options[:schema]
104
+ data = options[:data]
98
105
 
99
106
  # If properties are defined directly, like this:
100
107
  #
101
108
  # { "type": "object", "properties": { ... } }
102
109
  #
103
110
  if sub_schema[:properties]
104
- sub_blumquist = Blumquist.new(schema: sub_schema, data: @data[property], validate: @validate)
105
- @data[property] = sub_blumquist
106
- return
111
+ if sub_schema[:type].is_a?(String)
112
+ sub_blumquist = Blumquist.new(schema: sub_schema, data: data, validate: false)
113
+ return sub_blumquist
114
+ end
115
+
116
+ # If the type is an array, we can't make much of it
117
+ # because we wouldn't know which type to model as a
118
+ # blumquist object. Unless, of course, it's one object
119
+ # and one or more primitives.
120
+ if sub_schema[:type].is_a?(Array)
121
+
122
+ # It's an array but only contains one allowed type,
123
+ # this is easy.
124
+ if sub_schema[:type].length == 1
125
+ sub_schema[:type] = sub_schema[:type].first
126
+ sub_blumquist = Blumquist.new(schema: sub_schema, data: data, validate: false)
127
+ return sub_blumquist
128
+ end
129
+
130
+ # We can implement the other cases at a leter point.
131
+ end
132
+
133
+ # We shouldn't arrive here
134
+ raise(Errors::UnsupportedType, sub_schema)
107
135
  end
108
136
 
109
137
  # Properties not defined directly, object must be 'oneOf',
@@ -113,7 +141,9 @@ class Blumquist
113
141
  #
114
142
  # The json schema v4 draft specifies, that:
115
143
  #
116
- # "the oneOf keyword is new in draft v4; its value is an array of schemas, and an instance is valid if and only if it is valid against exactly one of these schemas"
144
+ # "the oneOf keyword is new in draft v4; its value is an array of
145
+ # schemas, and an instance is valid if and only if it is valid
146
+ # against exactly one of these schemas"
117
147
  #
118
148
  # *See: http://json-schema.org/example2.html
119
149
  #
@@ -133,8 +163,7 @@ class Blumquist
133
163
  definitions: @schema[:definitions]
134
164
  )
135
165
  end
136
- @data[property] = Blumquist.new(data: @data[property], schema: schema)
137
- return
166
+ return Blumquist.new(data: data, schema: schema, validate: true)
138
167
  end
139
168
  rescue
140
169
  # On to the next oneOf
@@ -144,12 +173,10 @@ class Blumquist
144
173
  # We found no matching object definition.
145
174
  # If a primitve is part of the `oneOfs,
146
175
  # that's no problem though.
147
- return if primitive_allowed
176
+ return data if primitive_allowed
148
177
 
149
178
  # We didn't find a schema in oneOf that matches our data
150
- raise(Errors::NoCompatibleOneOf, one_ofs: sub_schema[:oneOf], data: @data[property])
151
-
152
- return
179
+ raise(Errors::NoCompatibleOneOf, one_ofs: sub_schema[:oneOf], data: data)
153
180
  end
154
181
 
155
182
  # If there's neither `properties` nor `oneOf`, we don't
@@ -185,16 +212,16 @@ class Blumquist
185
212
 
186
213
  @data[property] ||= []
187
214
  @data[property] = @data[property].map do |item|
188
- Blumquist.new(schema: sub_schema, data: item, validate: @validate)
215
+ Blumquist.new(schema: sub_schema, data: item, validate: false)
189
216
  end
190
- elsif type_def[:type] == 'object'
217
+ elsif type_def[:type] == 'object' || type_def[:oneOf]
191
218
  sub_schema = type_def.merge(
192
219
  definitions: @schema[:definitions]
193
220
  )
194
221
 
195
222
  @data[property] ||= []
196
223
  @data[property] = @data[property].map do |item|
197
- Blumquist.new(schema: sub_schema, data: item, validate: @validate)
224
+ blumquistify_object(schema: sub_schema, data: item)
198
225
  end
199
226
 
200
227
  elsif primitive_type?(type_def[:type])
@@ -1,3 +1,3 @@
1
1
  class Blumquist
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blumquist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jannis Hermanns
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-28 00:00:00.000000000 Z
11
+ date: 2016-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport