Skip to content

nobuyo/json-typedef-elixir

Repository files navigation

jtd: JSON Validation for Elixir

An Implementation of JSON Type Definition in Elixir.

Installation

The package can be installed by adding to your list of dependencies in mix.exs:

  def deps do
    [{:jtd, "~> 0.1"}]
  end

Basic Usage

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"]
#   }
# ]

Links

About

Elixir implementation of JSON Type Definition

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages