json_schema-faker 0.5.2 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86004c55c41bf6565a1352415755ad096271acc1
4
- data.tar.gz: 7bd049df4f6b5fcf5d9d6b0eb9ac7e3903420690
3
+ metadata.gz: 7ebcb70af9b5de9a47ba9a6a8d539add9631e80e
4
+ data.tar.gz: 8c7d300c7562ef22fa6d29788f53dd464f742cde
5
5
  SHA512:
6
- metadata.gz: cf5aa0f7718587a6f509bb436b8c7830a9aeb8eab5ec8b7bbee1269ecb39641ea774a2d592fd0ddcaf9085ce1892e0accf7af81afda0a5150d8610e34d0ddc18
7
- data.tar.gz: e5ebca434d25c2ecae25f77f49a363bbe406bcecee211614674f760a495d22acef38195fc7e1747e1b9111ae23a9e9c0cce6e8651fa90064d48b8287b7ee6a26
6
+ metadata.gz: afea57d43a6f1862473597c5f4ec93ce9ae5a88f8abee2107b77b9d7c8c872544f098f0e4c2a62131e13ddcf8ebbd6de4a87c4657dcc3662e2ef83ad4f6228db
7
+ data.tar.gz: '09ee363bb4a24a32f8cd26f690335a0f25fe019b4e63ec6db4e311da07fe8e32a8370d6a13bc8e12c844c9d6e69d98b02fb2042e2873f874e4b40d2f071f6dec'
@@ -1,3 +1,9 @@
1
+ ## 0.6.0
2
+
3
+ * Added
4
+ * support of default faker against format
5
+ * support for copying format in take_logical_and_of_schema
6
+
1
7
  ## 0.5.2
2
8
 
3
9
  * Added
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 0.6.0
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
17
17
 
18
18
  spec.add_dependency "json_schema", ">= 0.12.4"
19
19
  spec.add_dependency "pxeger"
20
+ spec.add_dependency "faker"
20
21
 
21
22
  spec.add_development_dependency "bundler", "~> 1.12"
22
23
  spec.add_development_dependency "rake", "~> 10.0"
@@ -0,0 +1,65 @@
1
+ require "json_schema/faker"
2
+
3
+ require "faker"
4
+
5
+ require "date"
6
+
7
+ class JsonSchema::Faker
8
+ # Most format faker does not care other validations such as maxLength
9
+ module Formats
10
+ def date_time(schema, hint: nil, position: nil)
11
+ raise "invalid schema given" unless schema.format == "date-time"
12
+
13
+ (hint && hint[:example]) || ::DateTime.now.rfc3339
14
+ end
15
+
16
+ def email(schema, hint: nil, position: nil)
17
+ raise "invalid schema given" unless schema.format == "email"
18
+
19
+ (hint && hint[:example]) || ::Faker::Internet.safe_email
20
+ end
21
+
22
+ # https://tools.ietf.org/html/rfc2606
23
+ def hostname(schema, hint: nil, position: nil)
24
+ raise "invalid schema given" unless schema.format == "hostname"
25
+
26
+ (hint && hint[:example]) || safe_domain
27
+ end
28
+
29
+ # https://tools.ietf.org/html/rfc5737
30
+ def ipv4(schema, hint: nil, position: nil)
31
+ raise "invalid schema given" unless schema.format == "ipv4"
32
+
33
+ (hint && hint[:example]) || [
34
+ ->() { "192.0.2.#{(0..255).to_a.sample}" },
35
+ ->() { "198.51.100.#{(0..255).to_a.sample}" },
36
+ ->() { "203.0.113.#{(0..255).to_a.sample}" },
37
+ ].sample.call
38
+ end
39
+
40
+ # https://tools.ietf.org/html/rfc3849
41
+ def ipv6(schema, hint: nil, position: nil)
42
+ raise "invalid schema given" unless schema.format == "ipv6"
43
+
44
+ (hint && hint[:example]) || [
45
+ ->() { "2001:0db8:" + 6.times.map { "%04x" % rand(65535) }.join(":") },
46
+ ->() { "2001:0DB8:" + 6.times.map { "%04X" % rand(65535) }.join(":") },
47
+ ->() { "2001:db8:" + 6.times.map { "%x" % rand(65535) }.join(":") },
48
+ ->() { "2001:DB8:" + 6.times.map { "%X" % rand(65535) }.join(":") },
49
+ ].sample.call
50
+ end
51
+
52
+ def uri(schema, hint: nil, position: nil)
53
+ raise "invalid schema given" unless schema.format == "uri"
54
+
55
+ # TODO: urn
56
+ (hint && hint[:example]) || ::Faker::Internet.url(safe_domain)
57
+ end
58
+
59
+ protected def safe_domain
60
+ "example." + %w[ org com net ].sample
61
+ end
62
+
63
+ module_function *instance_methods
64
+ end
65
+ end
@@ -1,3 +1,4 @@
1
+ require "json_schema/faker/formats"
1
2
  require "json_schema/faker/util"
2
3
 
3
4
  module JsonSchema::Faker::Strategy
@@ -6,7 +7,14 @@ module JsonSchema::Faker::Strategy
6
7
 
7
8
  class << self
8
9
  def formats
9
- @formats ||= {}
10
+ @formats ||= {
11
+ "date-time" => ::JsonSchema::Faker::Formats.method(:date_time),
12
+ "email" => ::JsonSchema::Faker::Formats.method(:email),
13
+ "hostname" => ::JsonSchema::Faker::Formats.method(:hostname),
14
+ "ipv4" => ::JsonSchema::Faker::Formats.method(:ipv4),
15
+ "ipv6" => ::JsonSchema::Faker::Formats.method(:ipv6),
16
+ "uri" => ::JsonSchema::Faker::Formats.method(:uri),
17
+ }
10
18
  end
11
19
  end
12
20
 
@@ -232,6 +232,14 @@ class JsonSchema::Faker
232
232
  a.not = b.not
233
233
  end
234
234
  end
235
+ # semantic keyword
236
+ if b.format
237
+ if a.format
238
+ ::JsonSchema::Faker::Configuration.logger.warn "not support merging format" if ::JsonSchema::Faker::Configuration.logger
239
+ else
240
+ a.format = b.format
241
+ end
242
+ end
235
243
 
236
244
  a
237
245
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schema-faker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - okitan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-16 00:00:00.000000000 Z
11
+ date: 2017-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_schema
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faker
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -175,6 +189,7 @@ files:
175
189
  - bin/setup
176
190
  - json_schema-faker.gemspec
177
191
  - lib/json_schema/faker.rb
192
+ - lib/json_schema/faker/formats.rb
178
193
  - lib/json_schema/faker/strategy/greedy.rb
179
194
  - lib/json_schema/faker/strategy/simple.rb
180
195
  - lib/json_schema/faker/util.rb