View Source Talos (Talos v1.12.2)

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

Summary

Functions

@spec errors(struct() | module(), any()) :: any()
@spec permit(struct() | module(), any()) :: any()

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

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