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
endFields an action opens up via allow_nil_input 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.
The check also skips a field when an update or destroy action has a
same-named argument. The atomic execution path is the default for
those action types (require_atomic? defaults to true), and in
that path the built-in present validation validates the argument
instead of the attribute. The validation then enforces that the
caller supplied the argument, regardless of the attribute
constraint.
In the non-atomic path the validation falls back to the attribute when the argument is nil, where it 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 not examined either,
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.