Possible error in Changeset Docs

Hi All.

I’ve recently needed to use Ecto.Changeset.validate_change and I “think” I may have found a problem in the documentation. The docs state that you should return a list of errors and shows an example [title: “cannot be foo”] but I believe this should be [{title: “cannot be foo”}].

I’ve no experience in raising issues and just wanted to check with you good people that simply raising something on the appropriate Github page is the thing to do.



What makes you think so?

The example you copied from the docs looks like idiomatic elixir code while yours is a list with a one-tuple containing another keyword list.

Also as far as I remember, the copied example matches the format of errors in the changeset as far as I remember.

Hi @NobbZ - when I tried the example I received an error

##### FunctionClauseError <small>at POST</small> <small>/cost_centres</small>

# no function clause matching in anonymous fn/1 in Ecto.Changeset.validate_change/3

My code:

  def validate_uppercase(changeset, field) do
    IO.puts "about to validate uppercase"
    validate_change changeset, field, fn field, value ->
        if (String.upcase(value) != value) do
            [field, "value must be uppercase"]

If I change the return error to

[{field, "value must be uppercase"}]

then all is good.



[foo: "bar"] is syntactical sugar for [{:foo, "bar}].

And since you can not use that syntactical sugar when the key is a variable, you have to write out the tuple on your own as [{key, message}].

Still the documentation is correct.

iex(1)> key = :foo
iex(2)> [{key, "value"}]
[foo: "value"]

Ah that makes perfect sense. I knew it was a good idea to check here first.

Many thanks for your help @NobbZ.



1 Like