# `Macaw.Layers.Document`
[🔗](https://github.com/dcoai/macaw_layers/blob/v0.6.0/lib/macaw/layers/document.ex#L1)

A document type's validity, judged on its value (spec/01 §5).

A document type implements this behavior: `c:validate/1`, which judges the type's
**value**, the terms Elixir holds (atom keys where the type has a key, string names
elsewhere, atoms, times, the type's own structs), never a representation of it. `check/2`
is how any value is judged, whether built in Elixir or read from an encoding:

    {:ok, value} = Macaw.Layers.Document.check(value, MyApp.Page)

The kernel knows no encoding here. Reading one is an adapter's: `Macaw.Layers.Form` is the
JSON adapter, whose `read/3` takes a JSON document through the gates (the bytes, the
scan, the version, the migration, the recount), reads its value from the map form, and
then judges the value by `check/2`. The limits bound what reading an encoding costs, so
they are the adapter's; a value is judged with none.

A value's digest, its identity as a term, is macaw_base's (`Macaw.Base.Encoding.digest/1`,
spec/01 §7).

# `validate`

```elixir
@callback validate(value :: term()) :: :ok | {:error, [Macaw.Base.Fault.t(), ...]}
```

`:ok`, or every fault of the type's value. Unknown keys are refused by name (see
`unknown_keys/3`).

# `check`

```elixir
@spec check(term(), module()) :: {:ok, term()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The value judged by its type's `c:validate/1`: `{:ok, value}`, or every fault, ordered by
path. No encoding is involved: a value holding a term no adapter can write is judged as
the type's own rules say, and fails only at that adapter.

# `unknown_keys`

```elixir
@spec unknown_keys(map(), [term()], Macaw.Base.Fault.path()) :: [Macaw.Base.Fault.t()]
```

The faults of `map`'s keys that are not among `known`, each at its own key under `path`:
`{path ++ [key], :unknown_key}`, ordered by path.

`known` is the type's keys as its value holds them, atoms in a value. A key the type
knows stays the type's to read; one it does not is refused by name, never dropped. A key
a reader kept at its own spelling, a string, is named as it was written.

    iex> Macaw.Layers.Document.unknown_keys(%{:title => "t", "ttle" => "x"}, [:title], [:page])
    [{[:page, "ttle"], :unknown_key}]

---

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