Schema-first validation library for Elixir.
Usage examples can be found in the tests.
def deps do
[
{:decoder, "~> 0.1.0"}
]
end
map
,record
,tuple
,list
string
,integer
,boolean
,any
,nil
,atom
,literal
union
,optional
description
- field error descriptionextra_props
- omit/include extra propsstrict
- strict mode
Example:
assert {:ok, %{"name" => "Bruce Wayne", "age" => 30}} ===
D.decode(
D.map(
%{
"name" => D.string(),
"age" => D.integer()
},
extra_props: false
),
%{
"name" => "Bruce Wayne",
"age" => 30,
"extra" => "extra"
}
)
Derives can sanitize, validate and format data.
trim
gt
,gte
,lt
,lte
min
,max
not_empty
format
Examples:
assert {:ok, "Alfred Pennyworth"} = D.decode(D.string(derive: [:trim]), " Alfred Pennyworth ")
assert {:ok, DateTime.from_iso8601("2021-08-10T00:00:00Z") |> elem(1)} ==
D.decode(D.string(derive: [{:format, "date-time"}]), "2021-08-10T00:00:00Z")
assert {:error, [%{message: "should be longer than 5"}]} =
D.decode(D.string(derive: [:trim, {:min, 5}]), "f")
assert {:error, [%{message: "should not be empty"}]} =
D.decode(D.string(derive: [:trim, :not_empty]), " ")