View Source Ash.Resource.Validation behaviour (ash v1.53.3)

Represents a validation in Ash.

See Ash.Resource.Validation.Builtins for a list of builtin validations.

To write your own validation, define a module that implements the init/1 callback to validate options at compile time, and validate/2 callback to do the validation.

Then, in a resource, you can say:

validations do
  validate {MyValidation, [foo: :bar]}

To make it more readable, you can define a function in the module that returns that tuple, and import it into your resource.

defmodule MyValidation do
  def my_validation(value) do
    {__MODULE__, foo: value}
defmodule MyResource do

  import MyValidation

  validations do
    validate my_validation(:foo)

Link to this section Summary

Link to this section Types

@type path() :: [atom() | integer()]
@type t() :: %Ash.Resource.Validation{
  before_action?: term(),
  description: String.t() | nil,
  expensive?: boolean(),
  message: term(),
  module: atom(),
  on: [atom()],
  opts: [atom()],
  validation: {atom(), [atom()]},
  where: [{atom(), [atom()]}]

Link to this section Callbacks

@callback init(Keyword.t()) :: {:ok, Keyword.t()} | {:error, String.t()}
@callback validate(Ash.Changeset.t(), Keyword.t()) :: :ok | {:error, term()}

Link to this section Functions