blumquist 0.3.2 → 0.4.0

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: 389e1b2f8a0295f3c975f7b3877da1877c32d150
4
- data.tar.gz: d144ddd0daf59b3ce2719bfe60b7787d0b263417
3
+ metadata.gz: 3ee6152ea1367e4b7e55b943b64f0e299a29eb8a
4
+ data.tar.gz: 3aa49823ff6acee7a49542e0029c88969d9c5ecc
5
5
  SHA512:
6
- metadata.gz: 7b0d8b821d05fe847fc2670a86498849fb50f3b0ccfffb9c220c97876ca6d846eac7aadc93ac0692b7e1055d428552f8c4679bea7a57551b070ebb76371b6544
7
- data.tar.gz: 335da3462e4abb87f212a5c63c83ab6fc6ae3196faeaec8293612a78f824bd98171b663e7e63562fd02fa804baf8502f49883b6d28f7b2785a814eb4150f3822
6
+ metadata.gz: 438a17212917a8dd877ad8d0ad3ca0657f2c0a0e929a3c6104578dee237720bdb734603b99b6aeaf4dbd24ae7c7cb04e325a2a0fc5d38e71ba07f2cf2c55984f
7
+ data.tar.gz: f0524889add5b31f04db920b48ffc378a264e538211282fa1da0872df4f60eee49751e796b2eefe6a3cd3966c302d79d4222496c34c96b4295df93144044988f
@@ -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
- - fix #2 (arrays of primitives)
6
+ - Fix #2 (arrays of primitives)
3
7
 
4
8
  # 0.3.1
5
- - support array type definitions that are expressed as an object
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
- - important whitespace changes
10
- - proper exceptions
11
- - disallow arrays with undefined item types
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
- - validate objects (if desired)
15
- - use keyword arguments (in <2.0 compat mode)
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
@@ -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"
@@ -1,4 +1,4 @@
1
- require "blumquist/version"
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::UnsupportedType, @schema[:type])
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 type_def[:type] == 'object'
66
+ if type == 'object' || type_def[:oneOf]
60
67
  blumquistify_object(property)
61
68
 
62
69
  # Turn array elements into Blumquists
63
- elsif type_def[:type] == 'array'
70
+ elsif type == 'array'
64
71
  blumquistify_array(property)
65
72
 
66
73
  # Nothing to do for primitive values
67
- elsif primitive_type?(type_def[: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, type_def[:type])
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
- @data[property] = Blumquist.new(schema: sub_schema, data: @data[property], validate: @validate)
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]
@@ -0,0 +1,4 @@
1
+ class Blumquist
2
+ class Error < RuntimeError
3
+ end
4
+ end
@@ -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'
@@ -1,6 +1,6 @@
1
1
  class Blumquist
2
2
  module Errors
3
- class InvalidPointer < RuntimeError
3
+ class InvalidPointer < Blumquist::Error
4
4
  def initialize(pointer)
5
5
  super("Could not find pointer #{pointer.to_s.to_json}")
6
6
  end
@@ -1,6 +1,6 @@
1
1
  class Blumquist
2
2
  module Errors
3
- class MissingArrayItemstype < RuntimeError
3
+ class MissingArrayItemstype < Blumquist::Error
4
4
  def initialize(property)
5
5
  super("Array items' type missing in #{property.to_json}")
6
6
  end
@@ -0,0 +1,9 @@
1
+ class Blumquist
2
+ module Errors
3
+ class MissingProperties < Blumquist::Error
4
+ def initialize(type)
5
+ super("Neither 'properties' nor 'oneOf' defined in #{type.to_s.to_json}")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -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
@@ -0,0 +1,9 @@
1
+ class Blumquist
2
+ module Errors
3
+ class UnsupportedSchema < Blumquist::Error
4
+ def initialize(type)
5
+ super("The top level object of the schema must be \"object\" (it is #{type.to_json})")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,8 +1,8 @@
1
1
  class Blumquist
2
2
  module Errors
3
- class UnsupportedType < RuntimeError
3
+ class UnsupportedType < Blumquist::Error
4
4
  def initialize(type)
5
- super("Only null, boolean, number, string, array and object types are supported. Unsupported type #{type.to_s.to_json}")
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
@@ -1,3 +1,3 @@
1
1
  class Blumquist
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
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.3.2
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: 2015-12-09 00:00:00.000000000 Z
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: