RockSolid - Data generation from JSON Schema definitions

Hi everyone

I have been working for a while on RockSolid, a library to generate valid data from JSON schemas.

It is built on top of (the awesome) StreamData, so you can use it directly in property tests, or as a data generator. It supports JSON Schema drafts 04 through 2020-12, including recursive schemas, remote references, dependent schemas, if/then/else, etc.

It is tested against the open SchemaStore catalog, which contains some complex schemas with 10k+ lines, and can generate valid data for over 85% of the documents. So, while far from perfect, it is useful for most of the realistic scenarios. It has been useful for me, so hopefully you find it useful too.

Usage

Inside property tests

  property "generates valid user profiles" do
    schema = %{
      "type" => "object",
      "additionalProperties" => false,
      "properties" => %{
        "birthDate" => %{"type" => "string", "format" => "date"},
        "name" => %{"type" => "string", "pattern" => "^[A-Z][a-z]+$"},
        "email" => %{"type" => "string", "format" => "email"}
      },
      "required" => ["birthDate", "name", "email"]
    }

    check all user_data <- RockSolid.from_schema(schema) do
      assert Regex.match?(~r/^[A-Z][a-z]+$/, user_data["name"])
      assert %Date{} = Date.from_iso8601!(user_data["birthDate"])
      assert String.split(user_data["email"], "@") |> length() == 2
    end
  end

As a regular generator

iex(1)> specs = %{
  "type" => "object",
  "properties" => %{
    "serverIPs" => %{
      "type" => "array",
      "items" => %{"type" => "string", "format" => "ipv4"},
      "uniqueItems" => true,
      "minItems" => 1
    },
    "serverName" => %{"pattern" => "^[a-z][a-z_0-9]{2,255}$", "type" => "string"},
  },
  "required" => ["serverIPs", "serverName"],
  "additionalProperties" => false
}

iex(2)> specs |> RockSolid.from_schema() |> Enum.take(3)
[
  %{"serverIPs" => ["148.50.92.205"], "serverName" => "a5l"},
  %{"serverIPs" => ["230.26.166.121"], "serverName" => "y_w"},
  %{
    "serverIPs" => ["144.154.111.248", "155.134.134.38"],
    "serverName" => "v2_5"
  }
]

Feel free to give it a try and provde any feedback :smiley:

Links

6 Likes