1
votes

I'm trying to make a custom validation, so I've made a function that runs in a changeset but I'm getting an error that doesn't seem to make much sense to me at the moment.

Here is my model:

@required_fields ~w(commissioner user_id league_id)a

  def changeset(struct, params \\ %{}) do
    struct
      |> cast(params, @required_fields)
      |> validate_required(@required_fields)
      |> duplicate_check
  end

  def duplicate_check(_struct) do
    IEx.pry
  end

But when I run this code I get this error:

ERROR:

expected a map, got: :ok

BadMapError at GET /leagues/new
1
Try returning _struct from duplicate_check. IEx.pry returns :ok so it's probably that.Dogbert
Perfect. Yea, that makes sense I need to keep passing the struct throughBitwise

1 Answers

2
votes

duplicate_check returns the return value of IEx.pry which is :ok which means changeset now returns :ok which causes that error to be thrown. You need to return the struct from duplicate_check:

def duplicate_check(struct) do
  IEx.pry
  struct
end

Also, you might want to call the variable changeset because after cast you'll have a Changeset, not a struct of the current module.