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

How to define a filled list of strings #50

Open ndan opened 4 months ago

ndan commented 4 months ago

I'm struggling to define a contract to validate a list of strings where each string must be filled, and the minimum size of the list is 1.

%{tags: ["hello"]]} # valid

%{tags: []}   # invalid
%{tags: [1]}  # invalid

I ended up writing a rule, but is it possible to achieve it without a rule?

defmodule Contract do
  use Drops.Contract

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

  rule(:tags_filled, %{tags: []}) do
    {:error, "tags must not be empty"}
  end
end
solnic commented 3 months ago

Oops, seems like I forgot about this basic usecase. Thanks for reporting the issue, I'll add this eventually.