json_schema-faker 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/VERSION +1 -1
- data/lib/json_schema/faker.rb +1 -1
- data/lib/json_schema/faker/strategy/simple.rb +42 -12
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca9c68a89b0e12cd2c9add27620a0e27a612bc98
|
4
|
+
data.tar.gz: bd0f0df87c65b0a6d4d75e25bd73567c4adcf25a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f808e04df1769456412a40050173939a44bd2c7b2ec76cf6744811eea885b98f892dd408ef6a3aec7833f58f7067d4b5441c8a5f83f92958d2c1456f3dc461f
|
7
|
+
data.tar.gz: c510fd256a59d6be0f120c7e740692a056336c2121d49ede8cdf498b45913e2dc9f39844d0c64dab89fa0e943f281a50d7ba8256a64f9e04dcc52ce7469e69cb
|
data/CHANGELOG.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/json_schema/faker.rb
CHANGED
@@ -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:
|
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
|
-
|
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
|
-
|
38
|
-
hash[key] = generate(schema.properties[key], hint: hint, position: "#{position}/#{key}")
|
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 -=
|
64
|
+
keys = (schema.properties || {}).keys - object.keys
|
65
|
+
keys -= hint[:not_have_keys] if hint && hint[:not_have_keys]
|
45
66
|
|
46
|
-
|
47
|
-
hash[key] = generate(schema.properties[key], hint: hint, position: "#{position}/#{key}")
|
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
|
97
|
-
|
98
|
-
|
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
|