PC06-L01 · independent challenge

Complete order approval

Combine state, items, limits, and one unique approver.

Practical CEL4 test casesDocumented
What will I practise?

You can design a longer expression as a sequence of independently testable conditions.

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

Approve an order only when it is active and unblocked, has valid non-empty items, no calculated line total exceeds itemLimit, and exactly one active primary approver exists.

Exact requirements

  1. Validate order state before collections.
  2. Require present, non-empty items.
  3. Validate positive quantity and unit price for every item.
  4. Check every calculated line total against the limit.
  5. Require one active primary approver.

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: Work in this order: state, presence and size, item validity, per-item limit, approver.

Tip: press Ctrl/⌘ + Enter to run all tests.
Order meets every ruleNot run yet
Expected: true · bool
Show JSON input
{
  "order": {
    "active": true,
    "blocked": false,
    "itemLimit": 500,
    "items": [
      {
        "quantity": 2,
        "unitPrice": 100
      },
      {
        "quantity": 1,
        "unitPrice": 450
      }
    ],
    "approvers": [
      {
        "primary": true,
        "active": true
      },
      {
        "primary": false,
        "active": true
      }
    ]
  }
}
Item exceeds limitNot run yet
Expected: false · bool
Show JSON input
{
  "order": {
    "active": true,
    "blocked": false,
    "itemLimit": 500,
    "items": [
      {
        "quantity": 2,
        "unitPrice": 300
      }
    ],
    "approvers": [
      {
        "primary": true,
        "active": true
      }
    ]
  }
}
Two primary approversNot run yet
Expected: false · bool
Show JSON input
{
  "order": {
    "active": true,
    "blocked": false,
    "itemLimit": 500,
    "items": [
      {
        "quantity": 1,
        "unitPrice": 100
      }
    ],
    "approvers": [
      {
        "primary": true,
        "active": true
      },
      {
        "primary": true,
        "active": true
      }
    ]
  }
}
Items missingNot run yet
Expected: false · bool
Show JSON input
{
  "order": {
    "active": true,
    "blocked": false,
    "itemLimit": 500,
    "approvers": [
      {
        "primary": true,
        "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
order.active && !order.blocked && has(order.items) && order.items.size() > 0 && order.items.all(item, item.quantity > 0.0 && item.unitPrice > 0.0) && order.items.map(item, item.quantity * item.unitPrice).all(total, total <= order.itemLimit) && order.approvers.exists_one(person, person.primary && person.active)

Why the solution works

  1. The state checks reject the order early.
  2. has() and size() protect and constrain the item list.
  3. The first all() validates raw item values.
  4. map().all() calculates and checks each line total.
  5. exists_one() enforces approver uniqueness.

What the test cases prove

  • Order meets every rule: expected true (bool).
  • Item exceeds limit: expected false (bool).
  • Two primary approvers: expected false (bool).
  • Items missing: 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 design a longer expression as a sequence of independently testable conditions.

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.