PC03-L02 · guided challenge

One primary approver

Verify that exactly one active primary approver exists.

Practical CEL4 test casesDocumented
What will I practise?

You can put a compound predicate inside exists_one() and test zero, one, and multiple matches.

How is it checked?

Your one expression must produce the expected result for all four JSON inputs.

When am I done?

When all tests pass and you can explain every condition in your expression.

Word problem

The approver list is valid only when exactly one person is both primary and active.

Exact requirements

  1. A primary but inactive person does not count.
  2. An active but non-primary person does not count.
  3. Zero or multiple matching people return false.

Required result

Write one CEL expression. It must return the expected value and type for all four prepared JSON inputs. Do not change the inputs to make an incomplete expression pass.

Recommended procedure

  1. Translate each numbered requirement into one small boolean condition.
  2. Run the starter expression and inspect which cases fail.
  3. Add one condition at a time and run the complete suite again.
  4. Mark the challenge complete only when you can explain every operator and every boundary.

Solve the challenge · browser test suite

Write one expression that passes every case

Edit the CEL expression, then run it against all prepared JSON inputs. A solution is complete only when every case passes.

4 test cases

Hint: A match must satisfy both primary and active.

Tip: press Ctrl/⌘ + Enter to run all tests.
One valid matchNot run yet
Expected: true · bool
Show JSON input
{
  "approvers": [
    {
      "primary": true,
      "active": true
    },
    {
      "primary": false,
      "active": true
    }
  ]
}
Primary approver inactiveNot run yet
Expected: false · bool
Show JSON input
{
  "approvers": [
    {
      "primary": true,
      "active": false
    },
    {
      "primary": false,
      "active": true
    }
  ]
}
Two active primary approversNot run yet
Expected: false · bool
Show JSON input
{
  "approvers": [
    {
      "primary": true,
      "active": true
    },
    {
      "primary": true,
      "active": true
    }
  ]
}
No primary approverNot run yet
Expected: false · bool
Show JSON input
{
  "approvers": [
    {
      "primary": false,
      "active": true
    }
  ]
}
Test result

Not run yet

The same expression runs against every case. Test data is fixed so that boundary and failure scenarios cannot be skipped.

Explained solution

Show the complete expression
approvers.exists_one(person, person.primary && person.active)

Why the solution works

  1. The predicate combines primary and active with &&.
  2. exists_one() evaluates that complete predicate for every person.
  3. It counts true predicate results.
  4. Only a count of one produces true.

What the test cases prove

  • One valid match: expected true (bool).
  • Primary approver inactive: expected false (bool).
  • Two active primary approvers: expected false (bool).
  • No primary approver: expected false (bool).

The cases are intentionally different. A solution that passes only the first case has not yet implemented the complete rule.

Common mistake

Do not optimise the expression for the first successful input. A requirement is implemented only when its positive case, negative case, and boundary case all produce the stated result.

Knowledge check

Can you point to the exact part of the solution that makes each false test case fail? If two different failures depend on the same condition, explain why.

Key takeaway

You can put a compound predicate inside exists_one() and test zero, one, and multiple matches.

Sources

  • CEL-DEV — official CEL overview.
  • CEL-LANG — official CEL language definition for operators, types, macros, presence, strings, and time values used in this challenge.