Skip to content

Commit

Permalink
Merge pull request #541 from danschultzer/only-warn-primitives
Browse files Browse the repository at this point in the history
Only warn when using Ecto primitives
  • Loading branch information
danschultzer committed Jul 6, 2020
2 parents ebe73e8 + 6ab2425 commit 18954e6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

### Bug fixes

Expand Down
11 changes: 9 additions & 2 deletions lib/pow/ecto/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ defmodule Pow.Ecto.Schema do
`elixirc_options: [warnings_as_errors: true]` to the project options in
`mix.exs`.
The warning is also emitted if the field has an invalid primitive Ecto type.
It'll not be emitted for custom Ecto types.
## Customize Pow changeset
You can extract individual changeset methods to modify the changeset flow
Expand Down Expand Up @@ -146,7 +149,7 @@ defmodule Pow.Ecto.Schema do
`use Pow.Ecto.Schema, ...` call. This can be fetched by using the
`@pow_config` module attribute.
"""
alias Ecto.Changeset
alias Ecto.{Changeset, Type}
alias Pow.Config

defmodule SchemaError do
Expand Down Expand Up @@ -394,7 +397,11 @@ defmodule Pow.Ecto.Schema do
defp missing_field?({name, type}, ecto_fields, _changeset_fields),
do: missing_field?(name, type, ecto_fields)
defp missing_field?(name, type, existing_fields) do
not Enum.member?(existing_fields, {name, type})
not Enum.any?(existing_fields, fn
{^name, ^type} -> true
{^name, e_type} -> not Type.primitive?(e_type)
_any -> false
end)
end

defp warn_missing_fields_error(module, field_defs) do
Expand Down
53 changes: 53 additions & 0 deletions test/pow/ecto/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,57 @@ defmodule Pow.Ecto.SchemaTest do
field :password, :string, [virtual: true]
"""
end

test "warns invalid fields defined" do
assert CaptureIO.capture_io(:stderr, fn ->
defmodule InvalidFieldUser do
use Ecto.Schema
use Pow.Ecto.Schema

schema "users" do
field :email, :utc_datetime
field :password_hash, :string
field :current_password, :string, virtual: true
field :password, :string, virtual: true

timestamps()
end
end
end) =~
"""
Please define the following field(s) in the schema for Pow.Ecto.SchemaTest.InvalidFieldUser:
field :email, :string, [null: false]
"""
end

test "doesn't warn for field with custom type" do
assert CaptureIO.capture_io(:stderr, fn ->
defmodule CustomType do
use Ecto.Type

def type, do: :binary

def cast(value), do: {:ok, value}

def load(value), do: {:ok, value}

def dump(value), do: {:ok, value}
end

defmodule CustomFieldTypeUser do
use Ecto.Schema
use Pow.Ecto.Schema

schema "users" do
field :email, CustomType
field :password_hash, :string
field :current_password, :string, virtual: true
field :password, :string, virtual: true

timestamps()
end
end
end) == ""
end
end

0 comments on commit 18954e6

Please sign in to comment.