Skip to content

JSON encoder/decoder with strong typing and validation

License

Notifications You must be signed in to change notification settings

operable/conduit

Repository files navigation

Conduit

Build Status Coverage Status Ebert

Typed and validated JSON encoder and decoder

Installation

def deps do
  [{:conduit, github: "operable/conduit", branch: "master"}]
end

Usage

Defining Conduit structs

defmodule MyApp.User do
  use Conduit

  field :first_name, :string, required: true
  field :last_name, :string, required: true
  field :userid, :string, required: true
end

defmodule MyApp.Request do
  use Conduit

  field :command, :string, required: true
  field :args, array: :string
  field :requestor, object: MyApp.User
end

Marshaling and unmarshaling

try do
  request = MyApp.Request.decode!(json)
rescue
  e in Conduit.ValidationError ->
    log_bad_data(e, json)
end

Validation

request = MyApp.Request.decode!(json) |> evaluate_args
try do
  request.validate!
rescue
  e in Conduit.ValidatinError ->
    log_bad_args(e, json)
end

About

JSON encoder/decoder with strong typing and validation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages