View Source Shapex.Type behaviour (shapex v0.2.0)
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, :valid}
_ -> {:error, %{type: "Invalid email"}}
end
if results == %{} do
{:ok, :valid}
else
{:error, results}
end
end
end
Shapex.validate(%Email{allowed_domains: ["example.com"]}, "ilya@example.com")
{:ok, :valid}
Summary
Types
@type t() :: struct()