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.

cheers

Dave

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"]
        else
            []
        end
    end 
end

If I change the return error to

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

then all is good.

cheers

Dave

[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
:foo
iex(2)> [{key, "value"}]
[foo: "value"]
3 Likes

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

Many thanks for your help @NobbZ.

cheers

Dave

1 Like