json-schema_builder 0.7.1 → 0.8.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 +4 -4
- data/lib/json/schema_builder/array.rb +1 -0
- data/lib/json/schema_builder/attribute.rb +4 -1
- data/lib/json/schema_builder/dsl.rb +3 -1
- data/lib/json/schema_builder/entity.rb +15 -0
- data/lib/json/schema_builder/object.rb +5 -0
- data/lib/json/schema_builder/version.rb +1 -1
- data/spec/integration/builder_initialization_spec.rb +49 -0
- data/spec/integration/builder_reopening_spec.rb +49 -0
- data/spec/support/examples/builder_initialization.rb +41 -0
- data/spec/support/examples/builder_reopening.rb +13 -0
- data/spec/unit/entity_spec.rb +48 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2945d0d0e0746865b799fd0e2dcb0e1b066c52db
|
4
|
+
data.tar.gz: 40da6b42be71e5d59e72115956f9ad98c8950650
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5854cbff037513e952a31785d38e5e2e3b029b389dfafeaff23f0bdc1f07a9969861fec5b3ec2b58db7c7737ede5d726045c39a02b157e8feb6ca42cf468a0ef
|
7
|
+
data.tar.gz: 01e0e5bb89620531afaf9392e7691ea282e36f31858b54df70cfb938ea64b8d18878262f7c5757b7c2ff001ae2e92f19299f660dba022f4e1c6dca088a2af35f
|
@@ -7,11 +7,14 @@ module JSON
|
|
7
7
|
def attribute(name, as: nil, array: false)
|
8
8
|
attr = as || snakeize(name)
|
9
9
|
define_method name do |*values|
|
10
|
-
if array
|
10
|
+
result = if array
|
11
11
|
_array_attr attr, values.flatten
|
12
12
|
else
|
13
13
|
_attr attr, values.first
|
14
14
|
end
|
15
|
+
|
16
|
+
parent.reinitialize if parent
|
17
|
+
result
|
15
18
|
end
|
16
19
|
alias_method "#{ name }=", name
|
17
20
|
end
|
@@ -44,6 +44,21 @@ module JSON
|
|
44
44
|
initialize_with opts
|
45
45
|
eval_block &block
|
46
46
|
extract_types
|
47
|
+
@initialized = true
|
48
|
+
end
|
49
|
+
|
50
|
+
def initialized?
|
51
|
+
!!@initialized
|
52
|
+
end
|
53
|
+
|
54
|
+
def reinitialize
|
55
|
+
end
|
56
|
+
|
57
|
+
def extend(child_name, &block)
|
58
|
+
children.find { |c| c.name == child_name.to_sym }.tap do |child|
|
59
|
+
raise "Property #{child_name} does not exist" unless child
|
60
|
+
child.eval_block(&block) if block_given?
|
61
|
+
end
|
47
62
|
end
|
48
63
|
|
49
64
|
def add_fragment(child)
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Examples::BuilderInitialization, type: :integration do
|
4
|
+
it_behaves_like 'a builder' do
|
5
|
+
let(:expected_json) do
|
6
|
+
{
|
7
|
+
type: :object,
|
8
|
+
properties: {
|
9
|
+
name: {
|
10
|
+
type: :string
|
11
|
+
},
|
12
|
+
settings: {
|
13
|
+
type: :object,
|
14
|
+
properties: {
|
15
|
+
email: {
|
16
|
+
type: :string
|
17
|
+
}
|
18
|
+
}
|
19
|
+
},
|
20
|
+
preferences: {
|
21
|
+
anyOf: [
|
22
|
+
{
|
23
|
+
type: :object,
|
24
|
+
properties: {
|
25
|
+
enabled: {
|
26
|
+
type: :boolean,
|
27
|
+
default: true
|
28
|
+
}
|
29
|
+
}
|
30
|
+
},
|
31
|
+
{
|
32
|
+
type: :null
|
33
|
+
}
|
34
|
+
]
|
35
|
+
},
|
36
|
+
ids: {
|
37
|
+
type: :array,
|
38
|
+
items: {
|
39
|
+
anyOf: [
|
40
|
+
{type: :number},
|
41
|
+
{type: :string}
|
42
|
+
]
|
43
|
+
}
|
44
|
+
}
|
45
|
+
}
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Examples::BuilderReopening, type: :integration do
|
4
|
+
it_behaves_like 'a builder' do
|
5
|
+
let(:expected_json) do
|
6
|
+
{
|
7
|
+
type: :object,
|
8
|
+
properties: {
|
9
|
+
name: {
|
10
|
+
type: :string
|
11
|
+
},
|
12
|
+
settings: {
|
13
|
+
type: :object,
|
14
|
+
properties: {
|
15
|
+
email: {
|
16
|
+
type: :string
|
17
|
+
},
|
18
|
+
phone_number: {
|
19
|
+
type: :string
|
20
|
+
}
|
21
|
+
}
|
22
|
+
},
|
23
|
+
preferences: {
|
24
|
+
anyOf: [
|
25
|
+
{
|
26
|
+
type: :object,
|
27
|
+
properties: {
|
28
|
+
enabled: {
|
29
|
+
type: :boolean,
|
30
|
+
default: true
|
31
|
+
}
|
32
|
+
}
|
33
|
+
},
|
34
|
+
{
|
35
|
+
type: :null
|
36
|
+
}
|
37
|
+
]
|
38
|
+
},
|
39
|
+
ids: {
|
40
|
+
type: :array,
|
41
|
+
items: {
|
42
|
+
type: :string,
|
43
|
+
}
|
44
|
+
}
|
45
|
+
}
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Examples
|
2
|
+
class BuilderInitialization
|
3
|
+
include JSON::SchemaBuilder
|
4
|
+
|
5
|
+
def example
|
6
|
+
obj = object
|
7
|
+
obj.string :name
|
8
|
+
settings_for(obj)
|
9
|
+
preferences_for(obj)
|
10
|
+
add_ids_to(obj)
|
11
|
+
obj
|
12
|
+
end
|
13
|
+
|
14
|
+
def settings_for(obj)
|
15
|
+
settings = obj.object :settings
|
16
|
+
settings.string :email
|
17
|
+
settings
|
18
|
+
end
|
19
|
+
|
20
|
+
def preferences_for(obj)
|
21
|
+
preferences = obj.entity :preferences
|
22
|
+
preferences.any_of preference
|
23
|
+
preferences.any_of null
|
24
|
+
end
|
25
|
+
|
26
|
+
def preference
|
27
|
+
obj = object
|
28
|
+
enabled = obj.boolean :enabled
|
29
|
+
enabled.default = true
|
30
|
+
obj
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_ids_to(obj)
|
34
|
+
ids = obj.array :ids
|
35
|
+
ids.items do
|
36
|
+
any_of number
|
37
|
+
any_of string
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/unit/entity_spec.rb
CHANGED
@@ -181,4 +181,52 @@ RSpec.describe JSON::SchemaBuilder::Entity, type: :unit do
|
|
181
181
|
expect(schema.example.fragments.keys).to match_array(%w(#/ #/parent_name))
|
182
182
|
end
|
183
183
|
end
|
184
|
+
|
185
|
+
describe "#extend" do
|
186
|
+
let(:object) do
|
187
|
+
obj = JSON::SchemaBuilder::Object.new(nil) do
|
188
|
+
string :test
|
189
|
+
object :sub_object do
|
190
|
+
string :a_string
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
obj.extend(:sub_object) do
|
195
|
+
string :another_string
|
196
|
+
number :a_number, required: true
|
197
|
+
end
|
198
|
+
|
199
|
+
obj
|
200
|
+
end
|
201
|
+
let(:schema) { object.schema }
|
202
|
+
let(:properties) { schema[:properties] }
|
203
|
+
let(:sub_object) { properties[:sub_object] }
|
204
|
+
let(:sub_properties) { sub_object[:properties] }
|
205
|
+
|
206
|
+
it "does not remove properties" do
|
207
|
+
expect(properties[:test]).to eq "type" => "string"
|
208
|
+
expect(sub_object).to include "type" => "object"
|
209
|
+
expect(sub_properties).to include "a_string" => {"type" => "string"}
|
210
|
+
end
|
211
|
+
|
212
|
+
it "adds new properties" do
|
213
|
+
expect(sub_properties).to include "another_string" => {"type" => "string"}
|
214
|
+
expect(sub_properties).to include "a_number" => {"type" => "number"}
|
215
|
+
expect(sub_object[:required]).to match_array ["a_number"]
|
216
|
+
end
|
217
|
+
|
218
|
+
it "raises on unknown properties" do
|
219
|
+
expect do
|
220
|
+
object.extend(:foo) do
|
221
|
+
raise "doesn't work"
|
222
|
+
end
|
223
|
+
end.to raise_error "Property foo does not exist"
|
224
|
+
end
|
225
|
+
|
226
|
+
it "returns the child" do
|
227
|
+
child = object.extend(:sub_object)
|
228
|
+
expect(child).to be_a(JSON::SchemaBuilder::Object)
|
229
|
+
expect(child.name).to eq(:sub_object)
|
230
|
+
end
|
231
|
+
end
|
184
232
|
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.
|
4
|
+
version: 0.8.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-03-
|
11
|
+
date: 2018-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -203,6 +203,8 @@ files:
|
|
203
203
|
- lib/json/schema_builder/version.rb
|
204
204
|
- spec/integration/array_any_of_spec.rb
|
205
205
|
- spec/integration/array_definitions_spec.rb
|
206
|
+
- spec/integration/builder_initialization_spec.rb
|
207
|
+
- spec/integration/builder_reopening_spec.rb
|
206
208
|
- spec/integration/context_delegation_spec.rb
|
207
209
|
- spec/integration/custom_errors_spec.rb
|
208
210
|
- spec/integration/entity_literals_spec.rb
|
@@ -224,6 +226,8 @@ files:
|
|
224
226
|
- spec/support/attribute_matcher.rb
|
225
227
|
- spec/support/examples/array_any_of.rb
|
226
228
|
- spec/support/examples/array_definitions.rb
|
229
|
+
- spec/support/examples/builder_initialization.rb
|
230
|
+
- spec/support/examples/builder_reopening.rb
|
227
231
|
- spec/support/examples/context_delegation.rb
|
228
232
|
- spec/support/examples/custom_errors.rb
|
229
233
|
- spec/support/examples/entity_literals.rb
|
@@ -281,6 +285,8 @@ summary: Build JSON schemas with Ruby
|
|
281
285
|
test_files:
|
282
286
|
- spec/integration/array_any_of_spec.rb
|
283
287
|
- spec/integration/array_definitions_spec.rb
|
288
|
+
- spec/integration/builder_initialization_spec.rb
|
289
|
+
- spec/integration/builder_reopening_spec.rb
|
284
290
|
- spec/integration/context_delegation_spec.rb
|
285
291
|
- spec/integration/custom_errors_spec.rb
|
286
292
|
- spec/integration/entity_literals_spec.rb
|
@@ -302,6 +308,8 @@ test_files:
|
|
302
308
|
- spec/support/attribute_matcher.rb
|
303
309
|
- spec/support/examples/array_any_of.rb
|
304
310
|
- spec/support/examples/array_definitions.rb
|
311
|
+
- spec/support/examples/builder_initialization.rb
|
312
|
+
- spec/support/examples/builder_reopening.rb
|
305
313
|
- spec/support/examples/context_delegation.rb
|
306
314
|
- spec/support/examples/custom_errors.rb
|
307
315
|
- spec/support/examples/entity_literals.rb
|