Search documentation of ash

# Ash.Resource.Validation behaviour (ash v3.4.71)

View Source

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/3` callback to do the validation.

Then, in a resource, you can say:

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

# Summary

## Types

path()
ref()
t()

## Callbacks

atomic(changeset, opts, context)
atomic?()
describe(opts)
has_validate?()
init(opts)
validate(changeset, opts, context)

## Functions

action_schema()
opt_schema()
validation_type()

# Types

# path()

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

# ref()

    @type ref() :: {module(), Keyword.t()} | module()

# t()

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

# Callbacks

# atomic(changeset, opts, context)

(optional)

    @callback atomic(
      changeset :: Ash.Changeset.t(),
      opts :: Keyword.t(),
      context :: Ash.Resource.Validation.Context.t()
    ) ::
      :ok
      | {:atomic, involved_fields :: [atom()] | :*, condition_expr :: Ash.Expr.t(),
         error_expr :: Ash.Expr.t()}
      | [
          {:atomic, involved_fields :: [atom()] | :*,
           condition_expr :: Ash.Expr.t(), error_expr :: Ash.Expr.t()}
        ]
      | {:not_atomic, String.t()}
      | {:error, term()}

# atomic?()

    @callback atomic?() :: boolean()

# describe(opts)

(optional)

    @callback describe(opts :: Keyword.t()) ::
      String.t() | [message: String.t(), vars: Keyword.t()]

# has_validate?()

    @callback has_validate?() :: boolean()

# init(opts)

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

# validate(changeset, opts, context)

(optional)

    @callback validate(
      changeset :: Ash.Changeset.t(),
      opts :: Keyword.t(),
      context :: Ash.Resource.Validation.Context.t()
    ) :: :ok | {:error, term()}

# Functions

# action_schema()

# opt_schema()

# validation_type()