AshCredo.Check.Warning.RedundantValidation (ash_credo v0.15.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. 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.