Skip to content

Commit

Permalink
Add custom type example file
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Nov 3, 2023
1 parent 7ac99db commit 611d2ab
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/types/custom-01.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule FilledString do
use Drops.Type, {:string, [:filled?]}
end

defmodule UserContract do
use Drops.Contract

schema do
%{
required(:name) => FilledString
}
end
end

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

{:error, errors} = UserContract.conform(%{name: 1})
Enum.map(errors, &to_string/1)
# ["name must be a string"]

{:error, errors} = UserContract.conform(%{name: ""})
Enum.map(errors, &to_string/1)
# ["name must be filled"]

[%{type: type}]= UserContract.schema().keys
# %FilledString{
# primitive: :string,
# constraints: {:and, [predicate: {:type?, :string}, predicate: {:filled?, []}]}
# }

0 comments on commit 611d2ab

Please sign in to comment.