Module: Genny::Hash

Defined in:
lib/genny/json_schema.rb

Constant Summary collapse

@@additional_properties =
1

Class Method Summary collapse

Class Method Details

.additional_properties=(chance) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
# File 'lib/genny/json_schema.rb', line 67

def self.additional_properties=(chance)
  raise ArgumentError, "chance must be a probability between 0 and 1" if chance < 0 || chance > 1
  @@additional_properties = chance
end

.genny(opts = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/genny/json_schema.rb', line 72

def self.genny(opts = {})
  opts = Genny.symbolize(opts)
  opts[:additional_properties_n] ||= 3
  raise "additional_properties_n must be an integer" unless opts[:additional_properties_n] >= 0
  opts[:properties] ||= {}
  if opts[:additionalProperties].is_a?(::Hash)
    (opts[:additional_properties_n] * @@additional_properties).round.times do |i|
      opts[:properties]["gennyAdditionalProperty#{i + 1}"] ||= opts[:additionalProperties]
    end
  end
  ::Hash[opts[:properties].map { |key, schema_object|
    next nil if !(opts[:required] || []).include?(key) && Random.rand > @@additional_properties
    [
      key,
      JSONSchema.new(
        schema_object.merge(
          hint: [key, opts[:hint]].compact.join(" ")
        ),
        definitions: opts[:definitions],
        validate: false
      ).genny
    ]
  }.compact]
end