BitcrowdEcto.Changeset (bitcrowd_ecto v0.12.0) View Source

Extensions for Ecto changesets.

Link to this section Summary

Functions

Validates that a field that has been changed.

Validates a date field in the changeset is after the given reference date.

Validates two date fields to be a date range, so if both are set the first field has to be before the second field. The error is placed on the later field.

Validates a field timestamp to be after the given one

Validates two datetime fields to be a time range, so if both are set the first has to be before the second field. The error is placed on the later field.

Validates that an email has valid format.

Validates a field timestamp to be in the future, if present

Validates a changeset field with hexadecimal color format

Validates that a field is not changed from its current value, unless the current value is nil.

Validates two fields to be a range, so if both are set the first has to be before the second field. The error is placed on the second field.

Validates a field timestamp to be in the past, if present

Validates that a field has changed in a defined way.

Validates a field url to be qualified url

Link to this section Types

Link to this type

validate_email_option()

View Source

Specs

validate_email_option() ::
  {:max_length, non_neg_integer()} | {:only_web, boolean()}

Link to this section Functions

Link to this function

validate_changed(changeset, field)

View Source (since 0.1.0)

Specs

validate_changed(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t()

Validates that a field that has been changed.

Link to this function

validate_date_after(changeset, date_field, ref_date, opts \\ [])

View Source (since 0.11.0)

Specs

validate_date_after(Ecto.Changeset.t(), atom(), Date.t(), [
  {:formatter, (... -> any())}
]) :: Ecto.Changeset.t()

Validates a date field in the changeset is after the given reference date.

Link to this function

validate_date_order(changeset, from_field, until_field, opts \\ [])

View Source (since 0.6.0)

Specs

validate_date_order(Ecto.Changeset.t(), atom(), atom(),
  formatter: (... -> any()),
  valid_orders: [atom()]
) :: Ecto.Changeset.t()

Validates two date fields to be a date range, so if both are set the first field has to be before the second field. The error is placed on the later field.

Examples

validate_date_order(changeset, :from, :to)
validate_date_order(changeset, :from, :to, [valid_orders: :lt])
validate_date_order(changeset, :from, :to, [formatter: &Date.day_of_week/1])
Link to this function

validate_datetime_after(changeset, field, reference_datetime, opts \\ [])

View Source (since 0.6.0)

Specs

validate_datetime_after(Ecto.Changeset.t(), atom(), DateTime.t(), [
  {:formatter, (... -> any())}
]) :: Ecto.Changeset.t()

Validates a field timestamp to be after the given one

Link to this function

validate_datetime_order(changeset, from_field, until_field, opts \\ [])

View Source (since 0.6.0)

Specs

validate_datetime_order(Ecto.Changeset.t(), atom(), atom(),
  formatter: (... -> any()),
  valid_orders: [atom()]
) :: Ecto.Changeset.t()

Validates two datetime fields to be a time range, so if both are set the first has to be before the second field. The error is placed on the later field.

Examples

validate_datetime_order(changeset, :from, :to)
validate_datetime_order(changeset, :from, :to, [valid_orders: :lt])
validate_datetime_order(changeset, :from, :to, [formatter: &DateTime.to_time/1])
Link to this function

validate_email(changeset, field, opts \\ [])

View Source (since 0.1.0)

Specs

Validates that an email has valid format.

  • Ignores nil values.

Compliance

For a good list of valid/invalid emails, see https://gist.github.com/cjaoude/fd9910626629b53c4d25

The regex used in this validator doesn't understand half of the inputs, but we don't really care for now. Validating super strange emails is not a sport we want to compete in.

Options

  • :max_length - restricts the maximum length of the input, defaults to 320
  • :only_web - requires a dot in the domain part, e.g. domain.tld, defaults to true
Link to this function

validate_future_datetime(changeset, field, now \\ DateTime.utc_now())

View Source (since 0.6.0)

Specs

validate_future_datetime(Ecto.Changeset.t(), atom(), DateTime.t()) ::
  Ecto.Changeset.t()

Validates a field timestamp to be in the future, if present

Link to this function

validate_hex_color(changeset, hex_color_field)

View Source (since 0.11.0)

Specs

validate_hex_color(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t()

Validates a changeset field with hexadecimal color format

Link to this function

validate_immutable(changeset, field)

View Source (since 0.1.0)

Specs

validate_immutable(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t()

Validates that a field is not changed from its current value, unless the current value is nil.

Link to this function

validate_order(changeset, from_field, until_field, validation_key, opts \\ [])

View Source (since 0.6.0)

Specs

validate_order(Ecto.Changeset.t(), atom(), atom(), atom(),
  formatter: (... -> any()),
  compare_fun: (... -> any()),
  valid_orders: [atom()]
) :: Ecto.Changeset.t()

Validates two fields to be a range, so if both are set the first has to be before the second field. The error is placed on the second field.

Examples

validate_order(changeset, :from, :to, :to_is_after_from)
validate_order(changeset, :from, :to, :to_is_after_from, [compare_fun: fn a, b -> String.length(a) > String.length(b) end])
validate_order(changeset, :from, :to, :to_is_after_from, [formatter: &String.length/1])
Link to this function

validate_past_datetime(changeset, field, now \\ DateTime.utc_now())

View Source (since 0.6.0)

Specs

validate_past_datetime(Ecto.Changeset.t(), atom(), DateTime.t()) ::
  Ecto.Changeset.t()

Validates a field timestamp to be in the past, if present

Link to this function

validate_transition(changeset, field, transitions)

View Source (since 0.1.0)

Specs

validate_transition(Ecto.Changeset.t(), atom(), [{any(), any()}]) ::
  Ecto.Changeset.t()

Validates that a field has changed in a defined way.

Examples

validate_transition(changeset, field, [{"foo", "bar"}, {"foo", "yolo"}])

This marks the changeset invalid unless the value of :field is currently "foo" and is changed to "bar" or "yolo". If the field is not changed, a {state, state} transition has to be present in the list of transitions.

Link to this function

validate_url(changeset, field)

View Source (since 0.1.0)

Specs

validate_url(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t()

Validates a field url to be qualified url