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

Add super basic schema DSL #1

Closed solnic closed 1 year ago

solnic commented 1 year ago

This is an early implementation of the schema DLS provided by the Drops.Contract module.

defmodule TestContract do
  use Drops.Contract

  schema do
    %{
      required(:user) => %{
        required(:name) => type(:string, [:filled?]),
        required(:age) => type(:integer),
        required(:address) => %{
          required(:city) => type(:string, [:filled?]),
          required(:street) => type(:string, [:filled?]),
          required(:zipcode) => type(:string, [:filled?])
        }
      }
    }
  end
end

TestContract.conform(%{
  user: %{
    name: "",
    age: 21,
    address: %{
      city: "New York",
      street: "",
      zipcode: "10001"
    }
  }
})
# {:error,
#  [
#    error: [
#      [{:filled?, [:user, :address, :street], ""}],
#      {:filled?, [:user, :name], ""}
#    ]
#  ]}