# `Omni.Content.Data`
[🔗](https://github.com/aaronrussell/omni/blob/v2.0.0/lib/omni/content/data.ex#L1)

A data content block: structured data sent to the model as JSON.

Used in decisions, as part of the state or of a question's instructions
(see `Omni.Decision`). Not a message content block.

```elixir
Omni.Content.Data.new(%{ticket: %{subject: "Refund", plan: :enterprise}})
#=> %Omni.Content.Data{data: %{"ticket" => %{"subject" => "Refund", "plan" => "enterprise"}}}
```

## Struct fields

- `:data` - the data, as the JSON-decoded map the model sees

`new/1` passes the map through JSON, so the struct holds exactly what the
model sees: string keys, atoms as strings, and any struct with a
`JSON.Encoder` implementation (such as `Date`) in its JSON form. A value
JSON can't encode raises here, not when the request is sent.

# `t`

```elixir
@type t() :: %Omni.Content.Data{data: map()}
```

Structured data, as the JSON-decoded map sent to the model.

# `new`

```elixir
@spec new(map() | Enumerable.t()) :: t()
```

Creates a data block from the data map itself, or from a keyword list
(`data: map`), normalising the map through JSON.

```elixir
Omni.Content.Data.new(%{plan: :pro})
Omni.Content.Data.new(data: %{plan: :pro})
```

A map is always the data, never attributes (unlike `Omni.Content.Text.new/1`),
so `new(%{data: x})` builds `%{"data" => x}`.

Raises if `:data` is missing, is not a plain map, or holds a value JSON
can't encode.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
