# Conditional questions

> Add when to a Curva question and it is only asked when the state matches. Skipped questions are never sent to the model, stored or paid for.

Source: https://docs.tarkova.com/curva/guides/conditional/

Add `when` to a question and it is asked only when the state matches. Questions that don't apply
are skipped: never sent to the model, stored or paid for.

```json
{
  "state": {"department": "billing", "ticket": "I was charged twice"},
  "questions": {
    "refund": {"type": "noul", "instructions": "The customer asks for a refund",
               "when": {"department": "billing"}},
    "bug_area": {"type": "choice", "instructions": "Which part of the product is broken?",
                 "options": {"app": "", "api": "", "website": ""},
                 "when": {"department": ["technical", "security"]}}
  }
}
```

## How `when` matches

`when` is an object of `{"<state field>": value}`:

- The state must be a JSON object (otherwise the request gets 422), and the named fields are its
  **top-level** fields.
- A value must be equal to the field's value; a **list** of values matches any of them.
- An **operator object** works as in [rules](/curva/guides/rules/): `{"exists": true}`, `{"gte": 3}`,
  `{"contains": "refund"}`, `starts_with`, `gt`, `lt`, `lte`. An unknown operator gets 422.
- With several fields, **every** one must match.
- Any question type can have `when`.

A skipped question keeps its place in `answers` as:

```json
"bug_area": {"skipped": true}
```

If every question is skipped, no model is called at all. A skipped question has no answer to
correct, so feedback for it gets 404.

## From Python

```python
from curva import Curva, Noul

d = Curva().decide(
    {"department": "billing", "amount": 250, "ticket": "I was charged twice"},
    {
        "refund": Noul("The customer asks for a refund").when(department="billing"),
        "review": Noul("A person should check this refund").when(amount={"gte": 100}),
    },
)
```

A question given as a dict takes the same `"when": {...}` object.

## Decision trees

A field `@<key>` in `when` reads the answer to question `<key>` of the same request instead of
the state, so a follow-up is asked only when an earlier answer calls for it, in one call:

```json
"bug_area": {"type": "choice", "instructions": "Which part is broken?",
             "options": {"app": "", "api": ""}, "when": {"@department": "technical"}}
```

The question then waits for `department` (see [Workflows in one call](/curva/guides/dependencies/)).

Calibration is keyed by the question without its `when`, so adding or changing a condition
doesn't reset a question's calibration.
