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

17 support multiple types #18

Closed solnic closed 1 year ago

solnic commented 1 year ago

This adds support for defining a value that can have multiple types. You can also specify any extra constraints that should be applied:

defmodule TestContract do
  use Drops.Contract

  schema do
    %{required(:test) => type([:integer, {:string, [:filled?]}])}
  end
end

TestContract.conform(%{test: 312})
# {:ok, %{test: 312}}

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

TestContract.conform(%{test: :invalid})
# {:error, [error: {:string?, [:test], :invalid}]}

TestContract.conform(%{test: :invalid})
# {:error, [error: {:filled?, [:test], ""}]}

Closes #17