json-schema_builder 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/json/schema_builder.rb +16 -0
- data/lib/json/schema_builder/rspec.rb +3 -0
- data/lib/json/schema_builder/rspec_helper/deep_open_struct.rb +38 -0
- data/lib/json/schema_builder/rspec_helper/rspec_helper.rb +31 -0
- data/lib/json/schema_builder/version.rb +1 -1
- data/spec/integration/context_delegation_spec.rb +34 -30
- data/spec/integration/rooted_spec.rb +42 -0
- data/spec/integration/verbose_objects_spec.rb +16 -24
- data/spec/spec_helper.rb +1 -0
- data/spec/support/examples/context_delegation.rb +8 -0
- data/spec/support/examples/rooted.rb +19 -0
- data/spec/support/integration_helper.rb +2 -1
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f5b78c7099bde185d4a86c992f62027591989dc
|
4
|
+
data.tar.gz: 62ab154f7d358f6192203cd46859d13bbdc92c9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c12f9f064e511bf3b49ff204202be34464ea51050758f58d7ad841adf1f6f336ed9aae5f80b50f9b587273bd05878b1762e8dff12679803ff5f1901c333055f5
|
7
|
+
data.tar.gz: 4a410940b32b0b3b87560f3932b64e744ce792b016328a3614a9456ccd1152e7014b14371639485036045289d8cfb90da966b052aefaa6b7a8b650f2ff8eadfd
|
data/Gemfile.lock
CHANGED
data/lib/json/schema_builder.rb
CHANGED
@@ -16,6 +16,9 @@ module JSON
|
|
16
16
|
|
17
17
|
included do |klass|
|
18
18
|
extend JSON::SchemaBuilder::Configuration
|
19
|
+
class << self
|
20
|
+
attr_accessor :root_key
|
21
|
+
end
|
19
22
|
end
|
20
23
|
|
21
24
|
def self.default_options
|
@@ -27,5 +30,18 @@ module JSON
|
|
27
30
|
instance_variable_set "@#{ key }", value
|
28
31
|
end
|
29
32
|
end
|
33
|
+
|
34
|
+
def root(key = nil, &block)
|
35
|
+
root_key = key || self.class.root_key.to_sym
|
36
|
+
object do
|
37
|
+
object root_key, required: true, &block
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module ClassMethods
|
42
|
+
def root(key)
|
43
|
+
@root_key = key
|
44
|
+
end
|
45
|
+
end
|
30
46
|
end
|
31
47
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module JSON
|
2
|
+
module SchemaBuilder
|
3
|
+
module RSpecHelper
|
4
|
+
class DeepOpenStruct < OpenStruct
|
5
|
+
def initialize(hash = { })
|
6
|
+
@table = { }
|
7
|
+
hash.each_pair do |key, value|
|
8
|
+
key = key.to_sym
|
9
|
+
@table[key] = _transform value
|
10
|
+
new_ostruct_member key
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def ==(other)
|
15
|
+
to_h == other.to_h
|
16
|
+
end
|
17
|
+
alias_method :eql?, :==
|
18
|
+
|
19
|
+
def inspect
|
20
|
+
to_h.inspect
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def _transform(object)
|
26
|
+
case object
|
27
|
+
when Hash
|
28
|
+
DeepOpenStruct.new object
|
29
|
+
when Array
|
30
|
+
object.map{ |item| _transform item }
|
31
|
+
else
|
32
|
+
object
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module JSON
|
2
|
+
module SchemaBuilder
|
3
|
+
module RSpecHelper
|
4
|
+
extend ::RSpec::SharedContext
|
5
|
+
let(:schema_method){ raise 'undefined schema method' }
|
6
|
+
let(:schema_context){ { } }
|
7
|
+
let(:schema){ described_class.new(schema_context).send schema_method }
|
8
|
+
let(:json){ schema.as_json }
|
9
|
+
subject{ DeepOpenStruct.new json }
|
10
|
+
|
11
|
+
def self.included(klass)
|
12
|
+
super klass
|
13
|
+
klass.send :extend, ClassMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def with(key, &block)
|
18
|
+
describe ".#{ key }" do
|
19
|
+
eval "def subject; super.#{ key }; end"
|
20
|
+
|
21
|
+
it 'should return an entity' do
|
22
|
+
expect(schema).to be_a JSON::SchemaBuilder::Entity
|
23
|
+
end
|
24
|
+
|
25
|
+
instance_exec &block
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -1,43 +1,47 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Examples::ContextDelegation, type: :integration do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
{
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
[:example, :alternate_example].each do |method_name|
|
5
|
+
context 'with an admin' do
|
6
|
+
it_behaves_like 'a builder' do
|
7
|
+
let(:schema_method){ method_name }
|
8
|
+
let(:schema_context) do
|
9
|
+
{
|
10
|
+
user: Examples::ContextDelegation::User.new(role: :admin)
|
11
|
+
}
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
let(:expected_json) do
|
15
|
+
{
|
16
|
+
type: :object,
|
17
|
+
additionalProperties: false,
|
18
|
+
properties: {
|
19
|
+
name: { type: :string },
|
20
|
+
role: { type: :string }
|
21
|
+
}
|
19
22
|
}
|
20
|
-
|
23
|
+
end
|
21
24
|
end
|
22
25
|
end
|
23
|
-
end
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
context 'with a user' do
|
28
|
+
it_behaves_like 'a builder' do
|
29
|
+
let(:schema_method){ method_name }
|
30
|
+
let(:schema_context) do
|
31
|
+
{
|
32
|
+
user: Examples::ContextDelegation::User.new(role: :user)
|
33
|
+
}
|
34
|
+
end
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
let(:expected_json) do
|
37
|
+
{
|
38
|
+
type: :object,
|
39
|
+
additionalProperties: false,
|
40
|
+
properties: {
|
41
|
+
name: { type: :string }
|
42
|
+
}
|
39
43
|
}
|
40
|
-
|
44
|
+
end
|
41
45
|
end
|
42
46
|
end
|
43
47
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Examples::Rooted, type: :integration do
|
4
|
+
context 'with a root key' do
|
5
|
+
it_behaves_like 'a builder' do
|
6
|
+
let(:expected_json) do
|
7
|
+
{
|
8
|
+
type: :object,
|
9
|
+
required: [:rooted],
|
10
|
+
properties: {
|
11
|
+
rooted: {
|
12
|
+
type: :object,
|
13
|
+
properties: {
|
14
|
+
name: { type: :string }
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
18
|
+
}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'with an alternate root key' do
|
24
|
+
it_behaves_like 'a builder' do
|
25
|
+
let(:schema_method){ :alternate_root }
|
26
|
+
let(:expected_json) do
|
27
|
+
{
|
28
|
+
type: :object,
|
29
|
+
required: [:other_root],
|
30
|
+
properties: {
|
31
|
+
other_root: {
|
32
|
+
type: :object,
|
33
|
+
properties: {
|
34
|
+
name: { type: :string }
|
35
|
+
}
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -1,30 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Examples::VerboseObjects, type: :integration do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
id: {
|
21
|
-
type: :integer
|
22
|
-
}
|
23
|
-
}
|
24
|
-
}
|
25
|
-
}
|
26
|
-
}
|
27
|
-
}
|
4
|
+
include JSON::SchemaBuilder::RSpecHelper
|
5
|
+
let(:schema_method){ :example }
|
6
|
+
its(:type){ is_expected.to eql 'object' }
|
7
|
+
|
8
|
+
with :properties do
|
9
|
+
its(:name){ is_expected.to eql type: 'string', minLength: 1 }
|
10
|
+
|
11
|
+
with :ids do
|
12
|
+
its(:type){ is_expected.to eql 'array' }
|
13
|
+
its(:minItems){ is_expected.to eql 1 }
|
14
|
+
|
15
|
+
with :items do
|
16
|
+
its(:type){ is_expected.to eql 'object' }
|
17
|
+
its(:required){ is_expected.to eql ['id'] }
|
18
|
+
its('properties.id.type'){ is_expected.to eql 'integer' }
|
19
|
+
end
|
28
20
|
end
|
29
21
|
end
|
30
22
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Examples
|
2
2
|
class ContextDelegation
|
3
3
|
include JSON::SchemaBuilder
|
4
|
+
attr_accessor :user
|
4
5
|
delegate :admin?, to: :@user
|
5
6
|
|
6
7
|
def example
|
@@ -10,6 +11,13 @@ module Examples
|
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
14
|
+
def alternate_example
|
15
|
+
object additional_properties: false do
|
16
|
+
string :name
|
17
|
+
string :role if user.admin?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
13
21
|
class User < OpenStruct
|
14
22
|
def admin?
|
15
23
|
role == :admin
|
@@ -1,7 +1,8 @@
|
|
1
1
|
module IntegrationHelper
|
2
2
|
extend RSpec::SharedContext
|
3
3
|
let(:schema_context){ { } }
|
4
|
-
let(:
|
4
|
+
let(:schema_method){ :example }
|
5
|
+
let(:schema){ described_class.new(schema_context).send schema_method }
|
5
6
|
let(:json){ schema.as_json }
|
6
7
|
let(:expected_json){ { } }
|
7
8
|
|
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.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Parrish
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -156,6 +156,9 @@ files:
|
|
156
156
|
- lib/json/schema_builder/number.rb
|
157
157
|
- lib/json/schema_builder/numeric.rb
|
158
158
|
- lib/json/schema_builder/object.rb
|
159
|
+
- lib/json/schema_builder/rspec.rb
|
160
|
+
- lib/json/schema_builder/rspec_helper/deep_open_struct.rb
|
161
|
+
- lib/json/schema_builder/rspec_helper/rspec_helper.rb
|
159
162
|
- lib/json/schema_builder/schema.rb
|
160
163
|
- lib/json/schema_builder/string.rb
|
161
164
|
- lib/json/schema_builder/validation.rb
|
@@ -166,6 +169,7 @@ files:
|
|
166
169
|
- spec/integration/mixed_arrays_spec.rb
|
167
170
|
- spec/integration/mixed_objects_spec.rb
|
168
171
|
- spec/integration/object_definitions_spec.rb
|
172
|
+
- spec/integration/rooted_spec.rb
|
169
173
|
- spec/integration/schema_builder_spec.rb
|
170
174
|
- spec/integration/terse_arrays_spec.rb
|
171
175
|
- spec/integration/terse_objects_spec.rb
|
@@ -180,6 +184,7 @@ files:
|
|
180
184
|
- spec/support/examples/mixed_arrays.rb
|
181
185
|
- spec/support/examples/mixed_objects.rb
|
182
186
|
- spec/support/examples/object_definitions.rb
|
187
|
+
- spec/support/examples/rooted.rb
|
183
188
|
- spec/support/examples/schema_builder.rb
|
184
189
|
- spec/support/examples/terse_arrays.rb
|
185
190
|
- spec/support/examples/terse_objects.rb
|
@@ -229,6 +234,7 @@ test_files:
|
|
229
234
|
- spec/integration/mixed_arrays_spec.rb
|
230
235
|
- spec/integration/mixed_objects_spec.rb
|
231
236
|
- spec/integration/object_definitions_spec.rb
|
237
|
+
- spec/integration/rooted_spec.rb
|
232
238
|
- spec/integration/schema_builder_spec.rb
|
233
239
|
- spec/integration/terse_arrays_spec.rb
|
234
240
|
- spec/integration/terse_objects_spec.rb
|
@@ -243,6 +249,7 @@ test_files:
|
|
243
249
|
- spec/support/examples/mixed_arrays.rb
|
244
250
|
- spec/support/examples/mixed_objects.rb
|
245
251
|
- spec/support/examples/object_definitions.rb
|
252
|
+
- spec/support/examples/rooted.rb
|
246
253
|
- spec/support/examples/schema_builder.rb
|
247
254
|
- spec/support/examples/terse_arrays.rb
|
248
255
|
- spec/support/examples/terse_objects.rb
|