litmus v1.0.1 Litmus.Type.DateTime View Source

This type validates DateTimes. It accepts either DateTime structs or ISO-8601 strings. ISO-8601 datetime with timezone strings will be converted into DateTimes.

Options

  • :default - Setting :default will populate a field with the provided value, assuming that it is not present already. If a field already has a value present, it will not be altered.

  • :required - Setting :required to true will cause a validation error when a field is not present or the value is nil. Allowed values for required are true and false. The default is false.

Examples

iex> schema = %{"start_date" => %Litmus.Type.DateTime{}}
iex> {:ok, %{"start_date" => datetime}} = Litmus.validate(%{"start_date" => "2017-06-18T05:45:33Z"}, schema)
iex> DateTime.to_iso8601(datetime)
"2017-06-18T05:45:33Z"

iex> {:ok, default_datetime, _} = DateTime.from_iso8601("2019-05-01T06:25:00-0700")
...> schema = %{
...>   "start_date" => %Litmus.Type.DateTime{
...>     default: default_datetime
...>   }
...> }
iex> {:ok, %{"start_date" => datetime}} = Litmus.validate(%{}, schema)
iex> DateTime.to_iso8601(datetime)
"2019-05-01T13:25:00Z"

Link to this section Summary

Link to this section Types

Specs

t() :: %Litmus.Type.DateTime{default: any(), required: boolean()}

Link to this section Functions

Link to this function

validate_field(type, field, data)

View Source

Specs

validate_field(t(), term(), map()) :: {:ok, map()} | {:error, String.t()}