Absurd.JSON (absurd v0.2.0)

Copy Markdown View Source

The JSON boundary shared with the Absurd PostgreSQL schema.

Absurd stores protocol values in JSONB. Consequently, arbitrary Elixir terms, structs, tuples, atom map keys, and ETF values are rejected before a query is issued.

Examples

iex> Absurd.JSON.valid?(%{"name" => "Ada", "attempt" => 1})
true

iex> match?({:error, %Absurd.Error{kind: :validation}}, Absurd.JSON.validate(%{name: "Ada"}))
true

iex> Absurd.JSON.encode([true, nil, 3])
{:ok, "[true,null,3]"}

Summary

Types

A scalar accepted by JSON and PostgreSQL JSONB.

A recursively JSON-compatible Elixir value with binary object keys.

Functions

Validates and encodes a protocol value with Jason.

Returns whether value can cross the protocol JSON boundary.

Validates a protocol value.

Types

scalar()

@type scalar() :: nil | boolean() | number() | String.t()

A scalar accepted by JSON and PostgreSQL JSONB.

value()

@type value() :: scalar() | [value()] | %{optional(String.t()) => value()}

A recursively JSON-compatible Elixir value with binary object keys.

Functions

encode(value)

@spec encode(term()) :: {:ok, String.t()} | {:error, Absurd.Error.t()}

Validates and encodes a protocol value with Jason.

valid?(value)

@spec valid?(term()) :: boolean()

Returns whether value can cross the protocol JSON boundary.

validate(value)

@spec validate(term()) :: :ok | {:error, Absurd.Error.t()}

Validates a protocol value.

Returns an Absurd.Error describing the first invalid value and its path.