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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61fb32a305dd4fdda20ea2a77787c185ff963fe4
4
- data.tar.gz: 21305c280d00e355f899a99bdea50a69e40871b0
3
+ metadata.gz: 9c87d49db81067b8235d03f460077e16c9814a11
4
+ data.tar.gz: 2e2131f2ad7437e560b9d649acd0e0d7b3298ada
5
5
  SHA512:
6
- metadata.gz: 9ea8a4f73760b9b45adb79243d0c23c6c53af6eaf527abab84b6471bbc0cc8112b61e2ab6433544c5ef84e7e994f351e7663c07565c8fa0feb570ccbc2d84bfc
7
- data.tar.gz: c96b0c7d2fe7f7297d627513985fa958e1e813696f75a419d313eadf83cdab93b7afcea0b0a96c7310859dd3c665fd69117ff707af69b2268c1c7d5916be2735
6
+ metadata.gz: 5fdaf374bfeac077a0e6e001b6ed4bc9c012f84952ed75807ee8119f2f7b05c16675219885e124499f180a6f827218b4d064d50c1e4d31508c03ff7d13f064bd
7
+ data.tar.gz: 9309777d164ec18f1f564bb20d7c7060a56b85d355699620374156fca8c24818789b7e8385989b35ec1ce41db9ea17cb52fecb7ad68bf29f01cc3d69889cfb52
@@ -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.2"
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.}
@@ -29,9 +29,10 @@ module JsonSchemaSpec
29
29
  end
30
30
 
31
31
  def json_schema_params(resource, method, merge={})
32
- schema = json_schema(resource, method)
33
- params = json_schema_to_params(schema)
34
- params = Util.deep_merge(params, merge)
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, prefix=[])
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, prefix.dup)
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, prefix)
55
- if !value.is_a?(Hash) || value[:optional] || value[:type] == 'null'
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], prefix) ]
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
- json_schema_to_params(value[:properties], prefix << key)
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
 
@@ -26,6 +26,9 @@ user.json:
26
26
  avatar:
27
27
  type: string
28
28
  optional: true
29
+ bio:
30
+ type: string
31
+ optional: true
29
32
  name:
30
33
  type: string
31
34
  company:
@@ -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.2
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 00:00:00.000000000 Z
11
+ date: 2014-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json