cardinal

Tutorial

Build a small library app with one RecType, a validate hook, a REST call and a test.

You build library, an app with one RecType, Book. A Book needs at least one copy, and a test proves it. Run every command inside a workspace that already has a site, here mysite.localhost.

Create the app

cardinal app new library

This writes apps/library, runs git init there and adds library: [] to cardinal.workspace.exs. The next cardinal command compiles it.

Add a RecType

The manifest already lists one module, Library. A RecType's module must be one of the manifest's modules.

apps/library/rectypes/book.yaml
schema: 1
name: Book
module: Library
autoname: "field:title"
fields:
  - {fieldname: title, fieldtype: Data, label: Title, reqd: true, in_list_view: true}
  - {fieldname: author, fieldtype: Data, label: Author, in_list_view: true}
  - {fieldname: copies, fieldtype: Int, label: Copies, default: 1}
permissions:
  - {role: System Manager, rights: [read, write, create, delete, report, export]}

The file name is the RecType's name in snake case. autoname: "field:title" names each Book after its title.

Add a validate hook

The platform finds a controller by name: RecType Book in app library is Library.Controllers.Book.

apps/library/lib/controllers/book.ex
defmodule Library.Controllers.Book do
  @behaviour Cardinal.Controller

  @impl true
  def validate(rec, _old, _ctx) do
    if (rec["copies"] || 0) < 1 do
      message = "A book needs at least one copy"
      {:error, {:invalid, [%{path: "copies", code: "invalid", message: message}]}}
    else
      :ok
    end
  end
end

An {:invalid, errors} reason stops the save and rolls it back. The desk shows the message, and the REST API answers 422 with the error under fields.

Install and open it

cardinal app install -t mysite.localhost library
cardinal start

Log in as the System Manager and open /desk/book. Create a Book with Copies set to 0, and the form refuses it.

Call it over REST

On a User's form, Generate Keys gives an API key and secret. Send them as token KEY:SECRET.

curl -H "Authorization: token $KEY:$SECRET" \
  -H "Content-Type: application/json" \
  -d '{"title": "Dune", "author": "Frank Herbert", "copies": 2}' \
  http://mysite.localhost:4000/api/rec/Book

curl -H "Authorization: token $KEY:$SECRET" \
  'http://mysite.localhost:4000/api/rec/Book?fields=["name","copies"]'

Use the host and port cardinal start printed.

Test it

apps/library/test/book_test.exs
defmodule Library.BookTest do
  use ExUnit.Case, async: true
  alias Cardinal.{Ctx, DB}
  alias CardinalCore.Services.{TenantService, TxnService}

  setup_all do
    host = "library-#{System.unique_integer([:positive])}.test.localhost"
    {:ok, tenant} = Cardinal.Test.Tenants.install(host)
    on_exit(fn -> TenantService.drop(host) end)
    %{ctx: DB.as_system(%Ctx{tenant: tenant, user: "Administrator"})}
  end

  test "a book without copies is refused", %{ctx: ctx} do
    assert {:error, {:invalid, [%{path: "copies"}]}} =
             TxnService.run(ctx, &DB.insert(&1, "Book", %{"title" => "Dune", "copies" => 0}))
  end
end
cardinal test --app library

Next, add fields from the RecType YAML reference and more hooks from Controllers.

On this page