Licet.Schema (Licet v0.1.0)

Copy Markdown View Source

Parses schemas written in SpiceDB's schema language.

A running instance parses its schema itself, from the :schema option or Licet.write_schema/3. Call parse/1 directly to check a schema file, for example in a test or before a deploy.

Licet supports definitions, relations, permissions, the operators +, &, -, and ->, the arrow functions .any() and .all(), nil, wildcards, userset subjects such as team#member, and expiration. Caveats and self are rejected. Schema language is the full reference.

Summary

Functions

Parses and validates schema text.

Prints a compiled schema as text.

Types

t()

@type t() :: %{
  definitions: map(),
  expiration: boolean(),
  source: String.t(),
  hash: String.t()
}

Functions

parse(source)

@spec parse(String.t()) :: {:ok, t()} | {:error, Licet.Error.t()}

Parses and validates schema text.

On success, returns a compiled schema. Its fields other than :source are internal. On failure, returns an :invalid_schema error whose details hold the :line and :column of the problem.

Examples

iex> {:ok, schema} =
...>   Licet.Schema.parse("""
...>   definition user {}
...>
...>   definition document {
...>     relation owner: user
...>     permission edit = owner
...>   }
...>   """)
iex> schema.definitions |> Map.keys() |> Enum.sort()
["document", "user"]

iex> {:error, error} = Licet.Schema.parse("definition document {\n  permission edit = owner\n}")
iex> error.reason
:invalid_schema
iex> error.details
%{line: 2, column: 21}

to_zed(map)

@spec to_zed(t()) :: String.t()

Prints a compiled schema as text.

The result parses to the same definitions. Comments and formatting from the original text aren't preserved; definitions are sorted by name.

Examples

iex> {:ok, schema} = Licet.Schema.parse("definition user {}")
iex> Licet.Schema.to_zed(schema)
"definition user {\n}"