json_schema-faker 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/VERSION +1 -1
- data/lib/json_schema/faker/strategy/greedy.rb +70 -0
- data/lib/json_schema/faker/strategy/simple.rb +4 -4
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fe0bd03f8a005b3e787b728ce7287db41612d36
|
4
|
+
data.tar.gz: ab3c402bb39c68ee5c3c9102b5dcbc5dccc370d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cfdfcb77c087e913d4d555a6793a948a847f7dae5b4fe9722cc3f844a10a1f5f1b6b14b54ef20db7909faef0e59263bc71e32f0aba1d95af0f5f23f1c8005c7
|
7
|
+
data.tar.gz: 25b74a451865ab813ed00c2575fe8b43eab4775a6503dbf984baefbb4a794236c828d5a2e0b620115cc8af055e5c2caa470b3e0f91643e48eadea7d6fe787bed
|
data/CHANGELOG.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "json_schema/faker/strategy/simple"
|
2
|
+
|
3
|
+
module JsonSchema::Faker::Strategy
|
4
|
+
class Greedy < Simple
|
5
|
+
def generate_for_object(schema, hint: nil, position:)
|
6
|
+
# complete hint
|
7
|
+
object = if hint && hint[:example] && hint[:example].is_a?(Hash)
|
8
|
+
hint[:example].each.with_object({}) do |(key, value), hash|
|
9
|
+
if value.is_a?(Hash)
|
10
|
+
if schema.properties.has_key?(key)
|
11
|
+
hash[key] = generate(schema.properties[key], hint: { example: hint[:example][key] }, position: "#{position}/#{key}")
|
12
|
+
else
|
13
|
+
# TODO: support pattern properties
|
14
|
+
hash[key] = value
|
15
|
+
end
|
16
|
+
else
|
17
|
+
hash[key] = value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
else
|
21
|
+
{}
|
22
|
+
end
|
23
|
+
|
24
|
+
# http://json-schema.org/latest/json-schema-validation.html#anchor53
|
25
|
+
required_length = schema.max_properties || [ schema.properties.size, (schema.min_properties || 0) ].max
|
26
|
+
|
27
|
+
keys = ((schema.required || [])+ (schema.properties || {}).keys).uniq - object.keys
|
28
|
+
keys -= hint[:not_have_keys] if hint && hint[:not_have_keys]
|
29
|
+
|
30
|
+
keys.first(required_length).each.with_object(object) do |key, hash|
|
31
|
+
hash[key] = generate(schema.properties[key], hint: hint, position: "#{position}/#{key}")
|
32
|
+
end
|
33
|
+
|
34
|
+
# if length is not enough
|
35
|
+
if schema.additional_properties === false
|
36
|
+
(required_length - object.keys.length).times.each.with_object(object) do |i, hash|
|
37
|
+
if schema.pattern_properties.empty?
|
38
|
+
key = (schema.properties.keys - object.keys).first
|
39
|
+
hash[key] = generate(schema.properties[key], hint: hint, position: "#{position}/#{key}")
|
40
|
+
else
|
41
|
+
name = ::Pxeger.new(schema.pattern_properties.keys.first).generate
|
42
|
+
hash[name] = generate(schema.pattern_properties.values.first, hint: hint, position: "#{position}/#{name}")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
else
|
46
|
+
# FIXME: key confilct with properties
|
47
|
+
(required_length - object.keys.length).times.each.with_object(object) do |i, hash|
|
48
|
+
hash[i.to_s] = i
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# consider dependency
|
53
|
+
depended_keys = object.keys & schema.dependencies.keys
|
54
|
+
|
55
|
+
# FIXME: circular dependency is not supported
|
56
|
+
depended_keys.each.with_object(object) do |key, hash|
|
57
|
+
dependency = schema.dependencies[key]
|
58
|
+
|
59
|
+
if dependency.is_a?(::JsonSchema::Schema)
|
60
|
+
# too difficult we just merge
|
61
|
+
hash.update(generate(schema.dependencies[key], hint: nil, position: "#{position}/dependencies/#{key}"))
|
62
|
+
else
|
63
|
+
dependency.each do |additional_key|
|
64
|
+
object[additional_key] = generate(schema.properties[additional_key], hint: hint, position: "#{position}/dependencies/#{key}/#{additional_key}") unless object.has_key?(additional_key)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -144,14 +144,14 @@ module JsonSchema::Faker::Strategy
|
|
144
144
|
|
145
145
|
true
|
146
146
|
when "integer", "number"
|
147
|
-
generate_for_number(schema, hint: hint)
|
147
|
+
generate_for_number(schema, hint: hint, position: position)
|
148
148
|
when "null"
|
149
149
|
nil
|
150
150
|
when "object", nil
|
151
151
|
# here comes object without properties
|
152
152
|
generate_for_object(schema, hint: hint, position: position)
|
153
153
|
when "string"
|
154
|
-
generate_for_string(schema, hint: hint)
|
154
|
+
generate_for_string(schema, hint: hint, position: position)
|
155
155
|
else
|
156
156
|
raise "unknown type for #{schema.inspect_schema}"
|
157
157
|
end
|
@@ -178,7 +178,7 @@ module JsonSchema::Faker::Strategy
|
|
178
178
|
end
|
179
179
|
end
|
180
180
|
|
181
|
-
def generate_for_number(schema, hint: nil)
|
181
|
+
def generate_for_number(schema, hint: nil, position:)
|
182
182
|
return hint[:example] if hint && hint[:example] && hint[:example].is_a?(Numeric)
|
183
183
|
|
184
184
|
# http://json-schema.org/latest/json-schema-validation.html#anchor13
|
@@ -204,7 +204,7 @@ module JsonSchema::Faker::Strategy
|
|
204
204
|
end
|
205
205
|
end
|
206
206
|
|
207
|
-
def generate_for_string(schema, hint: nil)
|
207
|
+
def generate_for_string(schema, hint: nil, position:)
|
208
208
|
return hint[:example] if hint && hint[:example] && hint[:example].is_a?(String)
|
209
209
|
|
210
210
|
# http://json-schema.org/latest/json-schema-validation.html#anchor25
|
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.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- okitan
|
@@ -131,6 +131,7 @@ files:
|
|
131
131
|
- bin/setup
|
132
132
|
- json_schema-faker.gemspec
|
133
133
|
- lib/json_schema/faker.rb
|
134
|
+
- lib/json_schema/faker/strategy/greedy.rb
|
134
135
|
- lib/json_schema/faker/strategy/simple.rb
|
135
136
|
homepage: https://github.com/okitan/json_schema-faker
|
136
137
|
licenses:
|