Module: JsonSchema::Faker::Formats

Defined in:
lib/json_schema/faker/formats.rb

Overview

Most format faker does not care other validations such as maxLength

Instance Method Summary collapse

Instance Method Details

#date_time(schema, hint: nil, position: nil) ⇒ Object



10
11
12
13
14
# File 'lib/json_schema/faker/formats.rb', line 10

def date_time(schema, hint: nil, position: nil)
  raise "invalid schema given" unless schema.format == "date-time"

  (hint && hint[:example]) || ::DateTime.now.rfc3339
end

#email(schema, hint: nil, position: nil) ⇒ Object



16
17
18
19
20
# File 'lib/json_schema/faker/formats.rb', line 16

def email(schema, hint: nil, position: nil)
  raise "invalid schema given" unless schema.format == "email"

  (hint && hint[:example]) || ::Faker::Internet.safe_email
end

#hostname(schema, hint: nil, position: nil) ⇒ Object



23
24
25
26
27
# File 'lib/json_schema/faker/formats.rb', line 23

def hostname(schema, hint: nil, position: nil)
  raise "invalid schema given" unless schema.format == "hostname"

  (hint && hint[:example]) || safe_domain
end

#ipv4(schema, hint: nil, position: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/json_schema/faker/formats.rb', line 30

def ipv4(schema, hint: nil, position: nil)
  raise "invalid schema given" unless schema.format == "ipv4"

  (hint && hint[:example]) || [
    ->() { "192.0.2.#{(0..255).to_a.sample}" },
    ->() { "198.51.100.#{(0..255).to_a.sample}" },
    ->() { "203.0.113.#{(0..255).to_a.sample}" },
  ].sample.call
end

#ipv6(schema, hint: nil, position: nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/json_schema/faker/formats.rb', line 41

def ipv6(schema, hint: nil, position: nil)
  raise "invalid schema given" unless schema.format == "ipv6"

  (hint && hint[:example]) || [
    ->() { "2001:0db8:" + 6.times.map { "%04x" % rand(65535) }.join(":") },
    ->() { "2001:0DB8:" + 6.times.map { "%04X" % rand(65535) }.join(":") },
    ->() { "2001:db8:"  + 6.times.map {   "%x" % rand(65535) }.join(":") },
    ->() { "2001:DB8:"  + 6.times.map {   "%X" % rand(65535) }.join(":") },
  ].sample.call
end

#uri(schema, hint: nil, position: nil) ⇒ Object



52
53
54
55
56
57
# File 'lib/json_schema/faker/formats.rb', line 52

def uri(schema, hint: nil, position: nil)
  raise "invalid schema given" unless schema.format == "uri"

  # TODO: urn
  (hint && hint[:example]) || ::Faker::Internet.url(safe_domain)
end