AshCredo.Check.Warning.RedundantValidation (ash_credo v0.16.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of normal and works with any version of Elixir.

Explanation

Flags validate present(...) where every referenced field is an attribute that already has allow_nil? false. The attribute constraint guarantees presence on its own, so the validation can only ever duplicate an error the changeset already carries. Straight from Ash's usage rules: avoid validations that duplicate attribute constraints.

# Bad - allow_nil? false already rejects nil
attributes do
  attribute :name, :string, allow_nil?: false
end

actions do
  create :create do
    validate present(:name)
  end
end

# Good - let the attribute constraint handle it
attributes do
  attribute :name, :string, allow_nil?: false
end

Fields opened up via allow_nil_input on an action are skipped: there the attribute may legitimately be nil at validation time (for example filled by the data layer during an upsert), so present does add a real constraint. Fields shadowed by a same-named argument on an update or destroy action are skipped too. In the atomic execution path - the default for those action types (require_atomic? defaults to true) - Ash.Resource.Validation.Present validates the argument instead of the attribute, so the validation enforces "the caller supplied the argument" regardless of the attribute constraint. (The non-atomic path falls back to the attribute when the argument is nil, where the validation is indeed vacuous - but advising removal would silently drop the atomic-path constraint, so the check stays silent.) Create actions never execute atomically, so a same-named argument on a create does not rescue the validation and no escape applies there. Validations in read and generic actions are also skipped, because present resolves against action arguments there, not attributes.

Requirements

Your project must be compiled before running mix credo. If Ash is not available in the VM running Credo, the check is a no-op and emits a single diagnostic.

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.