defmodule YaloAuthor do @moduledoc """ Read the README.md for the YaloMessage spec. """ defstruct [:id, :role] def schema do %{ "type" => "object", "properties" => %{ "id" => %{"type" => "string"}, "role" => %{"type" => "string"} }, "required" => ["id", "role"] } end end defmodule YaloPhoto do @moduledoc """ Read the README.md for the YaloPhoto spec. """ defstruct [:url] def schema do %{ "type" => "object", "properties" => %{ }, "required" => [] } end end defmodule YaloDocument do @moduledoc """ Read the README.md for the YaloDocument spec. """ defstruct [:name] end defmodule YaloMessage do @moduledoc """ Read the README.md for the YaloMessage spec. """ @derive [Poison.Encoder] @enforce_keys [:botSlug, :date, :author, :m_id, :provider, :m_type, :reader] defstruct [:botSlug, :date, :author, :m_id, :provider, :reader, :m_type, :status, :text, :photo, :document] def schema do %{ "type" => "object", "properties" => %{ "botSlug" => %{"type" => "string"}, "date" => %{"type" => "integer"}, "author" => YaloAuthor.schema, "m_id" => %{"type" => "string"}, "provider" => %{"type" => "string"}, "reader" => %{"type" => "string"}, "m_type" => %{"type" => "string"}, "status" => %{"type" => "string"}, }, "required" => ["botSlug", "date", "author", "m_id", "provider", "m_type", "reader"] } end def decode_json(message) do Poison.decode(message, as: %YaloMessage{ botSlug: String, date: String, author: %YaloAuthor{}, m_id: String, reader: String, provider: String, m_type: String, }) end def is_valid(m), do: ExJsonSchema.Validator.validate(schema(), Poison.decode!(Poison.encode!(m))) == :ok end