View Source Dsv.Date (Dsv v0.2.1)
Dsv.Date module provides functions to validate a date value based on the specified rules (:min, :max, :range).
Possible options are:
:min
- check if the date is later or the same as the date specified in the:min
option.:max
- check if the date is earlier or the same as the date specified in the:max
option.:range
- check if the date is between the dates specified in the:range
options.
Summary
Functions
The Dsv.Date.valid?/2
function is designed to validate a given date against a set of options provided by the user.
The Dsv.Date.validate/2
function is designed to validate a given date against a set of options provided by the user.
Functions
The Dsv.Date.valid?/2
function is designed to validate a given date against a set of options provided by the user.
It checks whether the date falls within the specified range or exceeds certain boundaries defined by the min and max dates.
If the date meets all of the specified conditions, the function returns an :true
response; otherwise, it returns :false
.
Parameters
date
- The date value to validate.rules
- A keyword list of validation rules. Each rule is a tuple containing:- The validation type (
:min
,:max
,:range
). - The corresponding value or values for the validation type.
- The validation type (
Return
:true
- If the date is later than the min date provided in options, earlier than the max date provided in options, or falls between the dates specified in the range.:false
- If the date does not meet any of the specified conditions.
min and max options
:min
and:max
are inclusive
@spec validate( Date | DateTime | Time | NaiveDateTime | String.t(), keyword() ) :: :ok | {:error, String.t()}
The Dsv.Date.validate/2
function is designed to validate a given date against a set of options provided by the user.
It checks whether the date falls within the specified range or exceeds certain boundaries defined by the min and max dates.
If the date meets all of the specified conditions, the function returns an :ok
response; otherwise, it returns an appropriate error message.
Parameters
date
- The date value to validate.rules
- A keyword list of validation rules. Each rule is a tuple containing:- The validation type (
:min
,:max
,:range
). - The corresponding value or values for the validation type.
- The validation type (
Return
:ok
- If the date is later than the min date provided in options, earlier than the max date provided in options, or falls between the dates specified in the range.{:error, message}
- If the date does not meet any of the specified conditions, where message is an appropriate string message.
min and max options
:min
and:max
are inclusive
Examples for different types:
Examples - date
min
:min
example with first parameter as a string:
iex> Dsv.Date.validate("2020-10-10", min: ~D[2020-10-09])
:ok
iex> Dsv.Date.validate("2020-10-10", min: ~D[2020-10-10])
:ok
iex> Dsv.Date.validate("2020-10-10", min: ~D[2020-10-11])
{:error, "Date is earlier than the minimum allowed date 2020-10-11"}
:min
example with first parameter as a date:
iex> Dsv.Date.validate(~D[2020-10-10], min: ~D[2020-10-09])
:ok
iex> Dsv.Date.validate(~D[2020-10-10], min: ~D[2020-10-10])
:ok
iex> Dsv.Date.validate(~D[2020-10-10], min: ~D[2020-10-11])
{:error, "Date is earlier than the minimum allowed date 2020-10-11"}
max
:max
example with first parameter as a string:
iex> Dsv.Date.validate("2020-10-10", max: ~D[2020-10-11])
:ok
iex> Dsv.Date.validate("2020-10-10", max: ~D[2020-10-10])
:ok
iex> Dsv.Date.validate("2020-10-10", max: ~D[2020-10-09])
{:error, "Date is later than the maximum allowed date 2020-10-09"}
:max
example with first parameter as a date:
iex> Dsv.Date.validate(~D[2020-10-10], max: ~D[2020-10-11])
:ok
iex> Dsv.Date.validate(~D[2020-10-10], max: ~D[2020-10-10])
:ok
iex> Dsv.Date.validate(~D[2020-10-10], max: ~D[2020-10-09])
{:error, "Date is later than the maximum allowed date 2020-10-09"}
min & max
:min & :max
example:
iex> Dsv.Date.validate("2020-10-10", min: ~D[2020-10-09], max: ~D[2020-10-10])
:ok
iex> Dsv.Date.validate("2020-10-09", min: ~D[2020-10-09], max: ~D[2020-10-10])
:ok
iex> Dsv.Date.validate("2020-10-10", min: ~D[2020-10-09], max: ~D[2020-10-11])
:ok
iex> Dsv.Date.validate("2020-10-07", min: ~D[2020-10-08], max: ~D[2020-10-09])
{:error, "Date must be between 2020-10-08 and 2020-10-09"}
iex> Dsv.Date.validate("2020-10-10", min: ~D[2020-10-08], max: ~D[2020-10-09])
{:error, "Date must be between 2020-10-08 and 2020-10-09"}
iex> Dsv.Date.validate("2020-10-10", min: ~D[2020-10-10], max: ~D[2020-10-09])
** (RuntimeError) Min date (2020-10-10) can't be after max date (2020-10-09)
iex> Dsv.Date.validate(~D[2020-10-10], min: ~D[2020-10-09], max: ~D[2020-10-10])
:ok
iex> Dsv.Date.validate(~D[2020-10-09], min: ~D[2020-10-09], max: ~D[2020-10-10])
:ok
iex> Dsv.Date.validate(~D[2020-10-10], min: ~D[2020-10-09], max: ~D[2020-10-11])
:ok
iex> Dsv.Date.validate(~D[2020-10-07], min: ~D[2020-10-08], max: ~D[2020-10-09])
{:error, "Date must be between 2020-10-08 and 2020-10-09"}
iex> Dsv.Date.validate(~D[2020-10-10], min: ~D[2020-10-08], max: ~D[2020-10-09])
{:error, "Date must be between 2020-10-08 and 2020-10-09"}
iex> Dsv.Date.validate(~D[2020-10-10], min: ~D[2020-10-10], max: ~D[2020-10-09])
** (RuntimeError) Min date (2020-10-10) can't be after max date (2020-10-09)
range
:range
example:
iex> Dsv.Date.validate("2020-10-10", range: {~D[2020-10-09], ~D[2020-10-10]})
:ok
iex> Dsv.Date.validate("2020-10-09", range: {~D[2020-10-09], ~D[2020-10-10]})
:ok
iex> Dsv.Date.validate("2020-10-10", range: {~D[2020-10-09], ~D[2020-10-11]})
:ok
iex> Dsv.Date.validate("2020-10-07", range: {~D[2020-10-08], ~D[2020-10-09]})
{:error, "Date must be between 2020-10-08 and 2020-10-09"}
iex> Dsv.Date.validate("2020-10-10", range: {~D[2020-10-08], ~D[2020-10-09]})
{:error, "Date must be between 2020-10-08 and 2020-10-09"}
iex> Dsv.Date.validate("2020-10-10", range: {~D[2020-10-10], ~D[2020-10-09]})
** (RuntimeError) Min date (2020-10-10) can't be after max date (2020-10-09)
iex> Dsv.Date.validate(~D[2020-10-10], range: {~D[2020-10-09], ~D[2020-10-10]})
:ok
iex> Dsv.Date.validate(~D[2020-10-09], range: {~D[2020-10-09], ~D[2020-10-10]})
:ok
iex> Dsv.Date.validate(~D[2020-10-10], range: {~D[2020-10-09], ~D[2020-10-11]})
:ok
iex> Dsv.Date.validate(~D[2020-10-07], range: {~D[2020-10-08], ~D[2020-10-09]})
{:error, "Date must be between 2020-10-08 and 2020-10-09"}
iex> Dsv.Date.validate(~D[2020-10-10], range: {~D[2020-10-08], ~D[2020-10-09]})
{:error, "Date must be between 2020-10-08 and 2020-10-09"}
iex> Dsv.Date.validate(~D[2020-10-10], range: {~D[2020-10-10], ~D[2020-10-09]})
** (RuntimeError) Min date (2020-10-10) can't be after max date (2020-10-09)
Examples DateTime
min
:min
example:
iex> Dsv.Date.validate("2020-10-10T23:59:59Z", min: ~U[2020-10-10 23:59:58Z])
:ok
iex> Dsv.Date.validate("2020-10-10T23:59:59Z", min: ~U[2020-10-10 23:59:59Z])
:ok
iex> Dsv.Date.validate("2020-10-10T23:59:59Z", min: ~U[2020-10-11 00:00:00Z])
{:error, "Date is earlier than the minimum allowed date 2020-10-11 00:00:00Z"}
iex> Dsv.Date.validate(~U[2020-10-10T23:59:59Z], min: ~U[2020-10-10 23:59:58Z])
:ok
iex> Dsv.Date.validate(~U[2020-10-10T23:59:59Z], min: ~U[2020-10-10 23:59:59Z])
:ok
iex> Dsv.Date.validate(~U[2020-10-10T23:59:59Z], min: ~U[2020-10-11 00:00:00Z])
{:error, "Date is earlier than the minimum allowed date 2020-10-11 00:00:00Z"}
max
:max
example:
iex> Dsv.Date.validate("2020-10-10T23:59:59Z", max: ~U[2020-10-11 00:00:00Z])
:ok
iex> Dsv.Date.validate("2020-10-10T23:59:59Z", max: ~U[2020-10-10 23:59:59Z])
:ok
iex> Dsv.Date.validate("2020-10-10T23:59:59Z", max: ~U[2020-10-10 23:59:58Z])
{:error, "Date is later than the maximum allowed date 2020-10-10 23:59:58Z"}
iex> Dsv.Date.validate(~U[2020-10-10T23:59:59Z], max: ~U[2020-10-11 00:00:00Z])
:ok
iex> Dsv.Date.validate(~U[2020-10-10T23:59:59Z], max: ~U[2020-10-10 23:59:59Z])
:ok
iex> Dsv.Date.validate(~U[2020-10-10T23:59:59Z], max: ~U[2020-10-10 23:59:58Z])
{:error, "Date is later than the maximum allowed date 2020-10-10 23:59:58Z"}
min & max
:min & :max
example:
iex> Dsv.Date.validate("2020-10-10T23:59:58Z", min: ~U[2020-10-10 23:59:58Z], max: ~U[2020-10-10 23:59:59Z])
:ok
iex> Dsv.Date.validate("2020-10-10T23:59:59Z", min: ~U[2020-10-10 23:59:58Z], max: ~U[2020-10-10 23:59:59Z])
:ok
iex> Dsv.Date.validate("2020-10-10T23:59:58Z", min: ~U[2020-10-10 23:59:57Z], max: ~U[2020-10-10 23:59:59Z])
:ok
iex> Dsv.Date.validate("2020-10-10T23:59:57Z", min: ~U[2020-10-10 23:59:58Z], max: ~U[2020-10-10 23:59:59Z])
{:error, "Date must be between 2020-10-10 23:59:58Z and 2020-10-10 23:59:59Z"}
iex> Dsv.Date.validate("2020-10-10T23:59:59Z", min: ~U[2020-10-10 23:59:57Z], max: ~U[2020-10-10 23:59:58Z])
{:error, "Date must be between 2020-10-10 23:59:57Z and 2020-10-10 23:59:58Z"}
iex> Dsv.Date.validate("2020-10-10T23:59:58Z", min: ~U[2020-10-10 23:59:59Z], max: ~U[2020-10-10 23:59:58Z])
** (RuntimeError) Min date (2020-10-10 23:59:59Z) can't be after max date (2020-10-10 23:59:58Z)
iex> Dsv.Date.validate(~U[2020-10-10 23:59:58Z], min: ~U[2020-10-10 23:59:58Z], max: ~U[2020-10-10 23:59:59Z])
:ok
iex> Dsv.Date.validate(~U[2020-10-10 23:59:59Z], min: ~U[2020-10-10 23:59:58Z], max: ~U[2020-10-10 23:59:59Z])
:ok
iex> Dsv.Date.validate(~U[2020-10-10 23:59:58Z], min: ~U[2020-10-10 23:59:57Z], max: ~U[2020-10-10 23:59:59Z])
:ok
iex> Dsv.Date.validate(~U[2020-10-10 23:59:57Z], min: ~U[2020-10-10 23:59:58Z], max: ~U[2020-10-10 23:59:59Z])
{:error, "Date must be between 2020-10-10 23:59:58Z and 2020-10-10 23:59:59Z"}
iex> Dsv.Date.validate(~U[2020-10-10 23:59:59Z], min: ~U[2020-10-10 23:59:57Z], max: ~U[2020-10-10 23:59:58Z])
{:error, "Date must be between 2020-10-10 23:59:57Z and 2020-10-10 23:59:58Z"}
iex> Dsv.Date.validate(~U[2020-10-10 23:59:58Z], min: ~U[2020-10-10 23:59:59Z], max: ~U[2020-10-10 23:59:58Z])
** (RuntimeError) Min date (2020-10-10 23:59:59Z) can't be after max date (2020-10-10 23:59:58Z)
range
:range
example:
iex> Dsv.Date.validate("2020-10-10T23:59:58Z", range: {~U[2020-10-10 23:59:58Z], ~U[2020-10-10 23:59:59Z]})
:ok
iex> Dsv.Date.validate("2020-10-10T23:59:59Z", range: {~U[2020-10-10 23:59:58Z], ~U[2020-10-10 23:59:59Z]})
:ok
iex> Dsv.Date.validate("2020-10-10T23:59:58Z", range: {~U[2020-10-10 23:59:57Z], ~U[2020-10-10 23:59:59Z]})
:ok
iex> Dsv.Date.validate("2020-10-10T23:59:57Z", range: {~U[2020-10-10 23:59:58Z], ~U[2020-10-10 23:59:59Z]})
{:error, "Date must be between 2020-10-10 23:59:58Z and 2020-10-10 23:59:59Z"}
iex> Dsv.Date.validate("2020-10-10T23:59:59Z", range: {~U[2020-10-10 23:59:57Z], ~U[2020-10-10 23:59:58Z]})
{:error, "Date must be between 2020-10-10 23:59:57Z and 2020-10-10 23:59:58Z"}
iex> Dsv.Date.validate("2020-10-10T23:59:58Z", range: {~U[2020-10-10 23:59:59Z], ~U[2020-10-10 23:59:58Z]})
** (RuntimeError) Min date (2020-10-10 23:59:59Z) can't be after max date (2020-10-10 23:59:58Z)
iex> Dsv.Date.validate(~U[2020-10-10 23:59:58Z], range: {~U[2020-10-10 23:59:58Z], ~U[2020-10-10 23:59:59Z]})
:ok
iex> Dsv.Date.validate(~U[2020-10-10 23:59:59Z], range: {~U[2020-10-10 23:59:58Z], ~U[2020-10-10 23:59:59Z]})
:ok
iex> Dsv.Date.validate(~U[2020-10-10 23:59:58Z], range: {~U[2020-10-10 23:59:57Z], ~U[2020-10-10 23:59:59Z]})
:ok
iex> Dsv.Date.validate(~U[2020-10-10 23:59:57Z], range: {~U[2020-10-10 23:59:58Z], ~U[2020-10-10 23:59:59Z]})
{:error, "Date must be between 2020-10-10 23:59:58Z and 2020-10-10 23:59:59Z"}
iex> Dsv.Date.validate(~U[2020-10-10 23:59:59Z], range: {~U[2020-10-10 23:59:57Z], ~U[2020-10-10 23:59:58Z]})
{:error, "Date must be between 2020-10-10 23:59:57Z and 2020-10-10 23:59:58Z"}
iex> Dsv.Date.validate(~U[2020-10-10 23:59:58Z], [range: {~U[2020-10-10 23:59:59Z], ~U[2020-10-10 23:59:58Z]}])
** (RuntimeError) Min date (2020-10-10 23:59:59Z) can't be after max date (2020-10-10 23:59:58Z)
Examples Time
min
:min
example:
iex> Dsv.Date.validate("23:59:59Z", min: ~T[23:59:58Z])
:ok
iex> Dsv.Date.validate("23:59:59Z", min: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate("23:59:58Z", min: ~T[23:59:59Z])
{:error, "Date is earlier than the minimum allowed date 23:59:59"}
iex> Dsv.Date.validate(~T[23:59:59Z], min: ~T[23:59:58Z])
:ok
iex> Dsv.Date.validate(~T[23:59:59Z], min: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate(~T[23:59:58Z], min: ~T[23:59:59Z])
{:error, "Date is earlier than the minimum allowed date 23:59:59"}
max
:max
example:
iex> Dsv.Date.validate("23:59:58Z", max: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate("23:59:59Z", max: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate("23:59:59Z", max: ~T[23:59:58Z])
{:error, "Date is later than the maximum allowed date 23:59:58"}
iex> Dsv.Date.validate(~T[23:59:58Z], max: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate(~T[23:59:59Z], max: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate(~T[23:59:59Z], max: ~T[23:59:58Z])
{:error, "Date is later than the maximum allowed date 23:59:58"}
min & max
:min & :max
example:
iex> Dsv.Date.validate("23:59:58Z", min: ~T[23:59:58Z], max: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate("23:59:59Z", min: ~T[23:59:58Z], max: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate("23:59:58Z", min: ~T[23:59:57Z], max: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate("23:59:57Z", min: ~T[23:59:58Z], max: ~T[23:59:59Z])
{:error, "Date must be between 23:59:58 and 23:59:59"}
iex> Dsv.Date.validate("23:59:59Z", min: ~T[23:59:57Z], max: ~T[23:59:58Z])
{:error, "Date must be between 23:59:57 and 23:59:58"}
iex> Dsv.Date.validate("23:59:58Z", min: ~T[23:59:59Z], max: ~T[23:59:58Z])
** (RuntimeError) Min date (23:59:59) can't be after max date (23:59:58)
iex> Dsv.Date.validate(~T[23:59:58Z], min: ~T[23:59:58Z], max: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate(~T[23:59:59Z], min: ~T[23:59:58Z], max: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate(~T[23:59:58Z], min: ~T[23:59:57Z], max: ~T[23:59:59Z])
:ok
iex> Dsv.Date.validate(~T[23:59:57Z], min: ~T[23:59:58Z], max: ~T[23:59:59Z])
{:error, "Date must be between 23:59:58 and 23:59:59"}
iex> Dsv.Date.validate(~T[23:59:59Z], min: ~T[23:59:57Z], max: ~T[23:59:58Z])
{:error, "Date must be between 23:59:57 and 23:59:58"}
iex> Dsv.Date.validate(~T[23:59:58Z], min: ~T[23:59:59Z], max: ~T[23:59:58Z])
** (RuntimeError) Min date (23:59:59) can't be after max date (23:59:58)
range
:range
example:
iex> Dsv.Date.validate("23:59:58Z", range: {~T[23:59:58Z], ~T[23:59:59Z]})
:ok
iex> Dsv.Date.validate("23:59:59Z", range: {~T[23:59:58Z], ~T[23:59:59Z]})
:ok
iex> Dsv.Date.validate("23:59:58Z", range: {~T[23:59:57Z], ~T[23:59:59Z]})
:ok
iex> Dsv.Date.validate("23:59:57Z", range: {~T[23:59:58Z], ~T[23:59:59Z]})
{:error, "Date must be between 23:59:58 and 23:59:59"}
iex> Dsv.Date.validate("23:59:59Z", range: {~T[23:59:57Z], ~T[23:59:58Z]})
{:error, "Date must be between 23:59:57 and 23:59:58"}
iex> Dsv.Date.validate("23:59:58Z", range: {~T[23:59:59Z], ~T[23:59:58Z]})
** (RuntimeError) Min date (23:59:59) can't be after max date (23:59:58)
iex> Dsv.Date.validate(~T[23:59:58Z], range: {~T[23:59:58Z], ~T[23:59:59Z]})
:ok
iex> Dsv.Date.validate(~T[23:59:59Z], range: {~T[23:59:58Z], ~T[23:59:59Z]})
:ok
iex> Dsv.Date.validate(~T[23:59:58Z], range: {~T[23:59:57Z], ~T[23:59:59Z]})
:ok
iex> Dsv.Date.validate(~T[23:59:57Z], range: {~T[23:59:58Z], ~T[23:59:59Z]})
{:error, "Date must be between 23:59:58 and 23:59:59"}
iex> Dsv.Date.validate(~T[23:59:59Z], range: {~T[23:59:57Z], ~T[23:59:58Z]})
{:error, "Date must be between 23:59:57 and 23:59:58"}
iex> Dsv.Date.validate(~T[23:59:58Z], range: {~T[23:59:59Z], ~T[23:59:58Z]})
** (RuntimeError) Min date (23:59:59) can't be after max date (23:59:58)