View Source BitcrowdEcto.Changeset (bitcrowd_ecto v0.14.0)

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 a t:Money.t value according to the given options.

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
@type validate_email_option() ::
  {:max_length, non_neg_integer()} | {:only_web, boolean()}
Link to this type

validate_money_option()

View Source
@type validate_money_option() ::
  {:equal_to, Money.t()}
  | {:more_than, Money.t()}
  | {:less_than, Money.t()}
  | {:more_than_or_equal_to, Money.t()}
  | {:less_than_or_equal_to, Money.t()}
  | {:currency, atom()}

Link to this section Functions

Link to this function

validate_changed(changeset, field)

View Source (since 0.1.0)
@spec 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)
@spec 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)
@spec 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

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)
@spec 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)
@spec 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

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)
@spec validate_email(Ecto.Changeset.t(), atom(), [validate_email_option()]) ::
  Ecto.Changeset.t()

Validates that an email has valid format.

  • Ignores nil values.

compliance

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

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)
@spec 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)
@spec 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)
@spec 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_money(changeset, field, opts)

View Source (since 0.13.0)
@spec validate_money(Ecto.Changeset.t(), atom(), [validate_money_option()]) ::
  Ecto.Changeset.t() | no_return()

Validates a t:Money.t value according to the given options.

examples

Examples

validate_money(changeset, :amount, less_than: Money.new("100.00", :USD))
validate_money(changeset, :amount, greater_than_or_equal_to: Money.new("100.00", :USD))
validate_money(changeset, :amount, currency: :USD)
Link to this function

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

View Source (since 0.6.0)
@spec 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

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)
@spec 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)
@spec validate_transition(Ecto.Changeset.t(), atom(), [{any(), any()}]) ::
  Ecto.Changeset.t()

Validates that a field has changed in a defined way.

examples

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)
@spec validate_url(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t()

Validates a field url to be qualified url