ecto_anon icon indicating copy to clipboard operation
ecto_anon copied to clipboard

Option to use different values per type

Open prodis opened this issue 2 years ago • 3 comments

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?

prodis avatar Oct 07 '22 05:10 prodis

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 ? :)

quaresc avatar Oct 19 '22 15:10 quaresc

That is an interesting idea. Shall I open a PR with an implementation proposal?

prodis avatar Oct 19 '22 17:10 prodis

Sure with pleasure ! 🔥

quaresc avatar Oct 19 '22 17:10 quaresc