View Source EctoCommons.DateTimeValidator (Ecto Commons v0.3.6)
This module provides validators for DateTime
s.
You can use the following checks:
:is
to check if aDateTime
is exactly someDateTime
. You can also provide a:delta
option (in seconds) to specify a delta around which theDateTime
is still considered identical.:before
to check if aDateTime
is before someDateTime
:after
to check if aDateTime
is after someDateTime
You can also combine the given checks for complex checks. Errors won't be stacked though, the first error encountered will be returned and subsequent checks will be skipped. If you want to check everything at once, you'll need to call this validator multiple times.
Also, instead of providing a DateTime
, you can also provide some special atoms:
:utc_now
will compare the givenDateTime
with theDateTime
at runtime, by callingDateTime.utc_now()
.
Example:
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[2016-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate)
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[2016-05-24 13:26:08Z]}, errors: [], data: %{}, valid?: true>
# Using :is to ensure a date is identical to another date
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[2016-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, is: ~U[2016-05-24 13:26:08Z])
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[2016-05-24 13:26:08Z]}, errors: [], data: %{}, valid?: true>
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[2016-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, is: ~U[2017-05-24 13:26:08Z])
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[2016-05-24 13:26:08Z]}, errors: [birthdate: {"should be %{is}.", [validation: :datetime, kind: :is]}], data: %{}, valid?: false>
# Using :is with :delta to ensure a date is near another another date
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[2016-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, is: ~U[2016-05-24 13:46:08Z], delta: 3600)
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[2016-05-24 13:26:08Z]}, errors: [], data: %{}, valid?: true>
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[2016-05-24 15:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, is: ~U[2016-05-24 13:26:08Z], delta: 3600)
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[2016-05-24 15:26:08Z]}, errors: [birthdate: {"should be %{is}.", [validation: :datetime, kind: :is]}], data: %{}, valid?: false>
# Using :before to ensure date is before given date
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[2016-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, before: ~U[2017-05-24 00:00:00Z])
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[2016-05-24 13:26:08Z]}, errors: [], data: %{}, valid?: true>
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[2016-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, before: ~U[2015-05-24 00:00:00Z])
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[2016-05-24 13:26:08Z]}, errors: [birthdate: {"should be before %{before}.", [validation: :datetime, kind: :before]}], data: %{}, valid?: false>
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[2016-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, before: :utc_now)
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[2016-05-24 13:26:08Z]}, errors: [], data: %{}, valid?: true>
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[3000-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, before: :utc_now)
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[3000-05-24 13:26:08Z]}, errors: [birthdate: {"should be before %{before}.", [validation: :datetime, kind: :before]}], data: %{}, valid?: false>
# Using :after to ensure date is after given date
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[2016-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, after: ~U[2015-05-24 00:00:00Z])
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[2016-05-24 13:26:08Z]}, errors: [], data: %{}, valid?: true>
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[2016-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, after: ~U[2017-05-24 00:00:00Z])
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[2016-05-24 13:26:08Z]}, errors: [birthdate: {"should be after %{after}.", [validation: :datetime, kind: :after]}], data: %{}, valid?: false>
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[3000-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, after: :utc_now)
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[3000-05-24 13:26:08Z]}, errors: [], data: %{}, valid?: true>
iex> types = %{birthdate: :utc_datetime}
iex> params = %{birthdate: ~U[1000-05-24 13:26:08Z]}
iex> Ecto.Changeset.cast({%{}, types}, params, Map.keys(types))
...> |> validate_datetime(:birthdate, after: :utc_now)
#Ecto.Changeset<action: nil, changes: %{birthdate: ~U[1000-05-24 13:26:08Z]}, errors: [birthdate: {"should be after %{after}.", [validation: :datetime, kind: :after]}], data: %{}, valid?: false>