Cannot invoke remote function Regex.match?/2 inside guard

Hello, I wanted to create custom guard like this , but I have an error:

cannot invoke remote function Regex.match?/2 inside guard

guard:

defmodule TrangellHtmlSite.Blog.GuardCustom do
  defmacro amount_cheaker(number) do
    quote do
      Regex.match?(~r/^([+-]?[1-9]\d*|0)$/, unquote(number))
    end
  end
end


---

defmodule TrangellHtmlSiteWeb.PayController do
  use TrangellHtmlSiteWeb, :controller
  alias TrangellHtmlSite.Blog.ZarinPal
  import TrangellHtmlSite.Blog.GuardCustom, only: [amount_cheaker: 1]

  def check_pay(conn, %{"amount" => amount}) when amount_cheaker(amount) do
    conn = put_session(conn, :amount, amount)
    %{"{http://zarinpal.com/}Authority" => authority, "{http://zarinpal.com/}Status" =>  status} = ZarinPal.zarinpal_send(amount)
    check_status_send(conn, status, authority)
  end
end

How do I fix this?

1 Like

You can’t. This is a limitation of the BEAM.

2 Likes

You might want to read this.

https://hexdocs.pm/elixir/guards.html

3 Likes