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

Represents a validation in Ash.

See Ash.Resource.Validation.Builtin 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
  validation {MyValidation, [foo: :bar]}
end

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}
  end
end
defmodule MyResource do
  ...

  import MyValidation

  validations do
    validate my_validation(:foo)
  end
end

Link to this section Summary

Link to this section Types

Specs

path() :: [atom() | integer()]

Specs

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

Link to this section Functions

Link to this section Callbacks

Specs

init(Keyword.t()) :: {:ok, Keyword.t()} | {:error, String.t()}

Specs

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