json_schema_spec 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/json_schema_spec.gemspec +1 -1
- data/lib/json_schema_spec.rb +25 -9
- data/spec/fixtures/schema.yml +3 -0
- data/spec/json_schema_spec_spec.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c87d49db81067b8235d03f460077e16c9814a11
|
4
|
+
data.tar.gz: 2e2131f2ad7437e560b9d649acd0e0d7b3298ada
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5fdaf374bfeac077a0e6e001b6ed4bc9c012f84952ed75807ee8119f2f7b05c16675219885e124499f180a6f827218b4d064d50c1e4d31508c03ff7d13f064bd
|
7
|
+
data.tar.gz: 9309777d164ec18f1f564bb20d7c7060a56b85d355699620374156fca8c24818789b7e8385989b35ec1ce41db9ea17cb52fecb7ad68bf29f01cc3d69889cfb52
|
data/json_schema_spec.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "json_schema_spec"
|
7
|
-
spec.version = "0.1.
|
7
|
+
spec.version = "0.1.3"
|
8
8
|
spec.authors = ["Winton Welsh"]
|
9
9
|
spec.email = ["mail@wintoni.us"]
|
10
10
|
spec.description = %q{Generate fixtures from JSON schemas.}
|
data/lib/json_schema_spec.rb
CHANGED
@@ -29,9 +29,10 @@ module JsonSchemaSpec
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def json_schema_params(resource, method, merge={})
|
32
|
-
|
33
|
-
|
34
|
-
params
|
32
|
+
required = merge[:required]
|
33
|
+
schema = json_schema(resource, method)
|
34
|
+
params = json_schema_to_params(schema, required: required)
|
35
|
+
params = Util.deep_merge(params, merge)
|
35
36
|
|
36
37
|
unless merge.empty?
|
37
38
|
validate_json_schema(resource, method, params)
|
@@ -41,29 +42,36 @@ module JsonSchemaSpec
|
|
41
42
|
[ params[:request], params[:response] ]
|
42
43
|
end
|
43
44
|
|
44
|
-
def json_schema_to_params(schema,
|
45
|
+
def json_schema_to_params(schema, options={})
|
45
46
|
return schema unless schema.is_a?(Hash)
|
46
47
|
|
47
48
|
schema.inject({}) do |memo, (key, value)|
|
48
|
-
memo[key] = json_schema_value(key, value,
|
49
|
+
memo[key] = json_schema_value(key, value, options)
|
49
50
|
memo.delete(key) unless memo[key]
|
50
51
|
memo
|
51
52
|
end
|
52
53
|
end
|
53
54
|
|
54
|
-
def json_schema_value(key, value,
|
55
|
-
|
55
|
+
def json_schema_value(key, value, options={})
|
56
|
+
prefix = options[:prefix] || []
|
57
|
+
prefix = prefix.dup if prefix
|
58
|
+
|
59
|
+
required = options[:required] || []
|
60
|
+
required = [ required ].flatten if required
|
61
|
+
|
62
|
+
if return_nil?(key, value, required)
|
56
63
|
nil
|
57
64
|
elsif value[:enum]
|
58
65
|
value[:enum].shuffle.first
|
59
66
|
elsif value[:type] == 'array'
|
60
|
-
[ json_schema_value(key, value[:items],
|
67
|
+
[ json_schema_value(key, value[:items], options) ]
|
61
68
|
elsif value[:type] == 'boolean'
|
62
69
|
true
|
63
70
|
elsif value[:type] == 'integer'
|
64
71
|
rand(1_000_000)
|
65
72
|
elsif value[:type] == 'object'
|
66
|
-
|
73
|
+
prefix << key
|
74
|
+
json_schema_to_params(value[:properties], prefix: prefix, required: required)
|
67
75
|
elsif value[:type] == 'string'
|
68
76
|
json_schema_value_prefix(prefix) + key.to_s
|
69
77
|
else
|
@@ -93,6 +101,14 @@ module JsonSchemaSpec
|
|
93
101
|
end
|
94
102
|
end
|
95
103
|
|
104
|
+
def return_nil?(key, value, required)
|
105
|
+
!required.include?(key) && (
|
106
|
+
!value.is_a?(Hash) ||
|
107
|
+
value[:optional] ||
|
108
|
+
value[:type] == 'null'
|
109
|
+
)
|
110
|
+
end
|
111
|
+
|
96
112
|
def validate_json_schema(resource, method, params)
|
97
113
|
return if RUBY_VERSION =~ /^1\.8\./
|
98
114
|
|
data/spec/fixtures/schema.yml
CHANGED
@@ -4,7 +4,7 @@ describe JsonSchemaSpec do
|
|
4
4
|
describe "#json_schema_params" do
|
5
5
|
|
6
6
|
it "generates proper params" do
|
7
|
-
request, response = json_schema_params(:user, :get)
|
7
|
+
request, response = json_schema_params(:user, :get, :required => :bio)
|
8
8
|
|
9
9
|
expect(request).to eq(
|
10
10
|
:id => request[:id],
|
@@ -14,6 +14,7 @@ describe JsonSchemaSpec do
|
|
14
14
|
expect(response).to eq([{
|
15
15
|
:id => response[0][:id],
|
16
16
|
:admin => 0,
|
17
|
+
:bio => "bio",
|
17
18
|
:name => "name",
|
18
19
|
:company => {
|
19
20
|
:name => "company:name"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema_spec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Winton Welsh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|