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

Support to declare that a key needs to conform with the given Struct #49

Open
Exadra37 opened this issue May 31, 2024 · 1 comment
Open

Comments

@Exadra37
Copy link

I want to be able to declare the following contract required(:key) => %SomeStruct{} on a drop.

It doesn't seem possible to achieve this, even when using custom types, or am I missing something?

Full example:

defmodule MyApp.APIContract do
    use Drops.Contract

    alias MyApp.WhateverStruct

    @enforce_keys [:context, :action]

    defstruct @enforce_keys

    schema do
      %{
        required(:context) => %WhateverStruct{},
        required(:action) => string(:filled?),
      }
    end

    def new(attrs) when is_map(attrs) do
      case conform(attrs) do
        {:ok, attrs} ->
          struct(__MODULE__, attrs)

        error ->
          error
      end
    end
  end
@solnic
Copy link
Owner

solnic commented Jun 2, 2024

Thanks for reporting the issue! This will be supported eventually 😄

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

No branches or pull requests

2 participants