Riddler.Screens.Type.ChoiceQuestion (Riddler v0.9.0)

Copy Markdown View Source

A question a visitor answers by picking from a list of options.

label is what the visitor is asked, and it is a template. help is a line of guidance shown beside the question, and it is a template too. select is "one" or "many", and it fixes the shape of the response: one option's value under "one", a list of option values under "many".

answer_options is the list of options, in the order the visitor is shown them. An option carries value and label. value is what the response holds when the visitor picks that option: a string in the key shape [a-z][a-z0-9_]*, so that a condition or a template compares a response with it as written, and unique among this question's own options. It is not a key, so two questions may both offer the same value. label is what the visitor reads, and it is a template. An option may also carry description, a line a renderer shows under its label, and a template too: one that is not a string, or that holds a construct outside the subset, is document.invalid_template. Admission keeps exactly those three fields of an option and drops any other, as it drops an element field a type does not name.

answer_options_from is the other way to give a question its options: a path under context, written as a prefill is - "context.branches" - naming a list the host supplies when the screen is resolved. A question carries answer_options or answer_options_from, exactly one: both is document.conflicting_options, and neither is document.missing_field on answer_options. A path of another form is document.invalid_context_path. The options are the value at the path where it is a list of objects each carrying a "value" in the key shape and a "label", a string, no value twice; the resolved question carries them as answer_options, each its value and its label as the host wrote them - the labels are not templates, and a "description" the host's list carries is not read. A path that reaches no such list resolves the question with no options and is reported in missing_variables. A response is checked against the options the context holds when the screen is validated, not when it was shown. The checks of authored options do not apply to such a question, and its selection bounds are not compared with a number of options at admit, there being none.

default is the option the question presents as chosen when nothing else yields a value - one option's value under "one", a list of different option values under "many", each one this question's own options carry, so a question reading its options from context takes no default - and prefill a path under context whose value it presents first, where that value is one the default could be.

messages maps a code response validation raises on this question - response.required, response.invalid_shape, response.not_an_option or response.out_of_range - to the plain string its author shows for it, which a finding with that code carries as author_message.

style is a hint a renderer reads - radio, dropdown or chips for one, checkboxes, list or chips for many - from an open set the renderer owns; what this type checks is that it is a string. searchable is a hint too, a boolean, false when it is absent: true asks a renderer to let the visitor filter the options. It changes nothing a response is checked against, and one that is not a boolean is document.invalid_hint. required defaults to false, and true means the visitor picks at least one option. min_selections and max_selections bound how many options a visitor picks, and they are read only under select: "many". Each is a non-negative integer, a number with no fractional part counting as one - a bound written 2.0 is admitted as 2. format is carried and never consulted: it is a field nothing reads, whatever it names.

iex> node = %{type: "choice_question", key: "card_type", label: "Which library card would you like?", select: "one", answer_options: [%{value: "adult", label: "Adult card"}, %{value: "child", label: "Child card"}]}
iex> Riddler.Screens.Type.ChoiceQuestion.validate(node)
[]

iex> node = %{type: "choice_question", key: "pickup_branch", label: "Where will you collect your card?", select: "one", answer_options: [%{value: "riverside", label: "Riverside"}, %{value: "riverside", label: "Riverside (east entrance)"}]}
iex> [finding] = Riddler.Screens.Type.ChoiceQuestion.validate(node)
iex> {finding.code, finding.element_key}
{"document.duplicate_option_value", "pickup_branch"}