CargoSense / vex

Data Validation for Elixir
MIT License
595 stars 60 forks source link

protocol Vex.Extract not implemented for %User{...} #42

Closed jakub-zawislak closed 7 years ago

jakub-zawislak commented 7 years ago

This example from docs doesn't work https://github.com/CargoSense/vex#in-structs

defmodule User do
  defstruct username: nil, password: nil, password_confirmation: nil
  use Vex.Struct

  validates :username, presence: true,
                       length: [min: 4],
                       format: ~r/^[[:alpha:]][[:alnum:]]+$/
  validates :password, length: [min: 4],
                       confirmation: true
end
iex(12)> user = %User{username: "actualuser",
...(12)>              password: "abcdefghi",
...(12)>              password_confirmation: "abcdefghi"}
%User{password: "abcdefghi", password_confirmation: "abcdefghi",
 username: "actualuser"}
iex(13)> Vex.valid?(user)
** (Protocol.UndefinedError) protocol Vex.Extract not implemented for %User{password: "abcdefghi", password_confirmation: "abcdefghi", username: "actualuser"}
    lib/vex/extract.ex:1: Vex.Extract.impl_for!/1
    lib/vex/extract.ex:4: Vex.Extract.settings/1
    lib/vex.ex:4: Vex.valid?/1
astery commented 7 years ago

Protocols are consolidated at the moment iex is started. In order to play with protocols in console, you can disable it in project configuration, look here - https://hexdocs.pm/mix/Mix.Tasks.Compile.Protocols.html.

Also, try newer version of elixir, 1.4.5 give nice warning about it: warning: the Vex.Extract protocol has already been consolidated, an implementation for User has no effect

benwilson512 commented 7 years ago

Did you define the module within iex ?

jakub-zawislak commented 7 years ago

I created this module in a .ex file, inside a Phoenix project. Phoenix was configured to refresh page after every change so I'm not restarting it, but I forgot to recompile/restart server before playing with vex in the console. My bad. But finally my issue solved! Thanks