class GovukSchemas::RandomSchemaGenerator
def generate_random_object(subschema)
def generate_random_object(subschema) document = {} one_of_sample = subschema.fetch("oneOf", []).sample(random: @random) || {} (subschema["properties"] || {}).each do |attribute_name, attribute_properties| # TODO: When the schema contains `subschema['minProperties']` we always # populate all of the keys in the hash. This isn't quite random, but I # haven't found a nice way yet to ensure there's at least n elements in # the hash. should_generate_value = @generator.bool \ || subschema["required"].to_a.include?(attribute_name) \ || (one_of_sample["required"] || {}).to_a.include?(attribute_name) \ || (one_of_sample["properties"] || {}).keys.include?(attribute_name) \ || subschema["minProperties"] \ next unless should_generate_value one_of_properties = (one_of_sample["properties"] || {})[attribute_name] document[attribute_name] = generate_value(one_of_properties || attribute_properties) end document end