# `Macaw.Definition.Schema`
[🔗](https://github.com/dcoai/macaw/blob/v0.6.0/lib/macaw/definition/schema.ex#L1)

A schema's map and JSON forms (spec/15 §1): an adapter at the edge, moved from
macaw_data (dco-tek/macaw/macaw#478 decision 2, #480). The schema is the term,
`Macaw.Data.Schema`, and its version never goes through this form; JSON is converted
only here, at the edge.

The map form is the bare list of `[name, description]` pairs, sorted by name, for a
document that embeds a schema (a derived source's `"schema"`, a static source's
description), whose own stamp versions it. The JSON document is an object,
`{"$version": 1, "fields": pairs}`, on its own version line, read through the kernel's
gates (`Macaw.Layers.Form.gates/3`) and no gate of its own.

Every name, state and role is written by its spelling (`Macaw.Data.Schema.spell/1`),
every word of the vocabulary (`type`, `quantity`, `polarity`, `metric_kind`) as its
string; a reader reads each word through its closed vocabulary
(`Macaw.Data.Schema.vocabularies/0`), so **no atom is made from a document**. What is
read is judged by `Macaw.Data.Schema.new/1`, whose faults are the reader's: a key no
description or entry has, a word outside its vocabulary and an entry's `"color"` each
stay their string, for `new/1` to refuse by name.

    iex> schema = Macaw.Data.Schema.new!(%{latency: %{type: :number, unit: "ms"}})
    iex> Macaw.Definition.Schema.to_map(schema)
    [["latency", %{"type" => "number", "unit" => "ms"}]]
    iex> Macaw.Definition.Schema.from_json(Macaw.Definition.Schema.to_json(schema))
    {:ok, schema}

# `format_error`

```elixir
@spec format_error(Macaw.Data.Schema.error()) :: String.t()
```

One fault of reading a schema as a line a person reads: a reason the kernel's gates give
(a limit, a URL, a tag) in macaw_layers' words (`Macaw.Layers.Words`), every other as
`Macaw.Data.Schema.format_error/1` words it.

    iex> Macaw.Definition.Schema.format_error({[], {:limit, :bytes, 10, 12}})
    "schema: is 12 bytes; the limit bytes is 10"

# `from_json`

```elixir
@spec from_json(binary(), keyword()) ::
  {:ok, Macaw.Data.Schema.t()} | {:error, [Macaw.Data.Schema.error(), ...]}
```

The schema a JSON document holds: read through the kernel's gates
(`Macaw.Layers.Form.gates/3`: the bytes, the scan's depth and items before any value is
built, the decode, a newer `"$version"` refused by name, the migration, the recount),
`limits:` tightening them, then its `"fields"` read by `from_map/1`. The document holds
`"$version"` and `"fields"` only. A legacy document, the bare pairs written before the
version line, has a list for its root: once its bytes and nesting pass the gates, it is
lifted to version 1, the version whose content it is, and read from there.

# `from_json!`

```elixir
@spec from_json!(binary(), keyword()) :: Macaw.Data.Schema.t()
```

As `from_json/2`, raising `ArgumentError` naming every fault.

# `from_map`

```elixir
@spec from_map(term()) ::
  {:ok, Macaw.Data.Schema.t()} | {:error, [Macaw.Data.Schema.error(), ...]}
```

The schema a map form holds: `Macaw.Data.Schema.new/1` of what it reads, `{:ok,
schema}` or every fault by path. Anything but a list of `[string, map]` pairs is
`{[], {:type, :schema}}`. A struct as a description or a value entry stays as given, for
`new/1` to refuse at its path.

# `from_map!`

```elixir
@spec from_map!(term()) :: Macaw.Data.Schema.t()
```

As `from_map/1`, raising `ArgumentError` naming every fault.

# `to_json`

```elixir
@spec to_json(Macaw.Data.Schema.t()) :: String.t()
```

The schema's JSON document: an object stamped with the form's version,
`{"$version": 1, "fields": pairs}`, `fields` being `to_map/1`, encoded.

# `to_map`

```elixir
@spec to_map(Macaw.Data.Schema.t()) :: [[term()]]
```

The schema's map form: the JSON-ready list of `[name, description]` pairs, sorted by
name, every name, state and role written by its spelling, every word of the vocabulary
as its string.

---

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