Skip to main content

visibleIf

The questionnaire configuration may include a visibleIf property on questions to control conditional visibility.

What visibleIf does

  • If visibleIf is missing or null, the question is visible by default.
  • If visibleIf is present as a string, it should be treated as a JEXL expression.
    • If the expression evaluates to true, the question should be visible.
    • If it evaluates to false, the question should be hidden.

JEXL

Expressions follow the JEXL syntax.

Example expressions

The format of expressions returned from the API typically follows answer.<questionKey> <operator> <expectedValue>, like:

  • answer.diagnosis == 'true'
  • answer.bmi > 18

Example of evaluation in JS

Using @pawel-up/jexl, keeping track of the context in the application. The context should have the format:

{
"answer": {
<key1>: <value1>,
<key2>: <value2>
...
}
}
// visibleIf = "answer.diagnosis == 'true'"
// context = { answer: { diagnosis: true } }
const shouldShowQuestion = jexl.evalSync(visibleIf, context);
// evaluates to true