Macaw.Layers.Document behaviour (macaw_layers v0.6.0)

Copy Markdown View Source

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

A document type implements this behavior: 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).

Summary

Callbacks

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

Functions

The value judged by its type's 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.

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.

Callbacks

validate(value)

@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).

Functions

check(value, type)

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

The value judged by its type's 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(map, known, path \\ [])

@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}]