View Source Shapex.Type behaviour (shapex v0.3.1)
A behaviour for defining custom types.
Example:
defmodule Email do
@behaviour Shapex.Type
@type t :: %__MODULE__{
allowed_domains: [String.t()]
}
defstruct [:allowed_domains]
def validate(schema, value) do
results = case String.split(value, "@") do
[_, domain] when domain in schema.allowed_domains -> :ok
_ -> {:error, %{type: "Invalid email"}}
end
if results == %{} do
:ok
else
{:error, results}
end
end
end
Shapex.validate(%Email{allowed_domains: ["example.com"]}, "ilya@example.com")
:ok
Summary
Types
@type t() :: struct()