Logo Ecto Changesets

Ecto Changesets

Tổ chức
TheBushidoCollective
ecto-changesets

Use when validating and casting data with Ecto changesets including field validation, constraints, nested changesets, and data transformation. Use for ensuring data integrity before database operations.

Add to TypingMind

Tổng quan

Nhà phát hànhTheBushidoCollective
Kho mãhan
Tên kỹ năngecto-changesets
Sao
195
Bản fork
20
Tệp đi kèm
Chỉ có hướng dẫn
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI dùng hướng dẫn viết bằng Markdown thay vì mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • Hướng dẫn có đủ mọi thứ cần thiết

    Mọi thứ mô hình cần đều nằm trong hướng dẫn — không có tệp bổ sung cần đồng bộ.

  • Mã nguồn mở

    Được TheBushidoCollective công bố trên GitHub. Hãy đọc các tệp nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Ecto Changesets trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL của thư mục kỹ năng được hiển thị bên dưới rồi xác nhận.
    https://github.com/TheBushidoCollective/han/tree/main/plugins/frameworks/ecto/skills/ecto-changesets
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/TheBushidoCollective/han.git /tmp/han
mkdir -p .claude/skills
cp -r /tmp/han/plugins/frameworks/ecto/skills/ecto-changesets .claude/skills/ecto-changesets
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Ecto Changesets trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự nạp Ecto Changesets ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên bạn có thể dùng Ecto Changesets với cả những mô hình khác ngoài mô hình ban đầu. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên khi kỹ năng chưa được dùng, bạn không tốn token cho phần hướng dẫn.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

Ecto Changesets

Master Ecto changesets to validate, cast, and transform data before database operations. This skill covers changeset creation, validation, constraints, handling associations, and advanced patterns for maintaining data integrity.

Basic Changeset

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

  schema "users" do
    field :name, :string
    field :email, :string
    field :age, :integer

    timestamps()
  end

  def changeset(user, params \\ %{}) do
    user
    |> cast(params, [:name, :email, :age])
    |> validate_required([:name, :email])
  end
end

# Usage
changeset = MyApp.User.changeset(%MyApp.User{}, %{name: "John", email: "john@example.com"})

Changesets filter and validate parameters before they're applied to a struct. The cast/3 function specifies which fields can be changed, and validate_required/2 ensures specific fields are present.

Creating and Validating Changesets

elixir
defmodule MyApp.Person do
  use Ecto.Schema
  import Ecto.Changeset

  schema "people" do
    field :first_name, :string
    field :last_name, :string
    field :age, :integer

    timestamps()
  end

  def changeset(person, params \\ %{}) do
    person
    |> cast(params, [:first_name, :last_name, :age])
    |> validate_required([:first_name, :last_name])
    |> validate_number(:age, greater_than_or_equal_to: 0)
  end
end

# Create changeset
changeset = MyApp.Person.changeset(%MyApp.Person{}, %{first_name: "Jane"})

# Check validity
changeset.valid?  # false, last_name is missing

# Access errors
changeset.errors
# [first_name: {"can't be blank", [validation: :required]},
#  last_name: {"can't be blank", [validation: :required]}]

The valid? field indicates whether the changeset has any errors. The errors field contains a keyword list of validation failures with error messages and metadata.

Inserting with Changesets

elixir
person = %MyApp.Person{}
changeset = MyApp.Person.changeset(person, %{
  first_name: "John",
  last_name: "Doe",
  age: 30
})

case MyApp.Repo.insert(changeset) do
  {:ok, person} ->
    # Successfully inserted
    IO.puts("Created person with ID: #{person.id}")

  {:error, changeset} ->
    # Validation or constraint errors
    IO.inspect(changeset.errors)
end

The Repo.insert/1 function accepts a changeset and returns {:ok, struct} on success or {:error, changeset} on failure. Pattern matching makes error handling straightforward.

Updating with Changesets

elixir
person = MyApp.Repo.get!(MyApp.Person, 1)
changeset = MyApp.Person.changeset(person, %{age: 31})

case MyApp.Repo.update(changeset) do
  {:ok, updated_person} ->
    # Successfully updated
    IO.puts("Updated person age to: #{updated_person.age}")

  {:error, changeset} ->
    # Validation or constraint errors
    IO.inspect(changeset.errors)
end

Updates work similarly to inserts, but start with an existing struct from the database. The changeset tracks which fields have changed.

Type Casting

elixir
changeset = Ecto.Changeset.cast(%MyApp.User{}, %{"age" => "25"}, [:age])
user = MyApp.Repo.insert!(changeset)
user.age  # 25 (integer, not string)

The cast/3 function automatically converts parameter values to their schema-defined types. Strings like "25" are converted to integers when the field type is :integer.

Field Validations

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

  schema "users" do
    field :email, :string
    field :username, :string
    field :age, :integer
    field :bio, :string
    field :website, :string

    timestamps()
  end

  def changeset(user, params \\ %{}) do
    user
    |> cast(params, [:email, :username, :age, :bio, :website])
    |> validate_required([:email, :username])
    |> validate_format(:email, ~r/@/)
    |> validate_length(:username, min: 3, max: 20)
    |> validate_length(:bio, max: 500)
    |> validate_number(:age, greater_than: 0, less_than: 150)
    |> validate_inclusion(:age, 18..100)
    |> validate_format(:website, ~r/^https?:\/\//)
  end
end

Ecto provides many built-in validators including validate_format/3 for regex patterns, validate_length/3 for string lengths, validate_number/3 for numeric constraints, and validate_inclusion/3 for allowed values.

Custom Validation Functions

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

  schema "users" do
    field :email, :string
    field :password, :string, virtual: true
    field :password_hash, :string

    timestamps()
  end

  def changeset(user, params \\ %{}) do
    user
    |> cast(params, [:email, :password])
    |> validate_required([:email, :password])
    |> validate_email_format()
    |> validate_password_strength()
    |> hash_password()
  end

  defp validate_email_format(changeset) do
    changeset
    |> validate_format(:email, ~r/@/, message: "must be a valid email")
    |> validate_length(:email, max: 255)
  end

  defp validate_password_strength(changeset) do
    validate_change(changeset, :password, fn :password, password ->
      cond do
        String.length(password) < 8 ->
          [password: "must be at least 8 characters"]

        not String.match?(password, ~r/[A-Z]/) ->
          [password: "must contain at least one uppercase letter"]

        not String.match?(password, ~r/[0-9]/) ->
          [password: "must contain at least one number"]

        true ->
          []
      end
    end)
  end

  defp hash_password(changeset) do
    case changeset do
      %Ecto.Changeset{valid?: true, changes: %{password: password}} ->
        put_change(changeset, :password_hash, hash_password_value(password))

      _ ->
        changeset
    end
  end

  defp hash_password_value(password) do
    # Use a real hashing library like Argon2 or Bcrypt
    :crypto.hash(:sha256, password) |> Base.encode64()
  end
end

Custom validation functions use validate_change/3 to add custom logic. The put_change/3 function modifies changeset values, useful for transformations like password hashing.

Constraint Validations

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

  schema "users" do
    field :email, :string
    field :username, :string

    timestamps()
  end

  def changeset(user, params \\ %{}) do
    user
    |> cast(params, [:email, :username])
    |> validate_required([:email, :username])
    |> unique_constraint(:email)
    |> unique_constraint(:username)
  end
end

# Usage
case MyApp.Repo.insert(changeset) do
  {:ok, user} ->
    # Success

  {:error, changeset} ->
    # Will contain unique constraint error if email/username exists
    changeset.errors
end

Constraint validations check database-level constraints like unique indexes. They only run when the changeset is inserted or updated, not during validation.

Unique Constraint with Custom Error Message

elixir
def changeset(user, params \\ %{}) do
  user
  |> cast(params, [:email])
  |> validate_required([:email])
  |> unique_constraint(:email,
       name: :users_email_index,
       message: "has already been taken")
end

The unique_constraint/3 function accepts options to specify the constraint name and customize the error message. This maps database constraint violations to user-friendly errors.

Foreign Key Constraints

elixir
defmodule MyApp.Comment do
  use Ecto.Schema
  import Ecto.Changeset

  schema "comments" do
    field :body, :string
    belongs_to :post, MyApp.Post

    timestamps()
  end

  def changeset(comment, params \\ %{}) do
    comment
    |> cast(params, [:body, :post_id])
    |> validate_required([:body, :post_id])
    |> foreign_key_constraint(:post_id)
  end
end

The foreign_key_constraint/3 function validates that foreign key relationships are valid. If you try to create a comment with a non-existent post_id, the constraint will catch it.

Check Constraints

elixir
def changeset(product, params \\ %{}) do
  product
  |> cast(params, [:price, :discount_price])
  |> validate_required([:price])
  |> check_constraint(:discount_price,
       name: :discount_price_must_be_less_than_price,
       message: "must be less than the regular price")
end

Check constraints validate arbitrary database-level rules. The constraint must be defined in your migration with the same name.

Changeset Composition

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

  schema "users" do
    field :email, :string
    field :password, :string, virtual: true
    field :password_hash, :string
    field :role, :string

    timestamps()
  end

  def registration_changeset(user, params \\ %{}) do
    user
    |> cast(params, [:email, :password])
    |> validate_required([:email, :password])
    |> validate_email()
    |> validate_password()
    |> hash_password()
    |> put_change(:role, "user")
  end

  def admin_changeset(user, params \\ %{}) do
    user
    |> cast(params, [:email, :password, :role])
    |> validate_required([:email, :role])
    |> validate_email()
    |> validate_inclusion(:role, ["user", "admin", "moderator"])
    |> maybe_hash_password()
  end

  defp validate_email(changeset) do
    changeset
    |> validate_format(:email, ~r/@/)
    |> unique_constraint(:email)
  end

  defp validate_password(changeset) do
    validate_length(changeset, :password, min: 8)
  end

  defp hash_password(changeset) do
    case get_change(changeset, :password) do
      nil -> changeset
      password -> put_change(changeset, :password_hash, hash(password))
    end
  end

  defp maybe_hash_password(changeset) do
    case get_change(changeset, :password) do
      nil -> changeset
      password -> hash_password(changeset)
    end
  end

  defp hash(password), do: :crypto.hash(:sha256, password)
end

Different changesets can be used for different contexts (registration vs. admin updates). This keeps validation logic focused and prevents unintended changes.

Embedded Changeset Validation

elixir
defmodule MyApp.UserProfile do
  use Ecto.Schema
  import Ecto.Changeset

  embedded_schema do
    field :online, :boolean
    field :dark_mode, :boolean
    field :visibility, Ecto.Enum, values: [:public, :private, :friends_only]
  end

  def changeset(profile, attrs \\ %{}) do
    profile
    |> cast(attrs, [:online, :dark_mode, :visibility])
    |> validate_required([:online, :visibility])
  end
end

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

  schema "users" do
    field :full_name, :string
    field :email, :string

    embeds_one :profile, MyApp.UserProfile

    timestamps()
  end

  def changeset(user, attrs \\ %{}) do
    user
    |> cast(attrs, [:full_name, :email])
    |> cast_embed(:profile, required: true)
  end
end

# Usage
changeset = MyApp.User.changeset(%MyApp.User{}, %{
  full_name: "John Doe",
  email: "john@example.com",
  profile: %{online: true, visibility: :public}
})

changeset.valid?  # true

The cast_embed/3 function validates embedded schemas using their own changeset functions. Validation errors in embedded data propagate to the parent changeset.

Custom Embedded Changeset Function

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

  schema "users" do
    field :full_name, :string
    field :email, :string

    embeds_one :profile, Profile do
      field :online, :boolean
      field :dark_mode, :boolean
      field :visibility, Ecto.Enum, values: [:public, :private, :friends_only]
    end

    timestamps()
  end

  def changeset(user, attrs \\ %{}) do
    user
    |> cast(attrs, [:full_name, :email])
    |> cast_embed(:profile, required: true, with: &profile_changeset/2)
  end

  def profile_changeset(profile, attrs \\ %{}) do
    profile
    |> cast(attrs, [:online, :dark_mode, :visibility])
    |> validate_required([:online, :visibility])
  end
end

The :with option in cast_embed/3 specifies a custom changeset function for the embedded data, allowing specific validation logic.

Embedded Many Validation

elixir
defmodule MyApp.Order do
  use Ecto.Schema
  import Ecto.Changeset

  schema "orders" do
    field :customer_name, :string

    embeds_many :items, OrderItem do
      field :product_name, :string
      field :quantity, :integer
      field :price, :decimal
    end

    timestamps()
  end

  def changeset(order, attrs \\ %{}) do
    order
    |> cast(attrs, [:customer_name])
    |> cast_embed(:items, with: &item_changeset/2)
    |> validate_required([:customer_name])
    |> validate_length(:items, min: 1, message: "must have at least one item")
  end

  defp item_changeset(item, attrs) do
    item
    |> cast(attrs, [:product_name, :quantity, :price])
    |> validate_required([:product_name, :quantity, :price])
    |> validate_number(:quantity, greater_than: 0)
    |> validate_number(:price, greater_than: 0)
  end
end

Collections of embedded schemas are validated using cast_embed/3 with embeds_many. Each item in the collection is validated independently using its changeset function.

Association Changesets with put_assoc

elixir
defmodule MyApp.Post do
  use Ecto.Schema
  import Ecto.Changeset

  schema "posts" do
    field :title, :string
    field :body, :string

    many_to_many :tags, MyApp.Tag,
      join_through: "posts_tags",
      on_replace: :delete

    timestamps()
  end

  def changeset(post, params \\ %{}) do
    post
    |> cast(params, [:title, :body])
    |> validate_required([:title, :body])
    |> put_assoc(:tags, parse_tags(params))
  end

  defp parse_tags(params) do
    (params["tags"] || "")
    |> String.split(",")
    |> Enum.map(&String.trim/1)
    |> Enum.reject(&(&1 == ""))
    |> Enum.map(&get_or_insert_tag/1)
  end

  defp get_or_insert_tag(name) do
    MyApp.Repo.get_by(MyApp.Tag, name: name) ||
      MyApp.Repo.insert!(%MyApp.Tag{name: name})
  end
end

The put_assoc/3 function sets association data on a changeset. When combined with on_replace: :delete, it properly handles adding and removing associations.

Upsert Pattern with Unique Constraints

elixir
defmodule MyApp.Tag do
  use Ecto.Schema
  import Ecto.Changeset

  schema "tags" do
    field :name, :string

    timestamps()
  end

  def changeset(tag, params \\ %{}) do
    tag
    |> cast(params, [:name])
    |> validate_required([:name])
    |> unique_constraint(:name)
  end
end

defp get_or_insert_tag(name) do
  %MyApp.Tag{}
  |> MyApp.Tag.changeset(%{name: name})
  |> MyApp.Repo.insert()
  |> case do
    {:ok, tag} -> tag
    {:error, _} -> MyApp.Repo.get_by!(MyApp.Tag, name: name)
  end
end

This pattern handles race conditions when inserting records with unique constraints. If the insert fails due to a duplicate, it fetches the existing record.

Batch Upsert with insert_all

elixir
defmodule MyApp.Post do
  use Ecto.Schema
  import Ecto.Changeset
  import Ecto.Query

  schema "posts" do
    field :title, :string
    field :body, :string

    many_to_many :tags, MyApp.Tag,
      join_through: "posts_tags",
      on_replace: :delete

    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:title, :body])
    |> put_assoc(:tags, parse_tags(params))
  end

  defp parse_tags(params) do
    (params["tags"] || "")
    |> String.split(",")
    |> Enum.map(&String.trim/1)
    |> Enum.reject(&(&1 == ""))
    |> insert_and_get_all()
  end

  defp insert_and_get_all([]), do: []

  defp insert_and_get_all(names) do
    timestamp = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
    placeholders = %{timestamp: timestamp}

    maps = Enum.map(names, fn name ->
      %{
        name: name,
        inserted_at: {:placeholder, :timestamp},
        updated_at: {:placeholder, :timestamp}
      }
    end)

    MyApp.Repo.insert_all(
      MyApp.Tag,
      maps,
      placeholders: placeholders,
      on_conflict: :nothing
    )

    MyApp.Repo.all(from t in MyApp.Tag, where: t.name in ^names)
  end
end

The insert_all/3 function with on_conflict: :nothing performs bulk upserts efficiently, minimizing database round trips when handling multiple associations.

Traversing Changeset Errors

elixir
defmodule MyApp.ErrorHelpers do
  def error_messages(changeset) do
    Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
      Enum.reduce(opts, msg, fn {key, value}, acc ->
        String.replace(acc, "%{#{key}}", to_string(value))
      end)
    end)
  end
end

# Usage
changeset = MyApp.User.changeset(%MyApp.User{}, %{})
errors = MyApp.ErrorHelpers.error_messages(changeset)
# %{
#   email: ["can't be blank"],
#   username: ["can't be blank"]
# }

The traverse_errors/2 function walks through all errors in a changeset, including nested changesets, allowing you to format error messages for display.

Conditional Validations

elixir
defmodule MyApp.Product do
  use Ecto.Schema
  import Ecto.Changeset

  schema "products" do
    field :name, :string
    field :price, :decimal
    field :discount_price, :decimal
    field :is_on_sale, :boolean

    timestamps()
  end

  def changeset(product, params \\ %{}) do
    product
    |> cast(params, [:name, :price, :discount_price, :is_on_sale])
    |> validate_required([:name, :price])
    |> validate_discount_price()
  end

  defp validate_discount_price(changeset) do
    case get_field(changeset, :is_on_sale) do
      true ->
        changeset
        |> validate_required([:discount_price])
        |> validate_number(:discount_price, less_than: get_field(changeset, :price))

      _ ->
        changeset
    end
  end
end

Conditional validations apply different rules based on changeset data. Use get_field/2 to access current field values including changes and existing data.

Optimistic Locking with Version Fields

elixir
defmodule MyApp.Document do
  use Ecto.Schema
  import Ecto.Changeset

  schema "documents" do
    field :title, :string
    field :content, :string
    field :version, :integer, default: 1

    timestamps()
  end

  def changeset(document, params \\ %{}) do
    document
    |> cast(params, [:title, :content])
    |> validate_required([:title, :content])
    |> optimistic_lock(:version)
  end
end

# Update with version check
document = MyApp.Repo.get!(MyApp.Document, 1)
changeset = MyApp.Document.changeset(document, %{title: "Updated Title"})

case MyApp.Repo.update(changeset) do
  {:ok, updated_document} ->
    # Success, version incremented

  {:error, changeset} ->
    # Stale object error if version doesn't match
    IO.puts("Document was modified by another process")
end

The optimistic_lock/3 function adds version checking to prevent lost updates in concurrent scenarios. The update fails if the version has changed since reading.

Changeset Pipelines for Complex Workflows

elixir
defmodule MyApp.UserRegistration do
  import Ecto.Changeset

  def changeset(params) do
    %MyApp.User{}
    |> MyApp.User.changeset(params)
    |> validate_terms_accepted()
    |> validate_email_verification()
    |> set_initial_role()
    |> send_welcome_email()
  end

  defp validate_terms_accepted(changeset) do
    if get_change(changeset, :terms_accepted) == true do
      changeset
    else
      add_error(changeset, :terms_accepted, "must be accepted")
    end
  end

  defp validate_email_verification(changeset) do
    # Custom email verification logic
    changeset
  end

  defp set_initial_role(changeset) do
    put_change(changeset, :role, "user")
  end

  defp send_welcome_email(changeset) do
    if changeset.valid? do
      # Send email in a separate process
      email = get_change(changeset, :email)
      Task.start(fn -> MyApp.Mailer.send_welcome(email) end)
    end

    changeset
  end
end

Complex workflows can be built as changeset pipelines. Each step validates or transforms data, and the pipeline short-circuits if validation fails.

When to Use This Skill

Use ecto-changesets when you need to:

  • Validate user input before database operations
  • Cast external data to appropriate types
  • Enforce database constraints at the application level
  • Transform data before persistence (e.g., hashing passwords)
  • Handle nested or embedded data validation
  • Manage associations when creating or updating records
  • Provide user-friendly error messages for validation failures
  • Implement different validation rules for different contexts
  • Prevent race conditions with unique constraints
  • Track changes to records for audit purposes
  • Implement optimistic locking for concurrent updates
  • Build complex multi-step data validation workflows

Best Practices

  • Always use changesets for external data, never directly create structs
  • Define multiple changeset functions for different contexts (create, update, admin)
  • Keep changesets focused on validation and casting, not business logic
  • Use validate_required/2 before other validations to provide clear errors
  • Leverage database constraints and map them with constraint functions
  • Use virtual fields for data that shouldn't be persisted
  • Compose changesets using private helper functions
  • Return changesets from failed operations for better error handling
  • Use traverse_errors/2 to format errors for API responses
  • Document why specific validations exist, especially complex custom ones
  • Test changesets independently from database operations
  • Use get_change/2 for conditional logic on modified fields
  • Use get_field/2 when you need current value (changed or existing)
  • Keep embedded changeset functions close to the parent schema
  • Use on_replace option appropriately for association changesets

Common Pitfalls

  • Forgetting to include fields in the cast/3 allowed list
  • Not checking changeset.valid? before calling Repo functions
  • Mixing validation logic with business logic in changeset functions
  • Using put_change/3 instead of validations for constraints
  • Forgetting to add constraint validations for database constraints
  • Not handling race conditions with unique constraints properly
  • Overusing custom validations when built-in validators suffice
  • Mutating changesets instead of returning new ones
  • Not using virtual fields for temporary data like passwords
  • Calling Repo functions inside changeset functions
  • Using change/2 when you need validation with cast/3
  • Forgetting on_replace: :delete for many_to_many associations
  • Not validating embedded schemas separately
  • Hardcoding error messages instead of using metadata
  • Not testing edge cases in custom validations
  • Using get_change/2 when you need get_field/2 (or vice versa)
  • Not setting appropriate constraint names in migrations
  • Ignoring changeset errors in insert/update pipelines
  • Performing side effects in validation functions
  • Not documenting expected params shape for changesets

Resources

Official Ecto Documentation

Validation Functions

Constraint Functions

Association and Embedded Functions

Community Resources

Câu hỏi thường gặp

Kỹ năng AI Ecto Changesets làm được gì?

Use when validating and casting data with Ecto changesets including field validation, constraints, nested changesets, and data transformation. Use for ensuring data integrity before database operations.

Vì sao nên dùng Ecto Changesets trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Ecto Changesets là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Ecto Changesets trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/TheBushidoCollective/han/tree/main/plugins/frameworks/ecto/skills/ecto-changesets. TypingMind đọc tệp SKILL.md rồi cài đặt kỹ năng để bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Ecto Changesets?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Có thể dùng Ecto Changesets với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Ecto Changesets với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Ecto Changesets có miễn phí không?

Kỹ năng này được TheBushidoCollective phát hành trên GitHub. Hãy xem điều khoản giấy phép trong kho mã. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp các công cụ mới mà mô hình có thể gọi. Khi mô hình gọi một công cụ, mã của công cụ đó chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và những tiêu chí mà kết quả cần đáp ứng. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇