json_schema-faker 0.1.1 → 0.2.0

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: 6d295d3dfb065608b9af8b1bfcd4336ea16a7de1
4
- data.tar.gz: 08969d39f4b51f7c4fc07fd0c7ec3cb9408d3995
3
+ metadata.gz: ca9c68a89b0e12cd2c9add27620a0e27a612bc98
4
+ data.tar.gz: bd0f0df87c65b0a6d4d75e25bd73567c4adcf25a
5
5
  SHA512:
6
- metadata.gz: 9a3360dae5813be93b1c4aa5ac9cdd04c5c1e7c5151d90529811d4e5530c48a022c187967a2d1e92bd5d395c3660c2978dedcabbd46114d7d37d7dc3823698e3
7
- data.tar.gz: b07858fa1b121b06e58d3dd27342a76e2cc03e27fde3137ffd73d6a50c1ec15a9bf0c8cb9289795d08cd4f525ae86800022a1cfce2c456478f954bef5881d312
6
+ metadata.gz: 1f808e04df1769456412a40050173939a44bd2c7b2ec76cf6744811eea885b98f892dd408ef6a3aec7833f58f7067d4b5441c8a5f83f92958d2c1456f3dc461f
7
+ data.tar.gz: c510fd256a59d6be0f120c7e740692a056336c2121d49ede8cdf498b45913e2dc9f39844d0c64dab89fa0e943f281a50d7ba8256a64f9e04dcc52ce7469e69cb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.2.0
2
+
3
+ * ENHANCEMENT
4
+ * support example as hint
5
+
1
6
  ## 0.1.1
2
7
 
3
8
  * IMPROVEMENT
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -24,7 +24,7 @@ module JsonSchema
24
24
 
25
25
  Configuration.logger.debug "to generate against #{@schema.inspect_schema}" if Configuration.logger
26
26
 
27
- generated = strategy.call(@schema, hint: nil, position: "")
27
+ generated = strategy.call(@schema, hint: hint, position: "")
28
28
  Configuration.logger.debug "generated: #{generated.inspect}" if Configuration.logger
29
29
 
30
30
  generated
@@ -1,7 +1,10 @@
1
1
  module JsonSchema::Faker::Strategy
2
2
  class Simple
3
3
  def call(schema, hint: nil, position:)
4
- ::JsonSchema::Faker::Configuration.logger.debug "current position: #{position}" if ::JsonSchema::Faker::Configuration.logger
4
+ if ::JsonSchema::Faker::Configuration.logger
5
+ ::JsonSchema::Faker::Configuration.logger.debug "current position: #{position}"
6
+ ::JsonSchema::Faker::Configuration.logger.debug "current hint: #{hint.inspect}"
7
+ end
5
8
 
6
9
  raise "here comes nil for schema at #{position}" unless schema
7
10
 
@@ -29,22 +32,40 @@ module JsonSchema::Faker::Strategy
29
32
  alias_method :generate, :call
30
33
 
31
34
  def generate_for_object(schema, hint: nil, position:)
35
+ # complete hint
36
+ object = if hint && hint[:example] && hint[:example].is_a?(Hash)
37
+ hint[:example].each.with_object({}) do |(key, value), hash|
38
+ if value.is_a?(Hash)
39
+ if schema.properties.has_key?(key)
40
+ hash[key] = generate(schema.properties[key], hint: { example: hint[:example][key] }, position: "#{position}/#{key}")
41
+ else
42
+ # TODO: support pattern properties
43
+ hash[key] = value
44
+ end
45
+ else
46
+ hash[key] = value
47
+ end
48
+ end
49
+ else
50
+ {}
51
+ end
52
+
32
53
  # http://json-schema.org/latest/json-schema-validation.html#anchor53
33
54
  if schema.required
34
55
  keys = schema.required
35
56
  required_length = schema.min_properties || keys.length
36
57
 
37
- object = keys.each.with_object({}) do |key, hash|
38
- hash[key] = generate(schema.properties[key], hint: hint, position: "#{position}/#{key}") # TODO: pass hint
58
+ (keys - object.keys).each.with_object(object) do |key, hash|
59
+ hash[key] = generate(schema.properties[key], hint: hint, position: "#{position}/#{key}")
39
60
  end
40
61
  else
41
62
  required_length = schema.min_properties || schema.max_properties || 0
42
63
 
43
- keys = (schema.properties || {}).keys
44
- keys -= (hint[:not_have_keys] || []) if hint
64
+ keys = (schema.properties || {}).keys - object.keys
65
+ keys -= hint[:not_have_keys] if hint && hint[:not_have_keys]
45
66
 
46
- object = keys.first(required_length).each.with_object({}) do |key, hash|
47
- hash[key] = generate(schema.properties[key], hint: hint, position: "#{position}/#{key}") # TODO: pass hint
67
+ keys.first(required_length).each.with_object(object) do |key, hash|
68
+ hash[key] = generate(schema.properties[key], hint: hint, position: "#{position}/#{key}")
48
69
  end
49
70
  end
50
71
 
@@ -86,18 +107,18 @@ module JsonSchema::Faker::Strategy
86
107
 
87
108
  def generate_by_enum(schema, hint: nil, position:)
88
109
  black_list = (hint ? hint[:not_be_values] : nil)
110
+ list = black_list ? schema.enum - black_list : schema.enum
89
111
 
90
112
  if ::JsonSchema::Faker::Configuration.logger
91
113
  ::JsonSchema::Faker::Configuration.logger.info "generate by enum at #{position}"
92
114
  ::JsonSchema::Faker::Configuration.logger.debug schema.inspect_schema
93
115
  ::JsonSchema::Faker::Configuration.logger.debug "black list: #{black_list}" if black_list
116
+ ::JsonSchema::Faker::Configuration.logger.debug "list: #{list}"
94
117
  end
95
118
 
96
- if black_list
97
- (schema.enum - black_list).first
98
- else
99
- schema.enum.first
100
- end
119
+ return hint[:example] if hint && hint[:example] && hint[:example] && list.include?(hint[:example])
120
+
121
+ list.first
101
122
  end
102
123
 
103
124
  def generate_by_type(schema, hint: nil, position:)
@@ -112,6 +133,8 @@ module JsonSchema::Faker::Strategy
112
133
  when "array"
113
134
  generate_for_array(schema, hint: hint, position: position)
114
135
  when "boolean"
136
+ return hint[:example] if hint && hint[:example] && (hint[:example] === true || hint[:example] === false)
137
+
115
138
  true
116
139
  when "integer", "number"
117
140
  generate_for_number(schema, hint: hint)
@@ -128,6 +151,9 @@ module JsonSchema::Faker::Strategy
128
151
  end
129
152
 
130
153
  def generate_for_array(schema, hint: nil, position:)
154
+ # completing each items is difficult
155
+ return hint[:example] if hint && hint[:example] && hint[:example].is_a?(Array)
156
+
131
157
  # http://json-schema.org/latest/json-schema-validation.html#anchor36
132
158
  # additionalItems items maxItems minItems uniqueItems
133
159
  length = schema.min_items || 0
@@ -146,6 +172,8 @@ module JsonSchema::Faker::Strategy
146
172
  end
147
173
 
148
174
  def generate_for_number(schema, hint: nil)
175
+ return hint[:example] if hint && hint[:example] && hint[:example].is_a?(Numeric)
176
+
149
177
  # http://json-schema.org/latest/json-schema-validation.html#anchor13
150
178
  # TODO: use hint[:not_be_values]
151
179
  min = schema.min
@@ -170,6 +198,8 @@ module JsonSchema::Faker::Strategy
170
198
  end
171
199
 
172
200
  def generate_for_string(schema, hint: nil)
201
+ return hint[:example] if hint && hint[:example] && hint[:example].is_a?(String)
202
+
173
203
  # http://json-schema.org/latest/json-schema-validation.html#anchor25
174
204
  # TODO: use hint[:not_be_values]
175
205
  # TODO: support format
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schema-faker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - okitan