Typelizer.Serializer (Typelizer v0.2.0)

Copy Markdown View Source

A serializer DSL that is also the source of a TypeScript interface.

defmodule MyAppWeb.TaskSerializer do
  use Typelizer.Serializer, schema: MyApp.Tasks.Task

  attributes [:id, :title, :status, :due_on, :inserted_at]
  attribute :overdue, type: :boolean, value: &MyApp.Tasks.overdue?/1
  has_one :assignee, serializer: MyAppWeb.UserSerializer, nullable: true
  has_many :comments, serializer: MyAppWeb.CommentSerializer
end

MyAppWeb.TaskSerializer.serialize(task)
#=> %{"id" => "…", "title" => "…", "dueOn" => "2026-10-01", …}

MyAppWeb.TaskSerializer.serialize_many(tasks, user_id: current_user.id)

mix typelizer.gen writes the matching TypeScript interface.

Options

  • :schema - an Ecto schema or embedded schema. Types are inferred from it and each attribute is checked against it.
  • :name - the TypeScript interface name. Default: derived from the module name (MyAppWeb.Admin.TaskSerializer → AdminTask).
  • :key_transform - :camel or :snake. Default: config :typelizer, :key_transform (:camel).

Generated functions

  • serialize(record, opts \\ []) - returns a map with string keys, or nil for nil.
  • serialize_many(records, opts \\ []) - returns a list of maps.

The options are passed to arity-2 value: functions and to nested serializers.

See the Serializers guide for type specs, type inference and nullability.

Summary

Functions

Declares one field.

Declares a computed field. The function is the last argument, so the options must be written in brackets

Declares schema fields. Needs the :schema option.

Declares a list of nested objects, serialized with another serializer.

Declares one nested object, serialized with another serializer.

Functions

attribute(name, opts \\ [])

(macro)

Declares one field.

Options

  • :type - a type spec. Required with :value or without a schema.
  • :nullable - true or false. Always wins over inference.
  • :value - a function of the record (arity 1) or of the record and the options (arity 2) that computes the value.
  • :optional - true leaves the key out when the value is nil (key?: T).
  • :required - for an embed: the embed fields that are never null ([:street, geo: [:lat]]), or :all.
  • :if - a function (arity 1 or 2) that decides whether the key is sent (key?: T).

Examples

attribute :title
attribute :title, nullable: false
attribute :overdue, type: :boolean, value: &MyApp.Tasks.overdue?/1
attribute :overdue, [type: :boolean], fn task -> MyApp.Tasks.overdue?(task) end

attribute(name, opts, fun)

(macro)

Declares a computed field. The function is the last argument, so the options must be written in brackets:

attribute :overdue, [type: :boolean], fn task -> MyApp.Tasks.overdue?(task) end

attributes(names)

(macro)

Declares schema fields. Needs the :schema option.

attributes [:id, :title, :status]

has_many(name, opts)

(macro)

Declares a list of nested objects, serialized with another serializer.

Options

  • :serializer - required. The serializer of each nested value.
  • :value - a function (arity 1 or 2) that returns the nested values.
  • :optional, :if - as for attribute/2.

Examples

has_many :comments, serializer: MyAppWeb.CommentSerializer

has_one(name, opts)

(macro)

Declares one nested object, serialized with another serializer.

Options

  • :serializer - required. The serializer of the nested value.
  • :nullable - true or false.
  • :value - a function (arity 1 or 2) that returns the nested value.
  • :optional, :if - as for attribute/2.

Examples

has_one :assignee, serializer: MyAppWeb.UserSerializer, nullable: true