An Implementation of JSON Type Definition in Elixir.
The package can be installed by adding to your list of dependencies in mix.exs:
def deps do
[{:jtd, "~> 0.1"}]
end
schema = JTD.Schema.from_map(
%{
"properties" => %{
"name" => %{"type" => "string"},
"age" => %{"type" => "uint32"},
"phones" => %{
"elements" => %{
"type" => "string"
}
}
}
}
)
JTD.validate(schema, %{
"name" => "John Doe",
"age" => 43,
"phones" => ["+44 1234567", "+44 2345678"],
})
# Output: []
JTD.validate(schema, %{
"age" => "43",
"phones" => ["+44 1234567", 442345678],
})
# Output:
# [
# %JTD.ValidationError{
# instance_path: ["age"],
# schema_path: ["properties", "age", "type"]
# },
# %JTD.ValidationError{instance_path: [], schema_path: ["properties", "name"]},
# %JTD.ValidationError{
# instance_path: ["phones", "1"],
# schema_path: ["properties", "phones", "elements", "type"]
# }
# ]