ecto_anon
ecto_anon copied to clipboard
Option to use different values per type
Currently it is only possible to anonymise a field with a value different from the default value providing a custom function for each field.
For example:
defmodule User do
use Ecto.Schema
use EctoAnon.Schema
anon_schema [
name: &__MODULE__.custom_string/3
email: &__MODULE__.custom_string/3
]
schema "users" do
field :name, :string
field :age, :integer
field :email, :string
anonymized()
end
def custom_string(:string, %__MODULE__{} = _user, _opts \\ []), do: "[REDACTED]"
end
user = Repo.get(User, id)
%User{name: "jane", age: 24, email: "[email protected]"}
EctoAnon.run(user, Repo)
{:ok, %User{name: "[REDACTED]", age: 24, email: "[REDACTED]"}}
My suggestion is to have an alternative to provider a different value per type.
For example:
defmodule User do
use Ecto.Schema
use EctoAnon.Schema
anon_schema(
[:name, :email],
anonymized_values: [string: "[REDACTED]"]
)
schema "users" do
field :name, :string
field :age, :integer
field :email, :string
anonymized()
end
end
user = Repo.get(User, id)
%User{name: "jane", age: 24, email: "[email protected]"}
EctoAnon.run(user, Repo)
{:ok, %User{name: "[REDACTED]", age: 24, email: "[REDACTED]"}}
I am not thinking about the implementation details now, only the public interface and its usage for the sake of the discussion of the new feature. 🙂
What do you think?
Hello @prodis ! Thank you for your suggestion ! Indeed that could be useful ! But maybe I would see that more in a config file to override default type values instead of putting it in a each schemas
What do you think ? :)
That is an interesting idea. Shall I open a PR with an implementation proposal?
Sure with pleasure ! 🔥