Skip to content

feat: add batch_validate callback to Ash.Resource.Validation#2650

Merged
zachdaniel merged 1 commit intoash-project:mainfrom
RylandBangerter85:feat/batch-validate
Mar 29, 2026
Merged

feat: add batch_validate callback to Ash.Resource.Validation#2650
zachdaniel merged 1 commit intoash-project:mainfrom
RylandBangerter85:feat/batch-validate

Conversation

@RylandBangerter85
Copy link
Copy Markdown
Contributor

@RylandBangerter85 RylandBangerter85 commented Mar 25, 2026

Summary

  • Adds batch_validate/3, before_batch/3, after_batch/3, and batch_callbacks?/3 callbacks to Ash.Resource.Validation, mirroring the existing pattern on Ash.Resource.Change
  • Wires batch validation into bulk create, update, and destroy (via delegation) so validations can process entire batches at once instead of one record at a time
  • Fully backwards compatible - existing validations are unaffected; new code path only activates when batch_validate/3 is defined

Closes #1405

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

@zachdaniel
Copy link
Copy Markdown
Contributor

Nice! One big thing before I dive in to review: there should be no need for validations to run after the action 😄. We should really only need before_batch and batch_validate

@RylandBangerter85
Copy link
Copy Markdown
Contributor Author

Okay it should be updated!

Comment thread lib/ash/resource/validation.ex Outdated
@doc """
Runs on each batch before it is dispatched to the data layer.
"""
@callback before_batch(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should remove these callbacks now.

Comment thread lib/ash/actions/update/bulk.ex Outdated
|> Enum.reduce(batch, fn
{%{validation: _}, _index}, batch ->
batch
{%{validation: {module, validation_opts}}, _index}, batch ->
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This logic is getting more complex now and is repeated. Lets move it out into BatchHelpers?

Copy link
Copy Markdown
Contributor

@zachdaniel zachdaniel left a comment

Choose a reason for hiding this comment

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

Looks good, almost there! Also a merge conflict to investigate.

Validations now support batch processing during bulk operations via
batch_validate/3, before_batch/3, and after_batch/3, mirroring the
existing pattern on Ash.Resource.Change. This allows validations to
process entire batches at once instead of per-changeset, enabling
optimizations like batched external API calls or cross-record checks.

Made-with: Cursor
@zachdaniel zachdaniel merged commit 814c175 into ash-project:main Mar 29, 2026
40 of 45 checks passed
zachdaniel pushed a commit that referenced this pull request Mar 29, 2026
Validations now support batch processing during bulk operations via
batch_validate/3, mirroring the existing pattern on Ash.Resource.Change.
This allows validations to process entire batches at once instead of
per-changeset, enabling optimizations like batched external API calls or
cross-record checks.
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.

Error on get started

2 participants