Ash FrameworkAF
Ash Framework6mo ago
14 replies
jonas_h

How to create an atomic validation for the amount on AshMoney?

I have an attribute using AshMoney like so:

    attribute :amount, :money do
      constraints storage_type: :money_with_currency
      allow_nil? false
    end


And I'd like to validate that the amount part is non-zero, ignoring the currency. I've managed to create a custom validation for it but it's not atomic:

  @impl true
  def validate(changeset = %Ash.Changeset{}, _opts, _context) do
    amount = Ash.Changeset.get_attribute(changeset, :amount)

    if Decimal.eq?(amount.amount, Decimal.new(0)) do
      {:error, field: :amount, message: "can't be zero"}
    else
      :ok
    end
  end
Was this page helpful?