json-schema_builder 0.5.0 → 0.6.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: 7e1e931da1f8e1ef88c5f267408422b8195f7d85
4
- data.tar.gz: 561d28974eb9bcaa95d84c3799db95ea466ee3b1
3
+ metadata.gz: e13b97322fe070943bcaa063cbb11661fd0b576d
4
+ data.tar.gz: eb0dfeccc78fc5af31b50889af7473a9eeedf566
5
5
  SHA512:
6
- metadata.gz: de361202331e17c8b447fda6f5d76de261edef07f367af44493ef4bd1034c040e97a4efbd7c865ed18bd33ef10316bd42543d74d34be1ff6c6b907bf4aa018f7
7
- data.tar.gz: 0d23b47272718d8ca60b575ed39b10c562af101d7537e2b27e064ef8ea4ef87cb6b5822a9eedc65cc1deb8a93f24b555373048a8c68bf4a05671d97f2fab0814
6
+ metadata.gz: 9db0f2942aa747cff7b9f94d41fb363ec03f9262d30ad29b313582787fb6aa7eed1168165bd25ebf07bfad0fff83cb73c22ca60ec7e52c9766c0388ddc57a8cb
7
+ data.tar.gz: 4bea5f690c51c8b3caad56dff0818d72cd20282baf192334e6562597424d220021511ed15794837fb6dabef5128b72822ed657efb6a235852ddd45b85510934a
@@ -25,11 +25,14 @@ module JSON
25
25
 
26
26
  protected
27
27
 
28
- def _array_attr(attr, values)
28
+ def _array_attr(attr, values = [])
29
29
  if values.empty?
30
- self.schema[attr]
30
+ self.schema[attr] || []
31
31
  else
32
- self.schema[attr] = values
32
+ self.schema[attr] ||= []
33
+ self.schema[attr] += values
34
+ self.schema[attr].uniq!
35
+ self.schema[attr]
33
36
  end
34
37
  end
35
38
 
@@ -36,6 +36,7 @@ module JSON
36
36
  initialize_parent_with opts
37
37
  initialize_with opts
38
38
  eval_block &block
39
+ extract_types
39
40
  end
40
41
 
41
42
  def add_fragment(child)
@@ -84,6 +85,16 @@ module JSON
84
85
 
85
86
  protected
86
87
 
88
+ def extract_types
89
+ any_of(null) if @nullable
90
+ if any_of.present?
91
+ everything_else = schema.data.reject { |k, v| k == "anyOf" }
92
+ return unless everything_else.present?
93
+ schema.data.select! { |k, v| k == "anyOf" }
94
+ schema.data["anyOf"].unshift everything_else
95
+ end
96
+ end
97
+
87
98
  def initialize_parent_with(opts)
88
99
  @parent = opts.delete :parent
89
100
  if parent
@@ -96,6 +107,7 @@ module JSON
96
107
  end
97
108
 
98
109
  def initialize_with(opts)
110
+ @nullable = opts.delete :null
99
111
  @options = opts.delete(:root).class.options.to_h if opts[:root]
100
112
  opts.each_pair do |key, value|
101
113
  next if value.nil?
@@ -33,6 +33,10 @@ module JSON
33
33
  any_of list
34
34
  end
35
35
  end
36
+
37
+ def empty_string(name = nil, opts = { }, &block)
38
+ string name, opts.merge(max_length: 0), &block
39
+ end
36
40
  end
37
41
  end
38
42
  end
@@ -11,14 +11,18 @@ module JSON
11
11
  attribute :additional_properties
12
12
  attribute :pattern_properties
13
13
 
14
- def initialize(name, opts = { }, &block)
14
+ def initialize_children
15
15
  self.properties = { }
16
- super
17
- children.each do |child|
16
+ children.select(&:name).each do |child|
18
17
  self.properties[child.name] = child.as_json
19
18
  end
20
19
  end
21
20
 
21
+ def extract_types
22
+ initialize_children
23
+ super
24
+ end
25
+
22
26
  def required(*values)
23
27
  case values
24
28
  when []
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  module SchemaBuilder
3
- VERSION = '0.5.0'
3
+ VERSION = '0.6.0'
4
4
  end
5
5
  end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Examples::MixedAnyOfs, type: :integration do
4
+ it_behaves_like 'a builder' do
5
+ let(:expected_json) do
6
+ {
7
+ type: "object",
8
+ properties: {
9
+ blankable: {
10
+ anyOf: [
11
+ { type: "integer" },
12
+ { type: "string" },
13
+ { type: "null" }
14
+ ]
15
+ },
16
+ nullable: {
17
+ anyOf: [
18
+ {
19
+ type: "object",
20
+ properties: {
21
+ test: {
22
+ type: "string"
23
+ }
24
+ }
25
+ },
26
+ { type: "null" }
27
+ ]
28
+ },
29
+ flexible: {
30
+ anyOf: [
31
+ { type: "string", minLength: 100 },
32
+ { type: "string", maxLength: 0 },
33
+ { type: "integer" },
34
+ {
35
+ type: "object",
36
+ properties: {
37
+ one: {
38
+ type: "string"
39
+ }
40
+ }
41
+ },
42
+ {
43
+ type: "array",
44
+ items: {
45
+ type: "object",
46
+ properties: {
47
+ one: {
48
+ type: "string"
49
+ }
50
+ }
51
+ }
52
+ },
53
+ { type: "null" },
54
+ ]
55
+ },
56
+ }
57
+ }
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,28 @@
1
+ module Examples
2
+ class MixedAnyOfs
3
+ include JSON::SchemaBuilder
4
+
5
+ def example
6
+ object do
7
+ integer :blankable, any_of: [string], null: true
8
+
9
+ object :nullable, null: true do
10
+ string :test
11
+ end
12
+
13
+ entity :flexible, any_of: [string(min_length: 100), empty_string, integer], null: true do
14
+ any_of [
15
+ an_object(self),
16
+ array { items { an_object(self) } }
17
+ ]
18
+ end
19
+ end
20
+ end
21
+
22
+ def an_object(root)
23
+ root.object do
24
+ string :one
25
+ end
26
+ end
27
+ end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Parrish
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-16 00:00:00.000000000 Z
11
+ date: 2018-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -206,6 +206,7 @@ files:
206
206
  - spec/integration/custom_errors_spec.rb
207
207
  - spec/integration/entity_literals_spec.rb
208
208
  - spec/integration/ids_spec.rb
209
+ - spec/integration/mixed_any_ofs_spec.rb
209
210
  - spec/integration/mixed_arrays_spec.rb
210
211
  - spec/integration/mixed_objects_spec.rb
211
212
  - spec/integration/object_definitions_spec.rb
@@ -225,6 +226,7 @@ files:
225
226
  - spec/support/examples/custom_errors.rb
226
227
  - spec/support/examples/entity_literals.rb
227
228
  - spec/support/examples/ids.rb
229
+ - spec/support/examples/mixed_any_ofs.rb
228
230
  - spec/support/examples/mixed_arrays.rb
229
231
  - spec/support/examples/mixed_objects.rb
230
232
  - spec/support/examples/object_definitions.rb
@@ -280,6 +282,7 @@ test_files:
280
282
  - spec/integration/custom_errors_spec.rb
281
283
  - spec/integration/entity_literals_spec.rb
282
284
  - spec/integration/ids_spec.rb
285
+ - spec/integration/mixed_any_ofs_spec.rb
283
286
  - spec/integration/mixed_arrays_spec.rb
284
287
  - spec/integration/mixed_objects_spec.rb
285
288
  - spec/integration/object_definitions_spec.rb
@@ -299,6 +302,7 @@ test_files:
299
302
  - spec/support/examples/custom_errors.rb
300
303
  - spec/support/examples/entity_literals.rb
301
304
  - spec/support/examples/ids.rb
305
+ - spec/support/examples/mixed_any_ofs.rb
302
306
  - spec/support/examples/mixed_arrays.rb
303
307
  - spec/support/examples/mixed_objects.rb
304
308
  - spec/support/examples/object_definitions.rb