Skip to content

fix: bulk action global validation ordering and before_action? support#2623

Closed
nallwhy wants to merge 3 commits intoash-project:mainfrom
nallwhy:fix/bulk-create-validation-ordering-and-before-action
Closed

fix: bulk action global validation ordering and before_action? support#2623
nallwhy wants to merge 3 commits intoash-project:mainfrom
nallwhy:fix/bulk-create-validation-ordering-and-before-action

Conversation

@nallwhy
Copy link
Contributor

@nallwhy nallwhy commented Mar 11, 2026

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

Summary

  • Fix global validation/change ordering in bulk_create, bulk_update, and bulk_destroy to match non-bulk behavior
  • Add before_action? support for validations in the bulk create and bulk update pipelines

Problem

Two bugs across all bulk action modules (Ash.Actions.Create.Bulk, Ash.Actions.Update.Bulk, Ash.Actions.Destroy.Bulk):

  1. Reversed ordering: Global validations were concatenated before global changes in pre_template_all_changes.
    The non-bulk path runs action changes + global changes first, then global validations,
    but the bulk path was concatenating in action changes → global validations → global changes order.

  2. before_action? ignored: The before_action? flag on validations was completely ignored in the bulk path, causing them to run inline.
    The non-bulk path checks this flag and defers via Ash.Changeset.before_action/2,
    but the bulk run_action_changes never read this field.

Changes

lib/ash/actions/create/bulk.ex

  • pre_template_all_changes: Reorder concat so global changes come before global validations
  • run_action_changes: Check before_action? on validations; when true, register via Ash.Changeset.before_action/2 instead of running inline
  • Extract inline validation logic into run_bulk_validation/6
  • Simplify validation.message error handling to reduce duplication

lib/ash/actions/update/bulk.ex

  • pre_template_all_changes: Same ordering fix as bulk_create
  • run_action_changes: Add before_action? support for validations
  • Simplify validation.message error handling in validate_batch_non_atomically

lib/ash/actions/destroy/bulk.ex

  • pre_template_all_changes: Same ordering fix as bulk_create
  • before_action? support inherited via delegation to Ash.Actions.Update.Bulk.run_action_changes

Copilot AI review requested due to automatic review settings March 11, 2026 05:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes bulk create’s change/validation pipeline so it matches the non-bulk for_create behavior, including honoring before_action? validations.

Changes:

  • Reorders global changes vs global validations in bulk_create so global changes run before global validations.
  • Adds before_action? support for bulk validations by registering them via Ash.Changeset.before_action/2.
  • Adds regression tests covering both ordering and before_action? behavior for global validations.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/ash/actions/create/bulk.ex Fixes global change/validation ordering and defers before_action? validations via changeset hooks (with extracted helper).
test/actions/bulk/bulk_create_test.exs Adds coverage to ensure global changes precede global validations and before_action? validations behave like the non-bulk pipeline.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nallwhy nallwhy force-pushed the fix/bulk-create-validation-ordering-and-before-action branch from 9e839b0 to c9a44fa Compare March 11, 2026 08:41
@nallwhy nallwhy marked this pull request as draft March 11, 2026 12:42
@nallwhy nallwhy force-pushed the fix/bulk-create-validation-ordering-and-before-action branch from af9f175 to a1fb430 Compare March 11, 2026 12:48
@nallwhy nallwhy force-pushed the fix/bulk-create-validation-ordering-and-before-action branch from 516bdf0 to ad92994 Compare March 11, 2026 12:53
@nallwhy nallwhy changed the title fix: bulk_create global validation ordering and before_action? support fix: bulk action global validation ordering and before_action? support Mar 11, 2026
@nallwhy nallwhy marked this pull request as ready for review March 11, 2026 12:59
@zachdaniel
Copy link
Contributor

Can we separate these two changes? The reason for that is that before_action can't necessarily be supported in bulk_update actions depending on how they are running and it would be nice to consider these separately. Thanks!

@nallwhy nallwhy closed this Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants