json-schema_builder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +8 -0
  5. data/Gemfile +2 -0
  6. data/Gemfile.lock +66 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +31 -0
  9. data/Rakefile +1 -0
  10. data/json-schema_builder.gemspec +28 -0
  11. data/lib/json/schema_builder.rb +14 -0
  12. data/lib/json/schema_builder/array.rb +29 -0
  13. data/lib/json/schema_builder/boolean.rb +9 -0
  14. data/lib/json/schema_builder/dsl.rb +42 -0
  15. data/lib/json/schema_builder/entity.rb +101 -0
  16. data/lib/json/schema_builder/integer.rb +9 -0
  17. data/lib/json/schema_builder/null.rb +9 -0
  18. data/lib/json/schema_builder/number.rb +9 -0
  19. data/lib/json/schema_builder/numeric.rb +13 -0
  20. data/lib/json/schema_builder/object.rb +36 -0
  21. data/lib/json/schema_builder/schema.rb +16 -0
  22. data/lib/json/schema_builder/string.rb +12 -0
  23. data/lib/json/schema_builder/version.rb +5 -0
  24. data/spec/integration/entity_literals_spec.rb +40 -0
  25. data/spec/integration/mixed_arrays_spec.rb +16 -0
  26. data/spec/integration/schema_builder_spec.rb +19 -0
  27. data/spec/integration/terse_arrays_spec.rb +18 -0
  28. data/spec/integration/terse_objects_spec.rb +30 -0
  29. data/spec/integration/verbose_arrays_spec.rb +18 -0
  30. data/spec/integration/verbose_objects_spec.rb +30 -0
  31. data/spec/spec_helper.rb +13 -0
  32. data/spec/support/.keep +0 -0
  33. data/spec/support/attribute_matcher.rb +17 -0
  34. data/spec/support/examples/entity_literals.rb +33 -0
  35. data/spec/support/examples/mixed_arrays.rb +14 -0
  36. data/spec/support/examples/schema_builder.rb +5 -0
  37. data/spec/support/examples/terse_arrays.rb +11 -0
  38. data/spec/support/examples/terse_objects.rb +16 -0
  39. data/spec/support/examples/verbose_arrays.rb +19 -0
  40. data/spec/support/examples/verbose_objects.rb +24 -0
  41. data/spec/support/integration_helper.rb +12 -0
  42. data/spec/support/shared_contexts_for_entity.rb +24 -0
  43. data/spec/support/shared_examples_for_numeric.rb +10 -0
  44. data/spec/unit/array_spec.rb +55 -0
  45. data/spec/unit/boolean_spec.rb +6 -0
  46. data/spec/unit/dsl_spec.rb +70 -0
  47. data/spec/unit/entity_spec.rb +114 -0
  48. data/spec/unit/integer_spec.rb +7 -0
  49. data/spec/unit/null_spec.rb +6 -0
  50. data/spec/unit/number_spec.rb +7 -0
  51. data/spec/unit/object_spec.rb +28 -0
  52. data/spec/unit/schema_spec.rb +35 -0
  53. data/spec/unit/string_spec.rb +10 -0
  54. metadata +230 -0
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JSON::SchemaBuilder::Boolean, type: :unit do
4
+ subject{ described_class }
5
+ its(:registered_type){ is_expected.to eql :boolean }
6
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JSON::SchemaBuilder::DSL, type: :unit do
4
+ let!(:klass) do
5
+ Class.new do
6
+ include JSON::SchemaBuilder::DSL
7
+ class_attribute :registered_type
8
+ register :something
9
+ def initialize(*args); end
10
+ end
11
+ end
12
+ let(:instance){ klass.new }
13
+
14
+ describe '.register' do
15
+ it 'should register the class' do
16
+ expect(subject.types).to have_key :something
17
+ expect(subject.types[:something]).to eql klass
18
+ end
19
+
20
+ it 'should store the registered type' do
21
+ expect(klass.registered_type).to eql :something
22
+ end
23
+
24
+ context 'with a type method' do
25
+ it 'should generate' do
26
+ expect(instance).to respond_to :something
27
+ end
28
+
29
+ it 'should dispatch to entity' do
30
+ expect(instance).to receive(:entity).with(:something, 1, foo: :bar).and_call_original
31
+ instance.something 1, foo: :bar
32
+ end
33
+
34
+ it 'should allow unnamed entities' do
35
+ expect(instance).to receive(:entity).with(:something, nil, foo: :bar).and_call_original
36
+ instance.something foo: :bar
37
+ end
38
+ end
39
+ end
40
+
41
+ describe '#entity' do
42
+ context 'without an entity' do
43
+ it 'should create the type' do
44
+ expect(instance.class).to receive(:new)
45
+ .with('name', { }).and_call_original
46
+
47
+ entity = instance.entity :something, 'name'
48
+ expect(entity).to be_a klass
49
+ end
50
+ end
51
+
52
+ context 'with an entity' do
53
+ let!(:klass) do
54
+ Class.new JSON::SchemaBuilder::Entity do
55
+ include JSON::SchemaBuilder::DSL
56
+ register :something
57
+ def initialize(*args); end
58
+ end
59
+ end
60
+
61
+ it 'should set the parent' do
62
+ expect(instance.class).to receive(:new)
63
+ .with('name', parent: kind_of(klass)).and_call_original
64
+
65
+ entity = instance.entity :something, 'name'
66
+ expect(entity).to be_a klass
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,114 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JSON::SchemaBuilder::Entity, type: :unit do
4
+ subject{ described_class }
5
+
6
+ it{ is_expected.to define_attribute :title }
7
+ it{ is_expected.to define_attribute :description }
8
+ it{ is_expected.to define_attribute :type }
9
+ it{ is_expected.to define_attribute :enum }
10
+ it{ is_expected.to define_attribute :all_of }
11
+ it{ is_expected.to define_attribute :any_of }
12
+ it{ is_expected.to define_attribute :one_of }
13
+ it{ is_expected.to define_attribute :not_a }
14
+ it{ is_expected.to define_attribute :definitions }
15
+
16
+ describe '.attribute' do
17
+ include_context 'an entity'
18
+
19
+ it 'should define the attribute' do
20
+ expect(klass).to define_attribute :test
21
+ end
22
+
23
+ it 'should delegate the reader to the schema' do
24
+ subject.schema.test = 1
25
+ expect(subject.test).to eql 1
26
+ end
27
+
28
+ it 'should delegate the writer to the schema' do
29
+ subject.test = 1
30
+ expect(subject.schema.test).to eql 1
31
+ end
32
+
33
+ it 'should accept function-style writes' do
34
+ subject.test 1
35
+ expect(subject.test).to eql 1
36
+ end
37
+
38
+ it 'should snakeCase attribute reads' do
39
+ subject.schema.testName = 1
40
+ expect(subject.test_name).to eql 1
41
+ end
42
+
43
+ it 'should snakeCase attribute writes' do
44
+ subject.test_name = 1
45
+ expect(subject.schema.testName).to eql 1
46
+ end
47
+
48
+ it 'should handle array arguments' do
49
+ subject.test_list = [1, 2, 3]
50
+ expect(subject.schema.testList).to eql [1, 2, 3]
51
+ end
52
+
53
+ it 'should handle array argument lists' do
54
+ subject.test_list 1, 2, 3
55
+ expect(subject.schema.testList).to eql [1, 2, 3]
56
+ end
57
+
58
+ it 'should handle arbitrary key writes' do
59
+ subject.test_as 1
60
+ expect(subject.schema.testAs).to be_nil
61
+ expect(subject.schema.testOther).to eql 1
62
+ end
63
+
64
+ it 'should handle arbitrary key reads' do
65
+ subject.schema.testOther = 1
66
+ expect(subject.test_as).to eql 1
67
+ end
68
+ end
69
+
70
+ describe '#initialize' do
71
+ include_context 'an entity with a parent'
72
+
73
+ its(:name){ is_expected.to eql 'name' }
74
+ its(:title){ is_expected.to eql 'test' }
75
+ its(:parent){ is_expected.to eql parent }
76
+ its('schema.evaluated_block'){ is_expected.to be true }
77
+ its('parent.children'){ is_expected.to include subject }
78
+ end
79
+
80
+ describe '#required=' do
81
+ include_context 'an entity with a parent'
82
+
83
+ it 'should delegate to the parent entity' do
84
+ expect{
85
+ subject.required = 'name'
86
+ }.to change{
87
+ parent.required
88
+ }.to ['name']
89
+ end
90
+ end
91
+
92
+ describe '#merge_children!' do
93
+ include_context 'an entity with a parent'
94
+
95
+ let(:child_schema){ double 'JSON::SchemaBuilder::Schema' }
96
+ let(:child){ double 'JSON::SchemaBuilder::Entity', schema: child_schema }
97
+
98
+ it 'should merge children schemas' do
99
+ expect(child).to receive :schema
100
+ expect(subject).to receive(:children).and_return [child]
101
+ expect(subject.schema).to receive(:merge!).with child_schema
102
+ subject.merge_children!
103
+ end
104
+ end
105
+
106
+ describe '#as_json' do
107
+ include_context 'an entity'
108
+
109
+ it 'should delegate to schema' do
110
+ expect(subject.schema).to receive_message_chain :'to_h.as_json'
111
+ subject.as_json
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JSON::SchemaBuilder::Integer, type: :unit do
4
+ subject{ described_class }
5
+ it_behaves_like 'a numeric entity'
6
+ its(:registered_type){ is_expected.to eql :integer }
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JSON::SchemaBuilder::Null, type: :unit do
4
+ subject{ described_class }
5
+ its(:registered_type){ is_expected.to eql :null }
6
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JSON::SchemaBuilder::Number, type: :unit do
4
+ subject{ described_class }
5
+ it_behaves_like 'a numeric entity'
6
+ its(:registered_type){ is_expected.to eql :number }
7
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JSON::SchemaBuilder::Object, type: :unit do
4
+ subject{ described_class }
5
+
6
+ it{ is_expected.to define_attribute :required }
7
+ it{ is_expected.to define_attribute :min_properties }
8
+ it{ is_expected.to define_attribute :max_properties }
9
+ it{ is_expected.to define_attribute :properties }
10
+ it{ is_expected.to define_attribute :additional_properties }
11
+ it{ is_expected.to define_attribute :pattern_properties }
12
+ its(:registered_type){ is_expected.to eql :object }
13
+
14
+ describe '#initialize' do
15
+ subject do
16
+ described_class.new 'name' do
17
+ string :test1
18
+ string :test2
19
+ end
20
+ end
21
+
22
+ let(:string){ kind_of JSON::SchemaBuilder::String }
23
+ its(:children){ is_expected.to include string }
24
+ its('children.length'){ is_expected.to eql 2 }
25
+ its('properties'){ is_expected.to include test1: { 'type' => 'string' } }
26
+ its('properties'){ is_expected.to include test2: { 'type' => 'string' } }
27
+ end
28
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JSON::SchemaBuilder::Schema, type: :unit do
4
+ let(:schema){ described_class.new a: 1, b: { c: 3 } }
5
+ let(:other){ described_class.new a: 2, b: { d: 4 } }
6
+ it{ is_expected.to be_a OpenStruct }
7
+
8
+ describe '#merge' do
9
+ it 'should deep merge' do
10
+ merged = schema.merge other
11
+ expect(merged).to be_a described_class
12
+ expect(merged.to_h).to eql a: 2, b: { c: 3, d: 4 }
13
+ end
14
+
15
+ it 'should not modify the source schema' do
16
+ expect{ schema.merge other }.to_not change{ schema.to_h }
17
+ end
18
+
19
+ it 'should not modify the merging schema' do
20
+ expect{ schema.merge other }.to_not change{ other.to_h }
21
+ end
22
+ end
23
+
24
+ describe '#merge!' do
25
+ it 'should deep merge in place' do
26
+ merged = schema.merge! other
27
+ expect(merged).to be_a described_class
28
+ expect(merged.to_h).to eql a: 2, b: { c: 3, d: 4 }
29
+ end
30
+
31
+ it 'should not modify the merging schema' do
32
+ expect{ schema.merge! other }.to_not change { other.to_h }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe JSON::SchemaBuilder::String, type: :unit do
4
+ subject{ described_class }
5
+
6
+ it{ is_expected.to define_attribute :min_length }
7
+ it{ is_expected.to define_attribute :max_length }
8
+ it{ is_expected.to define_attribute :pattern }
9
+ its(:registered_type){ is_expected.to eql :string }
10
+ end
metadata ADDED
@@ -0,0 +1,230 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json-schema_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Parrish
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-its
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codeclimate-test-reporter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
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'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.0'
104
+ - - '='
105
+ - !ruby/object:Gem::Version
106
+ version: 4.2.0.rc2
107
+ type: :runtime
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: '4.0'
114
+ - - '='
115
+ - !ruby/object:Gem::Version
116
+ version: 4.2.0.rc2
117
+ description: Build JSON schemas with Ruby
118
+ email:
119
+ - michael@zooniverse.org
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".gitignore"
125
+ - ".rspec"
126
+ - ".travis.yml"
127
+ - Gemfile
128
+ - Gemfile.lock
129
+ - LICENSE.txt
130
+ - README.md
131
+ - Rakefile
132
+ - json-schema_builder.gemspec
133
+ - lib/json/schema_builder.rb
134
+ - lib/json/schema_builder/array.rb
135
+ - lib/json/schema_builder/boolean.rb
136
+ - lib/json/schema_builder/dsl.rb
137
+ - lib/json/schema_builder/entity.rb
138
+ - lib/json/schema_builder/integer.rb
139
+ - lib/json/schema_builder/null.rb
140
+ - lib/json/schema_builder/number.rb
141
+ - lib/json/schema_builder/numeric.rb
142
+ - lib/json/schema_builder/object.rb
143
+ - lib/json/schema_builder/schema.rb
144
+ - lib/json/schema_builder/string.rb
145
+ - lib/json/schema_builder/version.rb
146
+ - spec/integration/entity_literals_spec.rb
147
+ - spec/integration/mixed_arrays_spec.rb
148
+ - spec/integration/schema_builder_spec.rb
149
+ - spec/integration/terse_arrays_spec.rb
150
+ - spec/integration/terse_objects_spec.rb
151
+ - spec/integration/verbose_arrays_spec.rb
152
+ - spec/integration/verbose_objects_spec.rb
153
+ - spec/spec_helper.rb
154
+ - spec/support/.keep
155
+ - spec/support/attribute_matcher.rb
156
+ - spec/support/examples/entity_literals.rb
157
+ - spec/support/examples/mixed_arrays.rb
158
+ - spec/support/examples/schema_builder.rb
159
+ - spec/support/examples/terse_arrays.rb
160
+ - spec/support/examples/terse_objects.rb
161
+ - spec/support/examples/verbose_arrays.rb
162
+ - spec/support/examples/verbose_objects.rb
163
+ - spec/support/integration_helper.rb
164
+ - spec/support/shared_contexts_for_entity.rb
165
+ - spec/support/shared_examples_for_numeric.rb
166
+ - spec/unit/array_spec.rb
167
+ - spec/unit/boolean_spec.rb
168
+ - spec/unit/dsl_spec.rb
169
+ - spec/unit/entity_spec.rb
170
+ - spec/unit/integer_spec.rb
171
+ - spec/unit/null_spec.rb
172
+ - spec/unit/number_spec.rb
173
+ - spec/unit/object_spec.rb
174
+ - spec/unit/schema_spec.rb
175
+ - spec/unit/string_spec.rb
176
+ homepage: https://github.com/parrish/json-schema_builder
177
+ licenses:
178
+ - MIT
179
+ metadata: {}
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ required_rubygems_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ requirements: []
195
+ rubyforge_project:
196
+ rubygems_version: 2.4.3
197
+ signing_key:
198
+ specification_version: 4
199
+ summary: Build JSON schemas with Ruby
200
+ test_files:
201
+ - spec/integration/entity_literals_spec.rb
202
+ - spec/integration/mixed_arrays_spec.rb
203
+ - spec/integration/schema_builder_spec.rb
204
+ - spec/integration/terse_arrays_spec.rb
205
+ - spec/integration/terse_objects_spec.rb
206
+ - spec/integration/verbose_arrays_spec.rb
207
+ - spec/integration/verbose_objects_spec.rb
208
+ - spec/spec_helper.rb
209
+ - spec/support/.keep
210
+ - spec/support/attribute_matcher.rb
211
+ - spec/support/examples/entity_literals.rb
212
+ - spec/support/examples/mixed_arrays.rb
213
+ - spec/support/examples/schema_builder.rb
214
+ - spec/support/examples/terse_arrays.rb
215
+ - spec/support/examples/terse_objects.rb
216
+ - spec/support/examples/verbose_arrays.rb
217
+ - spec/support/examples/verbose_objects.rb
218
+ - spec/support/integration_helper.rb
219
+ - spec/support/shared_contexts_for_entity.rb
220
+ - spec/support/shared_examples_for_numeric.rb
221
+ - spec/unit/array_spec.rb
222
+ - spec/unit/boolean_spec.rb
223
+ - spec/unit/dsl_spec.rb
224
+ - spec/unit/entity_spec.rb
225
+ - spec/unit/integer_spec.rb
226
+ - spec/unit/null_spec.rb
227
+ - spec/unit/number_spec.rb
228
+ - spec/unit/object_spec.rb
229
+ - spec/unit/schema_spec.rb
230
+ - spec/unit/string_spec.rb