Agent skill

Ecto Changeset Patterns

by j-morgan6 in j-morgan6/elixir-phoenix-guide

A skill your agent uses when a resource needs multiple changesets (registration vs update), conditional validation, field transforms, or uniqueness validation — changeset composition.

MITAuto-check passed

Install Ecto Changeset Patterns

skills CLI
$ npx skills add j-morgan6/elixir-phoenix-guide --skill ecto-changeset-patterns -a claude-code

Project install by default; add -g for ~/.claude/skills/.

GitHub CLI
$ gh skill install j-morgan6/elixir-phoenix-guide ecto-changeset-patterns --agent claude-code

Project scope by default; add --scope user for a personal install. Needs GitHub CLI 2.90.0 or later (public preview).

Manual copy
$ git clone --depth 1 https://github.com/j-morgan6/elixir-phoenix-guide.git skills-src && mkdir -p .claude/skills && cp -r skills-src/skills/ecto-changeset-patterns .claude/skills/ecto-changeset-patterns && rm -rf skills-src

Use ~/.claude/skills/ instead of .claude/skills for a personal install. The folder must contain SKILL.md.

Claude Code skills documentation · loads skills from .claude/skills/

Facts

Skill name
ecto-changeset-patterns
GitHub stars
166
Token cost
~2.1k tokens
SKILL.md length
264 words
Files
1
Skills in repo
19
Repo updated
First seen
Licence
MIT

At a glance

A skill your agent uses when a resource needs multiple changesets (registration vs update), conditional validation, field transforms, or uniqueness validation — changeset composition.

  • Works in 7 steps: Create separate named changesets per… → Never require foreign key fields in… → Compose changesets with pipes — each… → …
  • A resource needs multiple changesets (registration vs update)
  • SKILL.md covers RULES — Follow these with no…, Separate Changesets Per…, cast_assoc — Critical Pitfall and Changeset Composition, plus 4 more sections
  • Instructions only: no scripts, shell commands, URLs or credentials in SKILL.md

What it does

Ecto Changeset Patterns is an agent skill from j-morgan6/elixir-phoenix-guide. Use when a resource needs multiple changesets (registration vs update), conditional validation, field transforms, or uniqueness validation — changeset composition.

Its SKILL.md is about 2.1k tokens, which your agent loads only when the skill is triggered. It is a single SKILL.md file with no bundled scripts.

The licence is MIT.

When your agent uses it

  • A resource needs multiple changesets (registration vs update)
  • Conditional validation
  • Field transforms
  • Uniqueness validation — changeset composition

Example prompts

  • “/ecto-changeset-patterns”

Workflow steps

7 steps, taken from the first numbered list in SKILL.md.

  1. Create separate named changesets per operation — registration_changeset, email_changeset, password_changeset; never overload a single…
  2. Never require foreign key fields in cast_assoc child changesets — the parent sets them automatically; requiring them causes "can't be…
  3. Compose changesets with pipes — each validation step is a separate function for reuse and clarity
  4. Use unsafe_validate_unique paired with unique_constraint — never one without the other; unsafe_validate_unique gives fast UI feedback…
  5. Use update_change/3 for field transformations — trimming, downcasing, slugifying happen in the changeset, never in the controller or context
  6. Accept opts \ [] for conditional validation — allows callers to toggle validation rules without creating yet another changeset function
  7. Validate at the changeset level, not in context functions — context functions should be thin wrappers around Repo calls

What it can do on your machine

Read from SKILL.md and the folder at commit cdfddac. It shows what the files ask for, not the result of running them.

  • Tool permissions

    Pre-approves nothing: there is no allowed-tools line, so your agent's usual permission prompts apply.

    From allowed-tools in the SKILL.md frontmatter.

  • Runs code

    No scripts in the folder and no shell commands in SKILL.md (its code samples are elixir).

    From the folder's file list and the shell code blocks in SKILL.md.

  • Network

    No URLs in SKILL.md.

    From URLs in SKILL.md, links to its own repository left out.

  • Credentials

    Names no API keys, tokens, secrets or passwords.

    From names ending in _API_KEY, _TOKEN, _SECRET, _KEY or _PASSWORD in SKILL.md.

Context cost

Ecto Changeset Patterns loads about 2.1k tokens when it runs. Until then it costs about 47 tokens; SKILL.md has 264 words of instructions outside code blocks.

Always · name and description, kept in context so the agent knows when to use it
~47
When it runs · the whole SKILL.md, loaded when a task matches
~2.1k

Estimates: characters ÷ 4, the usual rule of thumb; real counts depend on the model's tokenizer. Scripts and assets cost tokens only if the agent reads them.

Safety

Auto-check passed

The automated check found no risky patterns in SKILL.md.

Automated static check — not a guarantee. Review scripts before installing. It scans the text of SKILL.md for risky patterns (piping downloads into a shell, reading credential files, hidden Unicode, destructive commands); files beside SKILL.md are not scanned.

SKILL.md

The full file from j-morgan6/elixir-phoenix-guide at commit cdfddac, republished under its MIT licence (© j-morgan6). 264 words, ~2,122 tokens.

Download SKILL.mdSave it as .claude/skills/ecto-changeset-patterns/SKILL.md (or your agent's skills folder).
name
ecto-changeset-patterns
description
Use when a resource needs multiple changesets (registration vs update), conditional validation, field transforms, or uniqueness validation — changeset composition.
file_patterns
**/schemas/**/*.ex, **/accounts/*.ex, **/*_schema.ex
auto_suggest
true

Ecto Changeset Patterns

RULES — Follow these with no exceptions

  1. Create separate named changesets per operation — registration_changeset, email_changeset, password_changeset; never overload a single changeset/2
  2. Never require foreign key fields in cast_assoc child changesets — the parent sets them automatically; requiring them causes "can't be blank" errors
  3. Compose changesets with pipes — each validation step is a separate function for reuse and clarity
  4. Use unsafe_validate_unique paired with unique_constraint — never one without the other; unsafe_validate_unique gives fast UI feedback, unique_constraint handles race conditions
  5. Use update_change/3 for field transformations — trimming, downcasing, slugifying happen in the changeset, never in the controller or context
  6. Accept opts \\ [] for conditional validation — allows callers to toggle validation rules without creating yet another changeset function
  7. Validate at the changeset level, not in context functions — context functions should be thin wrappers around Repo calls

Separate Changesets Per Operation

Different operations need different validation rules. Don't overload changeset/2.

elixir
defmodule MyApp.Accounts.User do
  use Ecto.Schema
  import Ecto.Changeset

  schema "users" do
    field :email, :string
    field :username, :string
    field :password, :string, virtual: true, redact: true
    field :hashed_password, :string, redact: true
    field :bio, :string

    timestamps()
  end

  # Registration — all fields, password hashing
  def registration_changeset(user, attrs, opts \\ []) do
    user
    |> cast(attrs, [:email, :username, :password])
    |> validate_email(opts)
    |> validate_username()
    |> validate_password(opts)
  end

  # Email change — only email, requires reconfirmation
  def email_changeset(user, attrs, opts \\ []) do
    user
    |> cast(attrs, [:email])
    |> validate_email(opts)
  end

  # Password change — only password
  def password_changeset(user, attrs, opts \\ []) do
    user
    |> cast(attrs, [:password])
    |> validate_password(opts)
    |> put_password_hash()
  end

  # Profile update — non-sensitive fields only
  def profile_changeset(user, attrs) do
    user
    |> cast(attrs, [:username, :bio])
    |> validate_username()
  end
end

cast_assoc — Critical Pitfall

The most common source of "can't be blank" errors. Foreign keys are set automatically by the parent — never require them in the child changeset.

elixir
# Parent schema
defmodule MyApp.Blog.Post do
  schema "posts" do
    field :title, :string
    has_many :ingredients, MyApp.Blog.Ingredient

    timestamps()
  end

  def changeset(post, attrs) do
    post
    |> cast(attrs, [:title])
    |> validate_required([:title])
    |> cast_assoc(:ingredients, with: &MyApp.Blog.Ingredient.changeset/2)
  end
end

# Child schema — DO NOT require :post_id
defmodule MyApp.Blog.Ingredient do
  schema "ingredients" do
    field :name, :string
    field :quantity, :string
    belongs_to :post, MyApp.Blog.Post

    timestamps()
  end

  # Bad — :post_id is required but set automatically by cast_assoc
  def changeset(ingredient, attrs) do
    ingredient
    |> cast(attrs, [:name, :quantity, :post_id])
    |> validate_required([:name, :post_id])  # Fails!
  end

  # Good — only require user-provided fields
  def changeset(ingredient, attrs) do
    ingredient
    |> cast(attrs, [:name, :quantity])
    |> validate_required([:name])
  end
end

Changeset Composition

Break validation into small, reusable functions. Compose with pipes.

elixir
defmodule MyApp.Accounts.User do
  # Reusable validation components

  defp validate_email(changeset, opts) do
    changeset
    |> validate_required([:email])
    |> validate_format(:email, ~r/^[^\s]+@[^\s]+$/, message: "must have the @ sign and no spaces")
    |> validate_length(:email, max: 160)
    |> maybe_validate_unique_email(opts)
  end

  defp validate_username(changeset) do
    changeset
    |> validate_required([:username])
    |> validate_format(:username, ~r/^[a-zA-Z0-9_]+$/, message: "only letters, numbers, and underscores")
    |> validate_length(:username, min: 3, max: 30)
    |> unsafe_validate_unique(:username, MyApp.Repo)
    |> unique_constraint(:username)
  end

  defp validate_password(changeset, opts) do
    changeset
    |> validate_required([:password])
    |> validate_length(:password, min: 8, max: 72)
    |> maybe_hash_password(opts)
  end

  defp maybe_validate_unique_email(changeset, opts) do
    if Keyword.get(opts, :validate_email, true) do
      changeset
      |> unsafe_validate_unique(:email, MyApp.Repo)
      |> unique_constraint(:email)
    else
      changeset
    end
  end

  defp maybe_hash_password(changeset, opts) do
    if Keyword.get(opts, :hash_password, true) && changeset.valid? do
      changeset
      |> put_change(:hashed_password, Bcrypt.hash_pwd_salt(get_change(changeset, :password)))
      |> delete_change(:password)
    else
      changeset
    end
  end
end

Conditional Validation with opts

Use opts to toggle validation behavior from the caller. This avoids creating a new changeset function for every variation.

elixir
# In the schema module
def registration_changeset(user, attrs, opts \\ []) do
  user
  |> cast(attrs, [:email, :username, :password])
  |> validate_email(opts)
  |> validate_password(opts)
end

# In the context — normal registration
def register_user(attrs) do
  %User{}
  |> User.registration_changeset(attrs)
  |> Repo.insert()
end

# In tests — skip hashing for speed
def register_user_for_test(attrs) do
  %User{}
  |> User.registration_changeset(attrs, hash_password: false, validate_email: false)
  |> Repo.insert()
end

Field Transformations with update_change

Transform field values in the changeset, not in the controller or LiveView.

elixir
def changeset(user, attrs) do
  user
  |> cast(attrs, [:email, :username])
  |> update_change(:email, &String.downcase/1)
  |> update_change(:username, &String.trim/1)
  |> update_change(:username, &String.downcase/1)
end

# For slugs
def changeset(post, attrs) do
  post
  |> cast(attrs, [:title])
  |> validate_required([:title])
  |> generate_slug()
end

defp generate_slug(changeset) do
  case get_change(changeset, :title) do
    nil -> changeset
    title ->
      slug = title |> String.downcase() |> String.replace(~r/[^a-z0-9]+/, "-") |> String.trim("-")
      put_change(changeset, :slug, slug)
  end
end

Uniqueness Validation

Always pair unsafe_validate_unique with unique_constraint. They serve different purposes.

elixir
def changeset(user, attrs) do
  user
  |> cast(attrs, [:email, :username])
  # Fast check — queries DB, gives immediate UI feedback
  # "unsafe" because another insert could happen between check and insert
  |> unsafe_validate_unique(:email, MyApp.Repo)
  |> unsafe_validate_unique(:username, MyApp.Repo)
  # Constraint check — catches race conditions at insert time
  # Requires a matching unique index in the database
  |> unique_constraint(:email)
  |> unique_constraint(:username)
end

Testing Changesets

elixir
describe "registration_changeset/2" do
  test "valid with all required fields" do
    changeset = User.registration_changeset(%User{}, %{
      email: "test@example.com",
      username: "testuser",
      password: "validpassword123"
    })

    assert changeset.valid?
  end

  test "invalid without email" do
    changeset = User.registration_changeset(%User{}, %{
      username: "testuser",
      password: "validpassword123"
    })

    refute changeset.valid?
    assert "can't be blank" in errors_on(changeset).email
  end

  test "transforms email to lowercase" do
    changeset = User.email_changeset(%User{}, %{email: "TEST@Example.COM"})
    assert get_change(changeset, :email) == "test@example.com"
  end
end

See ecto-essentials skill for schema and migration patterns. See ecto-nested-associations skill for cast_assoc with nested data. See testing-essentials skill for comprehensive testing patterns.

© j-morgan6, MIT. Rendered from Markdown: HTML in the file is shown as text, images as links, and headings moved down two levels. Raw file

Files

Just SKILL.md in skills/ecto-changeset-patterns of j-morgan6/elixir-phoenix-guide.

Open the folder on GitHubat commit cdfddac

Compare with similar skills

Ecto Changeset Patterns next to the 5 skills that share the most tags, products or categories with it. Stars are the repository's; “used in” counts other GitHub owners with a copy.

Ecto Changeset Patterns compared with similar skills
SkillStarsUsed inTokensAuto-checkLicenceRepo updated
Ecto Changeset Patterns this skillj-morgan6/elixir-phoenix-guide166—~2.1kAutomated safety check: PassMIT
Changesetwithastro/astro63k—~1.1kAutomated safety check: PassCustom licence
Changesetudecode/plate17k—~1.3kAutomated safety check: PassCustom licence
Agent Resource Allocatorruvnet/ruflo74k2 repos~4.9kAutomated safety check: PassMIT
Resourceswindmill-labs/windmill18k—~1.7kAutomated safety check: PassCustom licence
Resource Hintsthedaviddias/Front-End-Checklist74k—~430Automated safety check: PassMIT

Similar skills

  • Changeset

    withastro/astro

    Official

    Create a changeset for the Astro monorepo. An agent skill from withastro/astro.

    63k GitHub stars~1.1k tokensUpdated today
    DevelopmentAuto-check passed
  • Changeset

    udecode/plate

    Command: changeset

    17k GitHub stars~1.3k tokensUpdated today
    DevelopmentAuto-check passed
  • Agent skill for resource-allocator - invoke with $agent-resource-allocator

    74k GitHub starsUsed in 2 repos~4.9k tokens
    Auto-check passed
  • Resources

    windmill-labs/windmill

    MUST use when managing resources.

    18k GitHub stars~1.7k tokensUpdated yesterday
    Backend & APIsAuto-check passed
  • Resource Hints

    thedaviddias/Front-End-Checklist

    A skill your agent uses when auditing slow page loads, heavy assets, or rendering delays related to Use resource hints for faster loading.

    74k GitHub stars~430 tokensUpdated yesterday
    Frontend & DesignAuto-check passed
  • Changeset

    garden-co/classic-jazz

    Generate changeset files for versioning and changelog management in this monorepo.

    2.5k GitHub stars~538 tokensUpdated 1 mo ago
    DevelopmentAuto-check passed

More from j-morgan6/elixir-phoenix-guide

All 19 skills in this repo
  • Code Quality

    j-morgan6/elixir-phoenix-guide

    A skill your agent uses when refactoring for duplication, complexity, or dead code — includes the plugin's on-demand analysis scripts.

    166 GitHub stars~1.4k tokensUpdated 3 mo ago
    Auto-check passed
  • Deployment Gotchas

    j-morgan6/elixir-phoenix-guide

    A skill your agent uses when preparing releases or deployment config — runtime.exs vs compile-time config, release migrations, PHXHOST/PHXSERVER, assets, health checks.

    166 GitHub stars~2.6k tokensUpdated 3 mo ago
    Auto-check passed
  • Ecto Essentials

    j-morgan6/elixir-phoenix-guide

    A skill your agent uses when defining schemas, writing queries, or creating migrations — schema design, Repo usage, indexes, query composition.

    166 GitHub stars~2.2k tokensUpdated 3 mo ago
    Auto-check passed
  • Ecto Nested Associations

    j-morgan6/elixir-phoenix-guide

    A skill your agent uses when a form or operation manages parent and child records together — castassoc/castembed, onreplace, Ecto.Multi across tables, FK cascade design.

    166 GitHub stars~2.2k tokensUpdated 3 mo ago
    Auto-check passed
  • Elixir Essentials

    j-morgan6/elixir-phoenix-guide

    A skill your agent uses when writing or refactoring core Elixir — pattern matching, case/cond/with, pipes, {:ok, }/{:error, } contracts.

    166 GitHub stars~2.2k tokensUpdated 3 mo ago
    Auto-check passed
  • Oban Essentials

    j-morgan6/elixir-phoenix-guide

    A skill your agent uses when writing background jobs with Oban — worker options, return contracts, idempotency, uniqueness, Oban.Testing.

    166 GitHub stars~2.1k tokensUpdated 3 mo ago
    Auto-check passed

Questions about Ecto Changeset Patterns

What does Ecto Changeset Patterns do?

A skill your agent uses when a resource needs multiple changesets (registration vs update), conditional validation, field transforms, or uniqueness validation — changeset composition. Ecto Changeset Patterns is an agent skill from j-morgan6/elixir-phoenix-guide. Use when a resource needs multiple changesets (registration vs update), conditional validation, field transforms, or uniqueness validation — changeset composition.

When should I use Ecto Changeset Patterns?

Ecto Changeset Patterns fits situations like: A resource needs multiple changesets (registration vs update); conditional validation; field transforms; uniqueness validation — changeset composition.

How do I install Ecto Changeset Patterns in Claude Code?

Run `npx skills add j-morgan6/elixir-phoenix-guide --skill ecto-changeset-patterns -a claude-code`. Or copy the skill folder (skills/ecto-changeset-patterns in j-morgan6/elixir-phoenix-guide) into .claude/skills/ecto-changeset-patterns in your project. Claude Code loads it when a task matches its description.

How do I install Ecto Changeset Patterns in Codex?

Run `npx skills add j-morgan6/elixir-phoenix-guide --skill ecto-changeset-patterns -a codex`. Or copy the skill folder (skills/ecto-changeset-patterns in j-morgan6/elixir-phoenix-guide) into .agents/skills/ecto-changeset-patterns in your project. Codex loads it when a task matches its description.

Can I use Ecto Changeset Patterns in Cursor, Gemini CLI or GitHub Copilot?

Cursor, Gemini CLI, GitHub Copilot and OpenCode also load SKILL.md folders. With the skills CLI, run `npx skills add j-morgan6/elixir-phoenix-guide --skill ecto-changeset-patterns -a cursor` (or -a gemini-cli, github-copilot or opencode for the others). To copy it by hand, put the folder in .cursor/skills/ecto-changeset-patterns, .gemini/skills/ecto-changeset-patterns, .github/skills/ecto-changeset-patterns and .opencode/skills/ecto-changeset-patterns in your project.

What does Ecto Changeset Patterns need to run?

SKILL.md names no scripts, command-line tools or credentials: Ecto Changeset Patterns is instructions for the agent only.

Does Ecto Changeset Patterns access the network?

SKILL.md contains no URLs. Any network use would come from the scripts or tools the agent runs. This is read from the text; nothing was executed.

Is Ecto Changeset Patterns safe to install?

Our automated static check of SKILL.md found no risky patterns, such as piping downloads into a shell, reading credential files or hidden Unicode. It is not a guarantee. Review the folder before installing.

What licence does Ecto Changeset Patterns use?

Ecto Changeset Patterns is published under the MIT licence (the repository's licence). It allows redistribution, so the full SKILL.md is shown on this page.

How many tokens does Ecto Changeset Patterns use?

About 2.1k tokens (SKILL.md is roughly 8.5k characters). Agents keep only the skill's name and description in context until a task matches; then they load SKILL.md in full.

What are the alternatives to Ecto Changeset Patterns?

Skills that share tags, products or a category with Ecto Changeset Patterns: Changeset (withastro/astro, 63k stars), Changeset (udecode/plate, 17k stars), Agent Resource Allocator (ruvnet/ruflo, 74k stars) and Resources (windmill-labs/windmill, 18k stars). The comparison table on this page puts their stars, adoption, token cost, safety result and licence side by side.

Who maintains Ecto Changeset Patterns?

j-morgan6 (a GitHub user) maintains it in j-morgan6/elixir-phoenix-guide, which has 166 GitHub stars. The repository holds 19 skills in this directory. The repository was last updated on July 6, 2026.

Source: j-morgan6/elixir-phoenix-guide on GitHub. Facts on this page come from the repository at the commit we read; the author's words are quoted as theirs.