RockSolid (rock_solid v0.0.11)

Copy Markdown

Data generation from JSON schema.

RockSolid exposes a single function: from_schema/2.

You can use it inside a property test

defmodule MyTest do
  use ExUnit.Case
  use ExUnitProperties

  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
end

or as a generator, since RockSolid.from_schema/2 returns elements of type StreamData.t/1

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"
  }
]

Summary

Functions

Generates data based on the input JSON schema

Functions

from_schema(json_schema, opts \\ [])

Generates data based on the input JSON schema

Options