Skip to content

Option to use different values per type #25

@prodis

Description

@prodis

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions