Box.Ecto.Changeset (box v0.15.1)

View Source

Ecto changeset helper functions.

Summary

Functions

Formats a changeset error

Generates a slug from another field on the schema

Generates a unique value. Requires at least a generator and a length

Hashes a value using Argon2

Puts a default value change if no value is provided

Updates a schema's timestamp from a boolean field. The schema needs to have a virtual field to check if it should update the timestamp or not.

Trims fields values in changeset

Applies a given function a valid changeset

Types

format_error_option()

@type format_error_option() :: {:gettext, module()}

generate_slug_option()

@type generate_slug_option() ::
  {:source, atom()}
  | {:field, atom()}
  | {:exists?, (String.t() -> boolean())}
  | {:separator, String.t()}

touch_timestamp_option()

@type touch_timestamp_option() :: {:key, atom()} | {:setter, atom()}

Functions

format_error(error, options \\ [])

@spec format_error(
  {atom(), {String.t(), Keyword.t()}} | {String.t(), Keyword.t()},
  [format_error_option()]
) :: String.t()

Formats a changeset error

generate_slug(changeset, options)

@spec generate_slug(Ecto.Changeset.t(), [generate_slug_option()]) ::
  Ecto.Changeset.t()

Generates a slug from another field on the schema

generate_unique(changeset, field, options)

@spec generate_unique(Ecto.Changeset.t(), atom(), Keyword.t()) :: Ecto.Changeset.t()

Generates a unique value. Requires at least a generator and a length

Examples

iex> generate_unique(changeset, :code, generator: Box.Generator.Base64, length: 12)

The above generates a 12 char long base 64 encoded string as ̀:code

hash(changeset, field, options \\ [])

Hashes a value using Argon2

put_default_change(changeset, field, value_or_function)

@spec put_default_change(Ecto.Changeset.t(), atom(), (-> any()) | any()) ::
  Ecto.Changeset.t()

Puts a default value change if no value is provided

touch_timestamp(changeset, options)

@spec touch_timestamp(Ecto.Changeset.t(), [touch_timestamp_option()]) ::
  Ecto.Changeset.t()

Updates a schema's timestamp from a boolean field. The schema needs to have a virtual field to check if it should update the timestamp or not.

By default, the function infers the timestamp field's name from the virtual field's name.

Example

Calling touch_timestamp(changeset, setter: :inverted) will attempt to update the inverted_at timestamp. This can be overriden with thie :key option

trim(changeset, field_or_fields)

@spec trim(Ecto.Changeset.t(), [atom()] | atom()) :: Ecto.Changeset.t()

Trims fields values in changeset

update_valid(changeset, function)

Applies a given function a valid changeset