PC03-L01 · guided challenge

Valid and high-value items

Validate every item while finding at least one high-value item.

Practical CEL4 test casesDocumented
What will I practise?

You can combine all() and exists() over one list with different predicates.

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

Every line item must have a positive quantity and a non-negative price. At least one item must also have a calculated value of 500 or more.

Exact requirements

  1. Validate quantity and price for every item.
  2. Calculate value as quantity multiplied by price.
  3. Find at least one calculated value at or above 500.

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: The first macro checks every record; the second looks for at least one calculated value.

Tip: press Ctrl/⌘ + Enter to run all tests.
Valid with high-value itemNot run yet
Expected: true · bool
Show JSON input
{
  "items": [
    {
      "quantity": 2,
      "price": 40
    },
    {
      "quantity": 5,
      "price": 120
    }
  ]
}
No high-value itemNot run yet
Expected: false · bool
Show JSON input
{
  "items": [
    {
      "quantity": 2,
      "price": 40
    },
    {
      "quantity": 3,
      "price": 100
    }
  ]
}
Invalid quantityNot run yet
Expected: false · bool
Show JSON input
{
  "items": [
    {
      "quantity": 0,
      "price": 600
    }
  ]
}
Negative priceNot run yet
Expected: false · bool
Show JSON input
{
  "items": [
    {
      "quantity": 2,
      "price": -1
    },
    {
      "quantity": 1,
      "price": 600
    }
  ]
}
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
items.all(item, item.quantity > 0.0 && item.price >= 0.0) && items.exists(item, item.quantity * item.price >= 500.0)

Why the solution works

  1. all() applies the two validity checks to every item.
  2. The predicate uses && because both item properties are mandatory.
  3. exists() performs the per-item multiplication only for the high-value question.
  4. Both macro results must be true.

What the test cases prove

  • Valid with high-value item: expected true (bool).
  • No high-value item: expected false (bool).
  • Invalid quantity: expected false (bool).
  • Negative price: 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 combine all() and exists() over one list with different predicates.

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.