extract v0.1.1 Parsers.DateParser

Parse input data for dates

Link to this section Summary

Link to this section Functions

Link to this function parse(struct, param_name, params, restrictions \\ []) (since 0.1.0)
parse(Map | Tuple, String | Atom, Map, Keywords | nil) ::
  {:ok, [], struct()} | {:error, [String], struct()}

Parse input data for date.

Returns a tuple with :ok or :error, a list of messages (used only if returns :error) and struct.

struct is previous struct. Can be map or tuple, anything else do nothing. param_name is key name. Can be atom or string. params is map with input data. Need to be a map. restrictions is keywords with restrictions of date. Can contains:

  • :default - will put input data with this value if input data is nil or empty string.
  • :max - the max date. Input data need to be equals or upper of this value.
  • :min - the min date. Input data need to be equals or lower of this value.
  • :required - if params map needs to have date value with valid date.

Examples

Just call:

iex> Parsers.DateParser.parse(%{}, :date, %{date: "2018-08-31"})
{:ok, [], %{date: ~D[2018-08-31]}}

# With default value
iex> Parsers.DateParser.parse(%{other: 123}, :date, %{date: ""}, default: "2018-08-31")
{:ok, [], %{date: ~D[2018-08-31], other: 123}}

# If input data is invalid
iex> Parsers.DateParser.parse(%{other: 123}, :date, %{date: "asdsa"})
{:error, ["invalid_date"], %{other: 123}}

# If input data is required and not provider
iex> Parsers.DateParser.parse(%{other: 123}, :date, %{}, required: true)
{:error, ["date_not_provided"], %{other: 123}}