Skip to content

Commit 18954e6

Browse files
authored
Merge pull request #541 from danschultzer/only-warn-primitives
Only warn when using Ecto primitives
2 parents ebe73e8 + 6ab2425 commit 18954e6

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
* [`Pow.Plug.Base`] Will now use the existing `:pow_config` in the `conn` when no plug options has been set
88
* [`PowInvitation.Phoenix.InvitationController`] Fixed bug where user was incorrectly redirected to the show action with unsigned token when user struct has no e-mail
9+
* [`Pow.Ecto.Schema`] Now only emits warning for primitive Ecto types
910

1011
### Bug fixes
1112

lib/pow/ecto/schema.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ defmodule Pow.Ecto.Schema do
119119
`elixirc_options: [warnings_as_errors: true]` to the project options in
120120
`mix.exs`.
121121
122+
The warning is also emitted if the field has an invalid primitive Ecto type.
123+
It'll not be emitted for custom Ecto types.
124+
122125
## Customize Pow changeset
123126
124127
You can extract individual changeset methods to modify the changeset flow
@@ -146,7 +149,7 @@ defmodule Pow.Ecto.Schema do
146149
`use Pow.Ecto.Schema, ...` call. This can be fetched by using the
147150
`@pow_config` module attribute.
148151
"""
149-
alias Ecto.Changeset
152+
alias Ecto.{Changeset, Type}
150153
alias Pow.Config
151154

152155
defmodule SchemaError do
@@ -394,7 +397,11 @@ defmodule Pow.Ecto.Schema do
394397
defp missing_field?({name, type}, ecto_fields, _changeset_fields),
395398
do: missing_field?(name, type, ecto_fields)
396399
defp missing_field?(name, type, existing_fields) do
397-
not Enum.member?(existing_fields, {name, type})
400+
not Enum.any?(existing_fields, fn
401+
{^name, ^type} -> true
402+
{^name, e_type} -> not Type.primitive?(e_type)
403+
_any -> false
404+
end)
398405
end
399406

400407
defp warn_missing_fields_error(module, field_defs) do

test/pow/ecto/schema_test.exs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,57 @@ defmodule Pow.Ecto.SchemaTest do
113113
field :password, :string, [virtual: true]
114114
"""
115115
end
116+
117+
test "warns invalid fields defined" do
118+
assert CaptureIO.capture_io(:stderr, fn ->
119+
defmodule InvalidFieldUser do
120+
use Ecto.Schema
121+
use Pow.Ecto.Schema
122+
123+
schema "users" do
124+
field :email, :utc_datetime
125+
field :password_hash, :string
126+
field :current_password, :string, virtual: true
127+
field :password, :string, virtual: true
128+
129+
timestamps()
130+
end
131+
end
132+
end) =~
133+
"""
134+
Please define the following field(s) in the schema for Pow.Ecto.SchemaTest.InvalidFieldUser:
135+
136+
field :email, :string, [null: false]
137+
"""
138+
end
139+
140+
test "doesn't warn for field with custom type" do
141+
assert CaptureIO.capture_io(:stderr, fn ->
142+
defmodule CustomType do
143+
use Ecto.Type
144+
145+
def type, do: :binary
146+
147+
def cast(value), do: {:ok, value}
148+
149+
def load(value), do: {:ok, value}
150+
151+
def dump(value), do: {:ok, value}
152+
end
153+
154+
defmodule CustomFieldTypeUser do
155+
use Ecto.Schema
156+
use Pow.Ecto.Schema
157+
158+
schema "users" do
159+
field :email, CustomType
160+
field :password_hash, :string
161+
field :current_password, :string, virtual: true
162+
field :password, :string, virtual: true
163+
164+
timestamps()
165+
end
166+
end
167+
end) == ""
168+
end
116169
end

0 commit comments

Comments
 (0)