solnic / drops

🛠️ Tools for working with data effectively - data contracts using types, schemas, domain validation rules, type-safe casting, and more.
Other
257 stars 6 forks source link

Add integer => date_time caster #22

Closed solnic closed 1 year ago

solnic commented 1 year ago

Using the default :second unit:

defmodule TestContract do
  use Drops.Contract

  schema do
    %{required(:test) => from(:integer) |> type(:date_time)}
  end
end

TestContract.conform(%{test: 1695277470})
# {:ok, %{test: ~U[2023-09-21 06:24:30Z]}}

Providing time unit as an argument for the casting function:

defmodule TestContract do
  use Drops.Contract

  schema do
    %{required(:test) => from(:integer, [:millisecond]) |> type(:date_time)}
  end
end

TestContract.conform(%{test: 1695277723355})
# {:ok, %{test: ~U[2023-09-21 06:28:43.355Z]}}

Closes #10