docs: explain when and why to use before_action? on validations - #2914
Merged
Conversation
Adds a section to the validations guide covering the two reasons to defer a validation to a before_action hook: avoiding repeated expensive checks when the changeset is rebuilt (e.g. on every form change), and running the check inside the transaction at execution time. Also lists the trade-offs (errors only on submit, not atomic, runs after before_transaction hooks). Fixes the actions lifecycle guide, which listed validations as running inside the transaction while the next paragraph said they run before it. Expands the option docstring and regenerates the DSL cheat sheet. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
zachdaniel
approved these changes
Sep 4, 2026
Contributor
|
🚀 Thank you for your contribution! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
before_action?on validations was only documented mechanically ("the validation will be run in a before_action hook") with no explanation of when or why you would want it. The original 1.x name,expensive?, carried the intent, but that was lost in the 2.0 rename.Validations guide: new
before_action? Optionsection explaining that validations normally run when the changeset/query is built, and the two reasons to defer one to execution time:AshPhoenix.Form.validate/3rebuilds the changeset on every change event, so a querying validation would otherwise hit the database on every keystroke.Also lists the trade-offs: errors surface only on submit, it cannot be atomic (
require_atomic? truefails, bulk falls back to:stream), it runs afterbefore_transaction/around_transactionhooks,only_when_valid?is checked at hook time, anddelay_global_validations?is the same thing applied to all global validations.Actions lifecycle guide: the "Inside Transaction" bullet listed action/global validations, contradicting the paragraph below it which says they run during changeset creation before the transaction. Corrected to hooks plus
before_action?validations.Option docstring in
Ash.Resource.Validationexpanded, DSL cheat sheet regenerated.Behavior verified against
Ash.Changeset.validate/5(hook wrapping),Ash.Changeset.with_hooks/3(transaction boundary) andAshPhoenix.Form.validate/3.Test plan
mix spark.cheat_sheetsregenerated cleanlymix test test/resource/validation_test.exs test/actions/bulk/bulk_update_test.exs test/actions/update_test.exs(105 passed)🤖 Generated with Claude Code