Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coercions #2

Merged
merged 1 commit into from
Sep 8, 2023
Merged

Add coercions #2

merged 1 commit into from
Sep 8, 2023

Conversation

solnic
Copy link
Owner

@solnic solnic commented Sep 8, 2023

This adds a basic support for coercion functions. The DSL allows you to define from which type you want to coerce and what should be the resulting type. Additional constraints for the input type are not added at this point, but it's as good starting point.

defmodule TestContract do
  use Drops.Contract

  schema do
    %{
      required(:code) => from(:integer) |> type(:string),
      required(:price) => from(:string) |> type(:integer)
    }
  end
end

TestContract.conform(%{code: 12, price: "11"})
# {:ok, %{code: "12", price: 11}}

TestContract.conform(%{code: 12, price: 11})  
# {:error, [error: {:string?, :price, 11}]}

@solnic solnic changed the title Add-coercions Add coercions Sep 8, 2023
@solnic solnic merged commit 2e335a6 into main Sep 8, 2023
1 check passed
@solnic solnic deleted the add-coercions branch September 8, 2023 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant