PC03-L03 · guided challenge

No invalid items

Build an error list with filter() and verify that it is empty.

Practical CEL4 test casesDocumented
What will I practise?

You can use filter() as a diagnostic intermediate step and size() for the final decision.

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

An item is invalid when its SKU is empty or its quantity is zero or negative. The complete list is valid when filtering invalid items produces an empty list.

Exact requirements

  1. The filter predicate describes invalidity.
  2. Either invalid property is enough to select an item.
  3. The filtered list must have size 0.

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 filtered list must contain invalid items, not valid ones.

Tip: press Ctrl/⌘ + Enter to run all tests.
All items validNot run yet
Expected: true · bool
Show JSON input
{
  "items": [
    {
      "sku": "A-1",
      "quantity": 2
    },
    {
      "sku": "B-2",
      "quantity": 1
    }
  ]
}
Empty SKUNot run yet
Expected: false · bool
Show JSON input
{
  "items": [
    {
      "sku": "",
      "quantity": 2
    }
  ]
}
Zero quantityNot run yet
Expected: false · bool
Show JSON input
{
  "items": [
    {
      "sku": "A-1",
      "quantity": 0
    }
  ]
}
Empty listNot run yet
Expected: true · bool
Show JSON input
{
  "items": []
}
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.filter(item, item.sku == '' || item.quantity <= 0.0).size() == 0

Why the solution works

  1. filter() keeps items whose invalidity predicate is true.
  2. || selects an item when either defect occurs.
  3. size() counts the resulting error list.
  4. Comparing the count with zero turns it into the final boolean decision.

What the test cases prove

  • All items valid: expected true (bool).
  • Empty SKU: expected false (bool).
  • Zero quantity: expected false (bool).
  • Empty list: expected true (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 use filter() as a diagnostic intermediate step and size() for the final decision.

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.