From 6ab2425897f870cefc96030bc14ebd7de8c633df Mon Sep 17 00:00:00 2001 From: Dan Schultzer Date: Sun, 5 Jul 2020 20:15:15 -0700 Subject: [PATCH] Only warn when using Ecto primitives --- CHANGELOG.md | 1 + lib/pow/ecto/schema.ex | 11 ++++++-- test/pow/ecto/schema_test.exs | 53 +++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 797b73ce..d7fa027d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/pow/ecto/schema.ex b/lib/pow/ecto/schema.ex index c7a8b9d6..fab65aea 100644 --- a/lib/pow/ecto/schema.ex +++ b/lib/pow/ecto/schema.ex @@ -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 @@ -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 @@ -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 diff --git a/test/pow/ecto/schema_test.exs b/test/pow/ecto/schema_test.exs index 3007c4fe..e976761c 100644 --- a/test/pow/ecto/schema_test.exs +++ b/test/pow/ecto/schema_test.exs @@ -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