Talos (Talos v1.12.1) View Source

Documentation for Talos.

Talos is params type validation library, can be used with any Elixir application

Sample usage:

defmodule MyAppWeb.UserController do
  # just import talos with functions helpers
  import Talos

  @interests_type enum(members: ["sports", "games", "food"]) # <- subtype
  # here we define expected struct
  @user_type map(fields: [
    field(key: "email", type: string(min_length: 5, max_length: 255, regexp: ~r/.*@.*/)),
    field(key: "age", type: integer(gteq: 18, allow_nil: true)),
    field(key: "interests", type: list(type: @interests_type), optional: true)
  ])

  def create(conn, params) do
    case Talos.valid?(@user_type, params) do
      true ->
        user = MyApp.create_user(params)
        conn
        |> put_flash(:info, "User created successfully.")
        |> redirect(to: Routes.user_path(conn, :show, user))
      false ->
        conn
        |> put_flash(:info, "Wrong params passed.")
        |> render("new.html")
    end
  end
end

Link to this section Summary

Link to this section Functions

Specs

errors(struct() | module(), any()) :: any()

Specs

permit(struct() | module(), any()) :: any()

Reduces data to what is defined in it's type.

Specs

valid?(struct() | module(), any()) :: boolean()