blumquist 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -7
- data/README.md +1 -1
- data/blumquist.gemspec +1 -0
- data/lib/blumquist.rb +73 -8
- data/lib/blumquist/error.rb +4 -0
- data/lib/blumquist/errors.rb +6 -0
- data/lib/blumquist/errors/invalid_pointer.rb +1 -1
- data/lib/blumquist/errors/missing_array_items_type.rb +1 -1
- data/lib/blumquist/errors/missing_properties.rb +9 -0
- data/lib/blumquist/errors/no_compatible_one_of.rb +11 -0
- data/lib/blumquist/errors/unsupported_schema.rb +9 -0
- data/lib/blumquist/errors/unsupported_type.rb +2 -2
- data/lib/blumquist/version.rb +1 -1
- metadata +21 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ee6152ea1367e4b7e55b943b64f0e299a29eb8a
|
4
|
+
data.tar.gz: 3aa49823ff6acee7a49542e0029c88969d9c5ecc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 438a17212917a8dd877ad8d0ad3ca0657f2c0a0e929a3c6104578dee237720bdb734603b99b6aeaf4dbd24ae7c7cb04e325a2a0fc5d38e71ba07f2cf2c55984f
|
7
|
+
data.tar.gz: f0524889add5b31f04db920b48ffc378a264e538211282fa1da0872df4f60eee49751e796b2eefe6a3cd3966c302d79d4222496c34c96b4295df93144044988f
|
data/CHANGELOG.md
CHANGED
@@ -1,15 +1,19 @@
|
|
1
|
+
# 0.4.0
|
2
|
+
- Support properties with multiple types
|
3
|
+
(e.g. an object, but also null)
|
4
|
+
|
1
5
|
# 0.3.2
|
2
|
-
-
|
6
|
+
- Fix #2 (arrays of primitives)
|
3
7
|
|
4
8
|
# 0.3.1
|
5
|
-
-
|
9
|
+
- Support array type definitions that are expressed as an object
|
6
10
|
or as an array of objects
|
7
11
|
|
8
12
|
# 0.3.0
|
9
|
-
-
|
10
|
-
-
|
11
|
-
-
|
13
|
+
- Important whitespace changes
|
14
|
+
- Proper exceptions
|
15
|
+
- Disallow arrays with undefined item types
|
12
16
|
|
13
17
|
# 0.2.0 (Oct-30-15)
|
14
|
-
-
|
15
|
-
-
|
18
|
+
- Validate objects (if desired)
|
19
|
+
- Use keyword arguments (in <2.0 compat mode)
|
data/README.md
CHANGED
@@ -103,7 +103,7 @@ Or install it yourself as:
|
|
103
103
|
|
104
104
|
## Contributing
|
105
105
|
|
106
|
-
Bug reports and pull requests are welcome on GitHub in the [issues section](https://github.com/moviepilot/blumquist/issues). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
106
|
+
Bug reports and pull requests are welcome on GitHub in the [issues section](https://github.com/moviepilot/blumquist/issues). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
107
107
|
|
108
108
|
|
109
109
|
## License
|
data/blumquist.gemspec
CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency "bundler", "~> 1"
|
26
26
|
spec.add_development_dependency "rake", "~> 10.0"
|
27
27
|
spec.add_development_dependency "rspec"
|
28
|
+
spec.add_development_dependency "pry"
|
28
29
|
spec.add_development_dependency "coveralls"
|
29
30
|
spec.add_development_dependency "simplecov"
|
30
31
|
spec.add_development_dependency "guard-rspec"
|
data/lib/blumquist.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'blumquist/version'
|
2
2
|
require 'active_support/core_ext/hash/indifferent_access'
|
3
3
|
require 'json'
|
4
4
|
require 'json-schema'
|
@@ -27,7 +27,7 @@ class Blumquist
|
|
27
27
|
|
28
28
|
def validate_schema
|
29
29
|
return if @schema[:type] == 'object'
|
30
|
-
raise(Errors::
|
30
|
+
raise(Errors::UnsupportedSchema, type: @schema[:type])
|
31
31
|
end
|
32
32
|
|
33
33
|
def resolve_json_pointers
|
@@ -54,21 +54,28 @@ class Blumquist
|
|
54
54
|
|
55
55
|
def define_getters
|
56
56
|
@schema[:properties].each do |property, type_def|
|
57
|
+
types = [type_def[:type]].flatten - ["null"]
|
58
|
+
type = types.first
|
59
|
+
|
60
|
+
# The type_def can contain one or more types.
|
61
|
+
# We only support single types, or one
|
62
|
+
# normal type and the null type.
|
63
|
+
raise(Errors::UnsupportedType, type_def[:type]) if types.length > 1
|
57
64
|
|
58
65
|
# Wrap objects recursively
|
59
|
-
if
|
66
|
+
if type == 'object' || type_def[:oneOf]
|
60
67
|
blumquistify_object(property)
|
61
68
|
|
62
69
|
# Turn array elements into Blumquists
|
63
|
-
elsif
|
70
|
+
elsif type == 'array'
|
64
71
|
blumquistify_array(property)
|
65
72
|
|
66
73
|
# Nothing to do for primitive values
|
67
|
-
elsif primitive_type?(
|
74
|
+
elsif primitive_type?(type)
|
68
75
|
|
69
76
|
# We don't know what to do, so let's panic
|
70
77
|
else
|
71
|
-
raise(Errors::UnsupportedType,
|
78
|
+
raise(Errors::UnsupportedType, type)
|
72
79
|
end
|
73
80
|
|
74
81
|
# And define the getter
|
@@ -88,7 +95,66 @@ class Blumquist
|
|
88
95
|
sub_schema = @schema[:properties][property].merge(
|
89
96
|
definitions: @schema[:definitions]
|
90
97
|
)
|
91
|
-
|
98
|
+
|
99
|
+
# If properties are defined directly, like this:
|
100
|
+
#
|
101
|
+
# { "type": "object", "properties": { ... } }
|
102
|
+
#
|
103
|
+
if sub_schema[:properties]
|
104
|
+
sub_blumquist = Blumquist.new(schema: sub_schema, data: @data[property], validate: @validate)
|
105
|
+
@data[property] = sub_blumquist
|
106
|
+
return
|
107
|
+
end
|
108
|
+
|
109
|
+
# Properties not defined directly, object must be 'oneOf',
|
110
|
+
# like this:
|
111
|
+
#
|
112
|
+
# { "type": "object", "oneOf": [{...}] }
|
113
|
+
#
|
114
|
+
# The json schema v4 draft specifies, that:
|
115
|
+
#
|
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"
|
117
|
+
#
|
118
|
+
# *See: http://json-schema.org/example2.html
|
119
|
+
#
|
120
|
+
# That means we can just go through the oneOfs and return
|
121
|
+
# the first that matches:
|
122
|
+
if sub_schema[:oneOf]
|
123
|
+
primitive_allowed = false
|
124
|
+
sub_schema[:oneOf].each do |one|
|
125
|
+
begin
|
126
|
+
if primitive_type?(one[:type])
|
127
|
+
primitive_allowed = true
|
128
|
+
else
|
129
|
+
if one[:type]
|
130
|
+
schema = one.merge(definitions: @schema[:definitions])
|
131
|
+
else
|
132
|
+
schema = resolve_json_pointer!(one).merge(
|
133
|
+
definitions: @schema[:definitions]
|
134
|
+
)
|
135
|
+
end
|
136
|
+
@data[property] = Blumquist.new(data: @data[property], schema: schema)
|
137
|
+
return
|
138
|
+
end
|
139
|
+
rescue
|
140
|
+
# On to the next oneOf
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# We found no matching object definition.
|
145
|
+
# If a primitve is part of the `oneOfs,
|
146
|
+
# that's no problem though.
|
147
|
+
return if primitive_allowed
|
148
|
+
|
149
|
+
# 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
|
153
|
+
end
|
154
|
+
|
155
|
+
# If there's neither `properties` nor `oneOf`, we don't
|
156
|
+
# know what to do and shall panic:
|
157
|
+
raise(Errors::MissingProperties, sub_schema)
|
92
158
|
end
|
93
159
|
|
94
160
|
def blumquistify_array(property)
|
@@ -112,7 +178,6 @@ class Blumquist
|
|
112
178
|
# The items of this array are defined by a pointer
|
113
179
|
if type_def[:$ref]
|
114
180
|
item_schema = resolve_json_pointer!(type_def)
|
115
|
-
raise(Errors::MissingArrayItemsType, @schema[:properties][property]) unless item_schema
|
116
181
|
|
117
182
|
sub_schema = item_schema.merge(
|
118
183
|
definitions: @schema[:definitions]
|
data/lib/blumquist/errors.rb
CHANGED
@@ -1 +1,7 @@
|
|
1
|
+
require 'blumquist/error'
|
2
|
+
require 'blumquist/errors/unsupported_schema'
|
1
3
|
require 'blumquist/errors/unsupported_type'
|
4
|
+
require 'blumquist/errors/invalid_pointer'
|
5
|
+
require 'blumquist/errors/missing_array_items_type'
|
6
|
+
require 'blumquist/errors/missing_properties'
|
7
|
+
require 'blumquist/errors/no_compatible_one_of'
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Blumquist
|
2
|
+
module Errors
|
3
|
+
class NoCompatibleOneOf < Blumquist::Error
|
4
|
+
def initialize(options)
|
5
|
+
data = options[:data]
|
6
|
+
one_ofs = options[:one_ofs]
|
7
|
+
super("Could not find a matching schema for #{data.to_json} in the oneOfs: #{one_ofs.to_json}")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
class Blumquist
|
2
2
|
module Errors
|
3
|
-
class UnsupportedType <
|
3
|
+
class UnsupportedType < Blumquist::Error
|
4
4
|
def initialize(type)
|
5
|
-
super("
|
5
|
+
super("Unsupported type '#{type.to_s}' (#{%w{null, boolean, number, string, array object}.to_json} are supported)")
|
6
6
|
end
|
7
7
|
end
|
8
8
|
end
|
data/lib/blumquist/version.rb
CHANGED
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
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jannis Hermanns
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: coveralls
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -168,9 +182,13 @@ files:
|
|
168
182
|
- Rakefile
|
169
183
|
- blumquist.gemspec
|
170
184
|
- lib/blumquist.rb
|
185
|
+
- lib/blumquist/error.rb
|
171
186
|
- lib/blumquist/errors.rb
|
172
187
|
- lib/blumquist/errors/invalid_pointer.rb
|
173
188
|
- lib/blumquist/errors/missing_array_items_type.rb
|
189
|
+
- lib/blumquist/errors/missing_properties.rb
|
190
|
+
- lib/blumquist/errors/no_compatible_one_of.rb
|
191
|
+
- lib/blumquist/errors/unsupported_schema.rb
|
174
192
|
- lib/blumquist/errors/unsupported_type.rb
|
175
193
|
- lib/blumquist/version.rb
|
176
194
|
homepage: https://github.com/moviepilot/blumquist
|
@@ -199,3 +217,4 @@ specification_version: 4
|
|
199
217
|
summary: Turn some data and a json schema into an immutable object with getters from
|
200
218
|
the schema
|
201
219
|
test_files: []
|
220
|
+
has_rdoc:
|