is v1.0.0 Is.AliasType protocol View Source
Defines validator alias for a given type.
It allows to use value type to call a specific validator.
For now, only Map
type (Is.Validators.Map
) has a type alias enabled.
Example
These two lines are equivalent:
iex> Is.validate(%{a: true}, map: %{a: :boolean})
[]
iex> Is.validate(%{a: true}, %{a: :boolean})
[]
Because we define an alias type for Map
:
defimpl Is.AliasType, for: Map do
def get(_) do
{:ok, :map}
end
end
You can also provide default options when returning an alias type:
defimpl Is.AliasType, for: MyStruct do
def get(_) do
{:ok, :map, %{a: :boolean}}
end
end
Which will allow us to valid MyStruct
data using:
iex> Is.validate(%MyStruct{a: 1}, %MyStruct{})
[{:error, [:a], "must be a boolean"}]
Link to this section Summary
Functions
Get type alias for given value
Link to this section Types
Link to this section Functions
Get type alias for given value.