solnic / drops

🛠️ Tools for working with data effectively - data contracts using types, schemas, domain validation rules, type-safe casting, and more.
Other
257 stars 6 forks source link

Support for string maps #5

Closed solnic closed 1 year ago

solnic commented 1 year ago

This adds atomize: true | false option to schema DSL which can be used to have string-based maps turned into atom-based maps:

defmodule TestContract do
  use Drops.Contract

  schema(atomize: true) 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" => "John",
   "age" => 21,
   "address" => %{
     "city" => "New York",
     "street" => "Central Park",
     "zipcode" => "10001"
   }
 }
})
# {:ok,
#  %{
#    user: %{
#      name: "John",
#      address: %{city: "New York", street: "Central Park", zipcode: "10001"},
#      age: 21
#    }
#  }}