Riddler.Screens.Document (Riddler v0.9.0)

Copy Markdown View Source

The screen document: what a host declares, and why one is refused.

A document is the contract between a host that authors content and any runtime that shows it. This module is the two halves of admitting one. admit/1 turns decoded JSON into the struct the rest of the package reads, or says the input is not a document at all. validate/1 takes that struct and returns every reason the document is wrong, all of them, so an author fixing what they wrote learns everything in one pass.

Neither raises on anything a host can produce, and neither consults a context: a document is wrong or right on its own, before a visitor exists, which is what lets an editor answer the author while the author is still looking at it.

What admit/1 takes and what it builds

It takes the decoded JSON map - string keys throughout, as Jason.decode!/1 gives - and builds a struct whose fields are atoms. The two are different things: what the host writes is JSON, and what this package reads is the struct. A screen is %{key: ..., title: ..., elements: [...]} and an element is an atom-keyed map carrying :type, its :key and :condition where the document declared them, its :accessible_label and :accessible_description where the document wrote them, on any type, its :checks where the document wrote them, each check that is an object carried atom-keyed with its :condition, :code and :message and nothing else, and the fields its type names. metadata is the one map that keeps its string keys: it is an open map by declaration, and a host may put what it likes beside name, description and domain.

The envelope's kind names the content kind the document belongs to, and a document that omits it is a screen document: the admitted struct carries "screens" for it, so a document authored before kinds existed is admitted unchanged. A kind this package has no runtime for is carried as it was written and refused by validate/1.

The envelope's schema_version says which key a screen and a variant spell their children under: nodes at version 1, which is the version of a document that declares none, and elements at version 2. Either way the admitted struct carries them as elements. The other version's key is held to the shape the schema gives it and is then carried nowhere; validate/1 refuses the screen or the variant that carried it. That shape is a list of elements, except an elements in a version 1 document, on a screen or an element, which is any value, as it was before version 2 existed. A document at a version this package does not implement is read as riddler 0.7.0 read it, from nodes, unless none of its screens carries that key; then it is read from elements as a version 2 document is, refusing no key.

admit/1 answers nil for an input that is not a screen document - not a map, or a map whose spine is not a document's: no list of screens, a metadata that is there and is not an object, null included, a screen that is not an object, a screen with no list of elements, an element that is not an object, a variant whose candidates are not a list. It answers nil too for a value of the wrong JSON type in a field the document schema types: an id, a kind, a screen's key or title, or an element's key, type or condition that is there and is not a string, null included; a schema_version that is there and is not an integer, a number with no fractional part counting as one and admitted as its integer; a nodes on a element whose type reads none that is not a list of elements; and an answer_options on a choice question that is not a list of objects, or an option whose value or label is there and is not a string. A list of elements there is admitted and dropped, as any field the type does not name is. That is the whole of what it refuses, and the document schema refuses each of them too, so a value the schema calls a document is admitted. Everything else - a missing field, a key of the wrong shape, a level of 9, a condition that does not parse - is a document that exists and is wrong, and saying which is validate/1's job.

A field this version does not know is not carried onto the admitted element. metadata is the declared place for what a host wants to keep beside the vocabulary, and the admitted element map is atom-keyed by declaration, so there is nowhere on an element for an unrecognized key to live. Whether such a key should also be a finding is not decided by the record this module implements, and this version does not raise one.

The findings validate/1 raises

One code per check, stable, and a host switches on the code rather than on the wording:

  • document.unknown_kind - the envelope names a content kind this package has no runtime for. The finding carries the value and no element key, because the envelope is the document's and not any one element's.
  • document.invalid_schema_version - the envelope declares a schema_version other than the two this package implements, 1 and 2. No element key, for the same reason.
  • document.other_version_spelling - a screen or a variant carries the other version's key for its children: elements in a version 1 document, or one declaring no version, and nodes in a version 2 document. One finding, whether or not it carries its own version's key beside it, its field the key refused and its element_key the screen's or the variant's. Nothing under that key is admitted, so a variant refused for it is not also missing its candidates.
  • document.unknown_type - the registry has no such type.
  • document.duplicate_key - a key used twice anywhere in the document. Only the keys that are strings are compared. A key that is not a string is already document.invalid_key, and two elements carrying the same non-string key are never reported as a duplicate: there is no key there to have been used twice, and saying there is would tell the author to rename one of them when what each of them needs is a key.
  • document.invalid_key - a key missing, a key that is there and is not a string, or a key not matching [a-z][a-z0-9_]*. One code, three messages, because all three are the same mistake from the document's side: the element has no name anything else can use. The first two carry no element key - element_key is how a host looks the element up, and neither an absent key nor a key of the wrong form is a name to look one up by - and the key the document did write is named in the message.
  • document.missing_field - a field the element's type requires.
  • document.level_out_of_range - a heading level outside 1 to 6.
  • document.invalid_condition - a condition that does not parse, an element's or a check's; a check's carries the field "checks" and a message naming the check by its place and its code.
  • document.invalid_check - a checks on an element that is not a question, or one that is not a list; and, per entry, an entry that is not an object, one with no condition, code or message, a code that is not a string of two or more dotted segments beginning check (an author's code, which this package never raises), a message that is not a non-empty string, and a code an earlier check on the same question carries. Its field is "checks", and its message names the entry by its place, counting from one, and by its code where it carries one.
  • document.invalid_template - a template field holding a construct outside the template subset, a choice question option's label and description, an image's alt, and the accessible_label and accessible_description any element may carry among them. An option's description, an alt or an accessible field that is not a string is this finding too. The finding carries the construct the template refused, and the element's key.
  • document.invalid_writes - a write that does not address a response, or whose value is not the constant form.
  • document.invalid_url - an image's src or a link's href, written as a literal, is not a URL the rule admits: an https:// URL naming a host, a host-relative URL, or, for an image's src alone, a base64 data: image of one of four raster types within its bound, as Riddler.Screens.Type.Image and Riddler.Screens.Type.Link state. Its field is "src" or "href", and its message says which part of the rule the value fails and never quotes the value.
  • document.invalid_hint - an image's size or fit is there and is not a string, or a choice question's searchable is there and is not a boolean.
  • document.invalid_style - a style is there and is not a string, on an element whose type names one: a text, a link, a button, a text question or a choice question. Which string it is stays the renderer's business; that it is a string is this package's.
  • document.invalid_validates - a button's validates is there and is not a boolean.
  • document.invalid_outcome - a button's outcome is there and is not a string, nil included. One finding per such button, its field "outcome", raised with the button's other field findings. A button with no outcome is document.missing_field, and is not this finding too.
  • document.invalid_required - a question's required is there and is not a boolean.
  • document.invalid_select - a choice question's select is there and is neither "one" nor "many".
  • document.empty_answer_options - a choice question's answer_options is an empty list.
  • document.invalid_option - a choice question's option carries no value, a value not matching [a-z][a-z0-9_]*, or no label.
  • document.duplicate_option_value - two options of one choice question carry the same value. Option values are not keys, so two questions may offer the same one.
  • document.invalid_selection_bounds - a choice question's min_selections or max_selections on a question whose select is not "many", one that is not a non-negative integer, a min_selections above max_selections, or either above the number of options. A bound with no fractional part counts as its integer.
  • document.unknown_format - a format name this package does not know.
  • document.invalid_bound - a text question's min or max is there and is not in the shape of the format that reads it: a number for the integer and number formats, and a string in the format's own shape for date, time and datetime, where a min after its max is refused too, its field "max". A bound under any other format, or none, is a field nothing consults and raises nothing, and a numeric min above its max is admitted.
  • document.invalid_step - a text question's step is there and is not a number, whatever the format, or none; or is there under the integer or number format and is not a number above zero, or, under integer, is a number with a fractional part. A number step under any other format, or none, is a field nothing consults and raises nothing.
  • document.invalid_pattern - a question that asks for the pattern format declares a pattern that format cannot compile. Nothing a visitor could type would satisfy it, so it is the document that is wrong and not a response. Only where the question declares that format: a pattern on a question asking for another format, or for none, is a field nothing consults and raises nothing.
  • document.invalid_default - a text, choice or boolean question's default is not of the question's response shape: a string for a text question, true or false for a boolean question, and for a choice question one of its own options' values under select: "one" or a list of different ones under "many". A choice question whose select is neither has no shape to read, and its default is not checked. A choice question that reads its options from context has no option of its own for a default to name, and any default on it is refused.
  • document.invalid_context_path - a prefill, a choice question's answer_options_from, or an image's src or a link's href beginning context., that is not a string of dotted segments starting at context, with at least one more after it, each in the key shape [a-z][a-z0-9_]*; its field names which.
  • document.conflicting_options - a choice question carries both answer_options and answer_options_from, its field "answer_options_from". A choice question carrying neither is document.missing_field on answer_options.
  • document.invalid_messages - a question's messages is there and is not an object, or a value in it is not a string. One finding per such value, its field "messages".
  • document.unknown_message_code - a key of a question's messages is not a code its type raises on a response, the message naming the key. A text question may key response.required, response.format and response.out_of_range; a choice question response.required, response.invalid_shape, response.not_an_option and response.out_of_range; a boolean or a secret question response.required and response.invalid_shape. Which codes a type raises is per type, not per question.
  • document.secret_initial_value - a secret question carries a default or a prefill, its field naming which. A secret is a value only the visitor supplies, and the finding never repeats what the field holds.
  • document.secret_reference - a template, a condition, a check's condition or a button's writes entry, anywhere in the document, names a secret question's response, responses.<key>, or a path under it. Its element_key is the element carrying the reference and its field the field that carries it; the message names the secret question's key, which is not a secret.
  • document.duplicate_outcome - two buttons that a screen can show together raise the same outcome, so a client handed that screen could not tell which of them the visitor pressed. Two buttons can be shown together unless they are candidates in different arms of one variant, which resolves to one winner; a condition on a button does not keep it from being compared, because which of two conditions holds is the root's and not the document's. One finding for each button that repeats an outcome a button before it on the screen raises, its field "outcome" and its element_key the repeating button's key; the message names the earlier button. Only outcomes that are strings are compared, so a button raising document.invalid_outcome is not compared, and the findings come after every other finding on the screen.
  • document.empty_variant - a variant with no candidates.
  • document.unreachable_variant_candidate - an unconditional candidate that is not last, which buries every candidate after it.

Examples

iex> doc = Riddler.Screens.Document.admit(%{
...>   "schema_version" => 2,
...>   "id" => "edoc_signup",
...>   "screens" => [%{"key" => "account", "title" => "Create your account", "elements" => [
...>     %{"type" => "heading", "key" => "account_heading", "level" => 1, "text" => "Create your account"}
...>   ]}]
...> })
iex> {:ok, ^doc} = Riddler.Screens.Document.validate(doc)
iex> hd(hd(doc.screens).elements).key
"account_heading"

iex> Riddler.Screens.Document.admit("not a document")
nil

Summary

Types

An admitted screen: its key, its title, and its elements in order, whichever version the document was written in. A screen that carried the other version's key carries that key as :other_version_spelling, which is what validate/1 refuses it by. The key is an internal mark, not part of what a host reads: a valid document never carries it, and resolution drops it.

t()

An admitted document.

Functions

Turns a decoded JSON document into the struct, or answers nil.

Returns {:ok, document} or every finding against it.

Types

screen()

@type screen() :: %{
  :key => String.t() | nil,
  :title => String.t() | nil,
  :elements => [Riddler.Screens.Type.element()],
  optional(:other_version_spelling) => String.t()
}

An admitted screen: its key, its title, and its elements in order, whichever version the document was written in. A screen that carried the other version's key carries that key as :other_version_spelling, which is what validate/1 refuses it by. The key is an internal mark, not part of what a host reads: a valid document never carries it, and resolution drops it.

t()

@type t() :: %Riddler.Screens.Document{
  id: String.t() | nil,
  kind: String.t(),
  metadata: %{optional(String.t()) => term()},
  schema_version: integer() | nil,
  screens: [screen()]
}

An admitted document.

Functions

admit(raw)

@spec admit(term()) :: t() | nil

Turns a decoded JSON document into the struct, or answers nil.

Total: it never raises, whatever it is handed. nil means the input is not a screen document. A document that is wrong rather than absent is admitted here and refused by validate/1.

iex> Riddler.Screens.Document.admit(%{"screens" => []})
%Riddler.Screens.Document{schema_version: nil, kind: "screens", id: nil, metadata: %{}, screens: []}

iex> Riddler.Screens.Document.admit(%{"screens" => "three of them"})
nil

validate(document)

@spec validate(t()) :: {:ok, t()} | {:error, [Riddler.Finding.t()]}

Returns {:ok, document} or every finding against it.

The checks are the document's own - keys, conditions, templates, duplicates, and the fields each type requires - together with the checks each type raises about itself. Findings come back in document order, element by element, with the duplicate keys first because they are about the document rather than about any one element.

iex> doc = Riddler.Screens.Document.admit(%{"schema_version" => 2, "screens" => [
...>   %{"key" => "account", "title" => "Create your account", "elements" => [
...>     %{"type" => "carousel", "key" => "pictures"}
...>   ]}
...> ]})
iex> {:error, [finding]} = Riddler.Screens.Document.validate(doc)
iex> {finding.code, finding.element_key}
{"document.unknown_type", "pictures"}