solnic / drops

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

Support custom casters #21

Closed solnic closed 1 year ago

solnic commented 1 year ago
defmodule TestContract do
  use Drops.Contract

  defmodule CustomCaster do
    def cast(:string, :string, value) do
      String.downcase(value)
    end
  end

  schema do
    %{required(:test) => from(:string, caster: CustomCaster) |> type(:string)}
  end
end

TestContract.conform(%{test: "Hello World"})
# {:ok, %{test: "hello world"}}