New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the type that was validated in errors generated by `Changeset.validate_length/3` #2913

Merged
merged 1 commit into from Feb 7, 2019

Conversation

Projects
None yet
3 participants
@timrogers
Copy link
Contributor

timrogers commented Feb 7, 2019

Errors in an Ecto.Changeset have a key, a message and additional contextual information provided in the form of a keyword list.

Ecto.Changeset comes bundled with a number of validations, for example validate_length/3.

The built-in validations tend to include additional contextual information - for example, validate_length/3 includes count and kind. So, for example, if the validation was configured to check that a field was "no more than 3" and it fails, then you will have [count: 3, validation: :length, kind: :max].

The validate_length validation is interesting, because it can operate on strings, binaries and lists. However, it doesn't expose in the additional context what type of data was validated.

This context can be valuable, for example if you want your errors to be fully machine-readable.

This exposes [type: :string], [type: :list] or [type: :binary], as appropriate, on the error.

Expose the type that was validated in errors generated by `Changeset.…
…validate_length/3`

Errors in an `Ecto.Changeset` have a key, a message and additional
contextual information provided in the form of a keyword list.

`Ecto.Changeset` comes bundled with a number of validations,
for example `validate_length/3`.

The built-in validations tend to include additional contextual
information - for example, `validate_length/3` includes `count`
and `kind`. So, for example, if the validation was configured to
check that a field was "no more than 3" and it fails, then you will
have `[count: 3, validation: :length, kind: :max]`.

The `validate_length` validation is interesting, because it can
operate on strings, binaries and lists. However, it doesn't expose
in the additional context what type of data was validated.

This context can be valuable, for example if you want your errors
to be fully machine-readable.

This exposes `[type: :string]`, `[type: :list]` or `[type: :binary]`,
as appropriate, on the error.
@@ -1953,27 +1953,27 @@ defmodule Ecto.Changeset do

defp wrong_length(_type, value, value, _opts), do: nil
defp wrong_length(:string, _length, value, opts), do:

This comment has been minimized.

@timrogers

timrogers Feb 7, 2019

Author Contributor

We could potentially pattern-match this if preferred:

defp wrong_length(:string = type, _length, value, opts), do:
  {message(opts, "should be %{count} character(s)"), count: value, validation: :length, kind: :is, type: :string}
end

@josevalim josevalim merged commit ecf0e93 into elixir-ecto:master Feb 7, 2019

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment