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 rule macro #3

Merged
merged 1 commit into from
Sep 9, 2023
Merged

Add rule macro #3

merged 1 commit into from
Sep 9, 2023

Conversation

solnic
Copy link
Owner

@solnic solnic commented Sep 9, 2023

This adds support for defining arbitrary rule functions that match against input that was already processed and validated by the contract's schema. This allows you to define additional validation logic that can be implemented in a type-safe fashion.

defmodule TestContract do
  use Drops.Contract

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

  rule(:unique?, [{:ok, {:name, value}}]) do
    case value do
      "John" -> {:error, {:taken, :name, value}}
      _ -> :ok
    end
  end
end

TestContract.conform(%{name: "Jane"})
# {:ok, %{name: "Jane"}}

TestContract.conform(%{name: "John"})
# {:error, [error: {:taken, :name, "John"}]}

@solnic solnic merged commit 10ad492 into main Sep 9, 2023
1 check passed
@solnic solnic deleted the add-support-for-arbitrary-rules branch September 9, 2023 08:59
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