CH05-L02 · applied

The exists_one() macro

Verify that exactly one list item satisfies a condition.

General CELRuns hereDocumented
What will I learn?

You can distinguish exactly one match from one or more matches.

Where will I try it?

Directly below, in the browser playground.

What do I do?

Run both modes, compare where their values come from, then complete the task.

Try CEL · browser pilot

Try both ways to supply values

Define local values inside one CEL expression, or provide variables as JSON input. Everything runs in your browser.

CEL pilot subset
One self-contained CEL expressioncel.bind() gives a local name to a value and returns the result of the final expression.

cel.bind()cel.bind() is an optional official extension. It is not part of the core CEL language and may not be enabled by every host application.

Tip: press Ctrl/⌘ + Enter to run.
Result

Press Run CEL to evaluate the active example.

Pilot scope: literals, explicit conversions, local cel.bind() variables, JSON variables, lists, maps, presence, collection macros, core string checks, time values, arithmetic, comparisons, logic, and the conditional operator.

What you will do

You will run the prepared expression in the playground above, change one input at a time, and explain the result. Start in CEL only, then repeat the same rule in CEL + JSON.

How it works

exists_one() counts how many items make its predicate true and succeeds only when the count is exactly one. It is stricter than exists(): zero matches and two matches both return false. The predicate here reads the boolean active member of each map.

Read the prepared example

The CEL + JSON rule is:

approvers.exists_one(person, person.active)

Its input is:

{
  "approvers": [
    { "name": "A", "active": false },
    { "name": "B", "active": true }
  ]
}

The CEL only tab contains the values and the rule in one expression. The enabled cel.bind() extension keeps each name local to its final argument.

Evaluation step by step

  1. Person A has active: false.
  2. Person B has active: true.
  3. Exactly one predicate result is true, so exists_one() returns true.

Your task

  1. Run the example.
  2. Set both people to inactive.
  3. Then set both people to active and compare both false results.

Expected observation: Zero active people and two active people both fail the exactly-one requirement.

Common mistake

Do not use exists_one() when the rule actually means “at least one.” The macro encodes cardinality, not just presence.

Show the explanation

exists_one() returns false; exists() returns true.

Knowledge check

How is exists_one() different from exists() when two items match?

Key takeaway

Use exists_one() only when uniqueness is part of the rule.

Sources

  • CEL-DEV — official CEL overview.
  • CEL-LANG — official CEL language definition.
  • CEL-GO-BIND — official documentation for the optional cel.bind() extension used by the self-contained tab.