ecto-changesets

Validate and cast data into Ecto changesets before database operations.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill ecto-changesets
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ecto-changesets
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-ecto/skills/ecto-changesets
Command: npx skills add https://github.com/TheBushidoCollective/han --skill ecto-changesets

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill helps you master Ecto changesets to validate, cast, and transform data before database operations, ensuring data integrity and providing user-friendly error messages in Elixir applications. It automates data validation and transformation workflows.

Core Features & Use Cases

  • Data Casting & Validation: Filter and convert external parameters to schema types, and apply built-in or custom validations (e.g., required fields, format, length, number ranges).
  • Constraint Handling: Map database-level constraints (unique, foreign key, check) to changeset errors, providing clear feedback to users.
  • Nested & Associated Changesets: Validate embedded schemas and manage associations (e.g., many_to_many tags) within a single changeset, ensuring consistency across related data.
  • Use Case: A user is registering on your platform. Use this Skill to create a User changeset that validates email format, password strength, ensures the email is unique, and hashes the password before saving. If any validation fails, the changeset provides specific error messages, guiding the user to correct their input.

Quick Start

Use the ecto-changesets skill to create a changeset for a User struct, casting name and email parameters and validating that both are required.

Frequently Asked Questions about ecto-changesets

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I validate and cast data before saving to the database in Elixir?

Ecto changesets validate and cast external parameters into schema types, applying field validations like required, format, and number constraints before database operations. Use `cast/3` to convert incoming data, then chain validators like `validate_required/2` and `validate_format/3` to enforce rules and return structured error messages if validation fails.

What's the best way to handle unique email validation and password constraints in Elixir?

Changesets map database-level constraints (unique, foreign key, check) to user-friendly error messages. Define validations in your schema, apply custom validators for password strength, and let `Repo.insert/1` or `Repo.update/1` catch constraint violations, surfacing them as changeset errors for clear user feedback.

Can I validate associated data and nested schemas in a single changeset?

Yes. Ecto changesets support validating embedded schemas and managing associations like `many_to_many` tags within one changeset, ensuring consistency across related data. Use association helpers to cast and validate nested records alongside parent schema validations in a single operation.

How do I apply custom validation logic beyond built-in validators?

Create custom validators by writing functions that accept a changeset and return a modified changeset with added errors. Chain custom validators with built-in ones like `validate_inclusion/3` and `validate_number/3` to enforce domain-specific business rules and data integrity requirements.

Why does my changeset validation pass but the database insert fails?

Changeset validation catches schema-level errors early, but database-level constraints (unique indexes, foreign keys, check constraints) trigger only during `Repo.insert/1` or `Repo.update/1`. Add changeset-level constraint validations or use `unique_constraint/2` to translate database errors into user-facing messages before insertion.