module JsonSchema::Faker::Formats

Most format faker does not care other validations such as maxLength

Public Instance Methods

date_time(schema, hint: nil, position: nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

tools.ietf.org/html/rfc2606

# 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) click to toggle source

tools.ietf.org/html/rfc5737

# 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) click to toggle source

tools.ietf.org/html/rfc3849

# 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) click to toggle source
# 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

Protected Instance Methods

safe_domain() click to toggle source
# File lib/json_schema/faker/formats.rb, line 59
          def safe_domain
  "example." + %w[ org com net ].sample
end