Skip to content

Commit cba84cc

Browse files
committed
Revert "Ensure type is fetched for field in validate inclusion, subset, and exclusion (#3742)"
This reverts commit 32afd9e. Also reverts commit c8d86c2, manually.
1 parent 43624c0 commit cba84cc

File tree

2 files changed

+5
-64
lines changed

2 files changed

+5
-64
lines changed

lib/ecto/changeset.ex

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,10 +2008,7 @@ defmodule Ecto.Changeset do
20082008
@spec validate_inclusion(t, atom, Enum.t, Keyword.t) :: t
20092009
def validate_inclusion(changeset, field, data, opts \\ []) do
20102010
validate_change changeset, field, {:inclusion, data}, fn _, value ->
2011-
type =
2012-
changeset.types
2013-
|> Map.fetch!(field)
2014-
|> Ecto.Type.type()
2011+
type = Map.fetch!(changeset.types, field)
20152012

20162013
if Ecto.Type.include?(type, value, data),
20172014
do: [],
@@ -2039,10 +2036,7 @@ defmodule Ecto.Changeset do
20392036
@spec validate_subset(t, atom, Enum.t, Keyword.t) :: t
20402037
def validate_subset(changeset, field, data, opts \\ []) do
20412038
validate_change changeset, field, {:subset, data}, fn _, value ->
2042-
{:array, element_type} =
2043-
changeset.types
2044-
|> Map.fetch!(field)
2045-
|> Ecto.Type.type()
2039+
{:array, element_type} = Map.fetch!(changeset.types, field)
20462040

20472041
case Enum.any?(value, fn element -> not Ecto.Type.include?(element_type, element, data) end) do
20482042
true -> [{field, {message(opts, "has an invalid entry"), [validation: :subset, enum: data]}}]
@@ -2066,10 +2060,7 @@ defmodule Ecto.Changeset do
20662060
@spec validate_exclusion(t, atom, Enum.t, Keyword.t) :: t
20672061
def validate_exclusion(changeset, field, data, opts \\ []) do
20682062
validate_change changeset, field, {:exclusion, data}, fn _, value ->
2069-
type =
2070-
changeset.types
2071-
|> Map.fetch!(field)
2072-
|> Ecto.Type.type()
2063+
type = Map.fetch!(changeset.types, field)
20732064

20742065
if Ecto.Type.include?(type, value, data), do:
20752066
[{field, {message(opts, "is reserved"), [validation: :exclusion, enum: data]}}], else: []

test/ecto/changeset_test.exs

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,6 @@ defmodule Ecto.ChangesetTest do
3636
end
3737
end
3838

39-
defmodule CustomSlug do
40-
use Ecto.Type
41-
42-
def type, do: :string
43-
def cast(val), do: {:ok, val}
44-
def load(val), do: {:ok, val}
45-
def dump(val), do: {:ok, val}
46-
end
47-
48-
defmodule CustomTag do
49-
use Ecto.Type
50-
51-
def type, do: {:array, :string}
52-
def cast(val) when is_list(val), do: {:ok, val}
53-
def cast(_), do: :error
54-
def load(val) when is_list(val), do: {:ok, val}
55-
def load(_), do: :error
56-
def dump(val) when is_list(val), do: {:ok, val}
57-
def dump(_), do: :error
58-
end
59-
6039
defmodule Email do
6140
use Ecto.Type
6241

@@ -88,14 +67,12 @@ defmodule Ecto.ChangesetTest do
8867
field :token, :integer, primary_key: true
8968
field :title, :string, default: ""
9069
field :author_email, Email
91-
field :slug, CustomSlug
9270
field :body
9371
field :uuid, :binary_id
9472
field :color, :binary
9573
field :decimal, :decimal
9674
field :upvotes, :integer, default: 0
9775
field :topics, {:array, :string}
98-
field :tags, CustomTag
9976
field :virtual, :string, virtual: true
10077
field :published_at, :naive_datetime
10178
field :source, :map
@@ -120,7 +97,7 @@ defmodule Ecto.ChangesetTest do
12097
end
12198

12299
defp changeset(schema \\ %Post{}, params) do
123-
cast(schema, params, ~w(id token title author_email slug body upvotes decimal color topics tags virtual)a)
100+
cast(schema, params, ~w(id token title author_email body upvotes decimal color topics virtual)a)
124101
end
125102

126103
defmodule CustomError do
@@ -1073,15 +1050,7 @@ defmodule Ecto.ChangesetTest do
10731050
assert changeset.valid?
10741051
end
10751052

1076-
test "validate_inclusion/3 with custom type" do
1077-
changeset =
1078-
changeset(%{"slug" => "foo"})
1079-
|> validate_inclusion(:slug, ~w(foo))
1080-
assert changeset.valid?
1081-
assert changeset.errors == []
1082-
assert validations(changeset) == [slug: {:inclusion, ~w(foo)}]
1083-
1084-
# type with custom equal function
1053+
test "validate_inclusion/3 with custom type and custom equal function" do
10851054
changeset =
10861055
changeset(%{"author_email" => "[email protected]"})
10871056
|> validate_inclusion(:author_email, ["[email protected]"])
@@ -1121,16 +1090,6 @@ defmodule Ecto.ChangesetTest do
11211090
assert changeset.valid?
11221091
end
11231092

1124-
test "validate_subset/3 with custom type" do
1125-
changeset =
1126-
changeset(%{"tags" => ["cute", "animals"]})
1127-
|> validate_subset(:tags, ~w(cute animals))
1128-
1129-
assert changeset.valid?
1130-
assert changeset.errors == []
1131-
assert validations(changeset) == [tags: {:subset, ~w(cute animals)}]
1132-
end
1133-
11341093
test "validate_exclusion/3" do
11351094
changeset =
11361095
changeset(%{"title" => "world"})
@@ -1161,15 +1120,6 @@ defmodule Ecto.ChangesetTest do
11611120
assert changeset.errors == [value: {"is reserved", [validation: :exclusion, enum: decimals]}]
11621121
end
11631122

1164-
test "validate_exclusion/3 with custom type" do
1165-
changeset =
1166-
changeset(%{"slug" => "sun"})
1167-
|> validate_exclusion(:slug, ~w(moon))
1168-
assert changeset.valid?
1169-
assert changeset.errors == []
1170-
assert validations(changeset) == [slug: {:exclusion, ~w(moon)}]
1171-
end
1172-
11731123
test "validate_length/3 with string" do
11741124
changeset = changeset(%{"title" => "world"}) |> validate_length(:title, min: 3, max: 7)
11751125
assert changeset.valid?

0 commit comments

Comments
 (0)