ecto-changeset-patterns
Installation
SKILL.md
Ecto Changeset Patterns
RULES — Follow these with no exceptions
- Create separate named changesets per operation —
registration_changeset,email_changeset,password_changeset; never overload a singlechangeset/2 - Never require foreign key fields in
cast_assocchild changesets — the parent sets them automatically; requiring them causes "can't be blank" errors - Compose changesets with pipes — each validation step is a separate function for reuse and clarity
- Use
unsafe_validate_uniquepaired withunique_constraint— never one without the other;unsafe_validate_uniquegives fast UI feedback,unique_constrainthandles race conditions - Use
update_change/3for field transformations — trimming, downcasing, slugifying happen in the changeset, never in the controller or context - Accept
opts \\ []for conditional validation — allows callers to toggle validation rules without creating yet another changeset function - Validate at the changeset level, not in context functions — context functions should be thin wrappers around
Repocalls
Separate Changesets Per Operation
Different operations need different validation rules. Don't overload changeset/2.