PC04-L01 · guided challenge

Optional discount code

Allow a missing code, but validate the exact format of a present code.

Practical CEL4 test casesDocumented
What will I practise?

You can express “optional, but valid when present.”

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

A discount code is optional. When present, it must be non-null and contain 4 to 10 uppercase ASCII letters or digits.

Exact requirements

  1. A missing code is valid.
  2. A present null is invalid.
  3. Lowercase or punctuation is invalid.
  4. The regular expression checks the complete value.

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 side of || accepts a missing member; the second validates a present value.

Tip: press Ctrl/⌘ + Enter to run all tests.
Code missingNot run yet
Expected: true · bool
Show JSON input
{
  "order": {}
}
Valid codeNot run yet
Expected: true · bool
Show JSON input
{
  "order": {
    "discountCode": "SAVE20"
  }
}
Lowercase lettersNot run yet
Expected: false · bool
Show JSON input
{
  "order": {
    "discountCode": "save20"
  }
}
Null valueNot run yet
Expected: false · bool
Show JSON input
{
  "order": {
    "discountCode": null
  }
}
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
!has(order.discountCode) || (order.discountCode != null && order.discountCode.matches('^[A-Z0-9]{4,10}$'))

Why the solution works

  1. !has(...) handles the valid missing case.
  2. Because of ||, validation runs only for a present member.
  3. The null check protects matches().
  4. Anchors and {4,10} enforce the complete format and length.

What the test cases prove

  • Code missing: expected true (bool).
  • Valid code: expected true (bool).
  • Lowercase letters: expected false (bool).
  • Null value: 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 express “optional, but valid when present.”

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.