Hello everyone, I am new to elixir and phoenix but I’ve been already fallen in love with them, but unfortunately I got stuck in a problem when I was developing a validation for my first API with Absinthe. When I just list all accounts, or create a new account without failing on any validation everything works fine, but
Whenever I tried to validate the current_balance > 0, and it is equals or lower than 0 in a custom validation on the changeset I get the error:
Request: POST /api/graphiql
** (exit) an exception was raised:
** (Protocol.UndefinedError) protocol Enumerable not implemented for #Ecto.Changeset
the code of the account schema I’ve trying to implement its followed above:
defmodule GraphBanking.Accounts.Account do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "accounts" do
field(:current_balance, :float)
timestamps()
end
@doc false
def changeset(account, attrs) do
account
|> cast(attrs, [:current_balance])
|> validate_required([:current_balance])
|> validate_balance()
end
defp validate_balance(changeset) do
currentBalance = get_change(changeset, :current_balance)
IO.inspect(currentBalance)
if(currentBalance <= 0) do
changeset
|> add_error(:current_balance, "It must be higher than 0")
else
changeset
end
end
end
Any help would be more than appreciated