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 aschema_versionother 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:elementsin a version 1 document, or one declaring no version, andnodesin a version 2 document. One finding, whether or not it carries its own version's key beside it, itsfieldthe key refused and itselement_keythe 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 alreadydocument.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_keyis 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 thefield"checks"and a message naming the check by its place and its code.document.invalid_check- acheckson 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 nocondition,codeormessage, acodethat is not a string of two or more dotted segments beginningcheck(an author's code, which this package never raises), amessagethat is not a non-empty string, and acodean earlier check on the same question carries. Itsfieldis"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'slabelanddescription, an image'salt, and theaccessible_labelandaccessible_descriptionany element may carry among them. An option'sdescription, analtor 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'ssrcor a link'shref, written as a literal, is not a URL the rule admits: anhttps://URL naming a host, a host-relative URL, or, for an image'ssrcalone, a base64data:image of one of four raster types within its bound, asRiddler.Screens.Type.ImageandRiddler.Screens.Type.Linkstate. Itsfieldis"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'ssizeorfitis there and is not a string, or a choice question'ssearchableis there and is not a boolean.document.invalid_style- astyleis 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'svalidatesis there and is not a boolean.document.invalid_outcome- a button'soutcomeis there and is not a string,nilincluded. One finding per such button, itsfield"outcome", raised with the button's other field findings. A button with nooutcomeisdocument.missing_field, and is not this finding too.document.invalid_required- a question'srequiredis there and is not a boolean.document.invalid_select- a choice question'sselectis there and is neither"one"nor"many".document.empty_answer_options- a choice question'sanswer_optionsis an empty list.document.invalid_option- a choice question's option carries novalue, avaluenot matching[a-z][a-z0-9_]*, or nolabel.document.duplicate_option_value- two options of one choice question carry the samevalue. Option values are not keys, so two questions may offer the same one.document.invalid_selection_bounds- a choice question'smin_selectionsormax_selectionson a question whoseselectis not"many", one that is not a non-negative integer, amin_selectionsabovemax_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'sminormaxis there and is not in the shape of the format that reads it: a number for theintegerandnumberformats, and a string in the format's own shape fordate,timeanddatetime, where aminafter itsmaxis refused too, itsfield"max". A bound under any other format, or none, is a field nothing consults and raises nothing, and a numericminabove itsmaxis admitted.document.invalid_step- a text question'sstepis there and is not a number, whatever the format, or none; or is there under theintegerornumberformat and is not a number above zero, or, underinteger, 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 thepatternformat declares apatternthat 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: apatternon 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'sdefaultis not of the question's response shape: a string for a text question,trueorfalsefor a boolean question, and for a choice question one of its own options' values underselect: "one"or a list of different ones under"many". A choice question whoseselectis 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- aprefill, a choice question'sanswer_options_from, or an image'ssrcor a link'shrefbeginningcontext., that is not a string of dotted segments starting atcontext, with at least one more after it, each in the key shape[a-z][a-z0-9_]*; itsfieldnames which.document.conflicting_options- a choice question carries bothanswer_optionsandanswer_options_from, itsfield"answer_options_from". A choice question carrying neither isdocument.missing_fieldonanswer_options.document.invalid_messages- a question'smessagesis there and is not an object, or a value in it is not a string. One finding per such value, itsfield"messages".document.unknown_message_code- a key of a question'smessagesis not a code its type raises on a response, the message naming the key. A text question may keyresponse.required,response.formatandresponse.out_of_range; a choice questionresponse.required,response.invalid_shape,response.not_an_optionandresponse.out_of_range; a boolean or a secret questionresponse.requiredandresponse.invalid_shape. Which codes a type raises is per type, not per question.document.secret_initial_value- a secret question carries adefaultor aprefill, itsfieldnaming 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'swritesentry, anywhere in the document, names a secret question's response,responses.<key>, or a path under it. Itselement_keyis the element carrying the reference and itsfieldthe 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 sameoutcome, 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, itsfield"outcome"and itselement_keythe repeating button's key; the message names the earlier button. Only outcomes that are strings are compared, so a button raisingdocument.invalid_outcomeis 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.
An admitted document.
Functions
Turns a decoded JSON document into the struct, or answers nil.
Returns {:ok, document} or every finding against it.
Types
@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.
@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
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
@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"}