View Source EctoAnon (ecto_anon v0.4.0)
EctoAnon
provides a simple way to handle data anonymization directly in your Ecto schemas.
Link to this section Summary
Functions
Updates an Ecto struct with anonymized data based on its anon_schema declaration. It also updates any embedded schema declared in anon_schema.
Link to this section Functions
@spec run(struct(), Ecto.Repo.t(), keyword()) :: {:ok, Ecto.Schema.t()} | {:error, :non_anonymizable_struct}
Updates an Ecto struct with anonymized data based on its anon_schema declaration. It also updates any embedded schema declared in anon_schema.
defmodule User do
use Ecto.Schema
use EctoAnon.Schema
anon_schema [
:firstname,
email: &__MODULE__.anonymized_email/3
birthdate: [:anonymized_date, options: [:only_year]]
]
schema "user" do
field :fistname, :string
field :email, :string
field :birthdate, :utc_datetime
end
def anonymized_email(_type, _value, _opts) do
"xxx@xxx.com"
end
end
It returns {:ok, struct}
if the struct has been successfully updated or {:error, :non_anonymizable_struct}
if the struct has no anonymizable fields.
options
Options
:cascade
- When set totrue
, allows ecto_anon to preload and anonymize all associations (and associations of these associations) automatically in cascade. Could be used to anonymize all data related a struct in a single call. Note that this won't traversebelongs_to
associations to avoid infinite and cyclic anonymizations. Default: false:log
- When set totrue
, it will setanonymized
field when EctoAnon.run applies anonymization on a ressource. Default: true
example
Example
defmodule User do
use Ecto.Schema
use EctoAnon.Schema
anon_schema [
:email
]
schema "users" do
field :name, :string
field :age, :integer, default: 0
field :email, :string
end
end
By default, an anon_field will be anonymized with a default value based on its type:
iex> user = Repo.get(User, id)
%User{name: "jane", age: 0, email: "jane@email.com"}
iex> EctoAnon.run(user, Repo)
{:ok, %User{name: "jane", age: 0, email: "redacted"}}