PC02-L04 · guided challenge

Notification channel

Select a preferred channel, a safe fallback, or none.

Practical CEL4 test casesDocumented
What will I practise?

You can combine presence checks with a multi-level result selection.

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

Use a preferred notification channel when it is email or sms. Otherwise use email when a customer email exists and is non-empty. If neither path works, return none.

Exact requirements

  1. An unsupported preference is not returned.
  2. Missing preference does not cause an error.
  3. Email is a fallback only when usable.
  4. The result is always a string.

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: Try a valid preference first. If it does not qualify, check the fallback email.

Tip: press Ctrl/⌘ + Enter to run all tests.
Valid preferenceNot run yet
Expected: "sms" · string
Show JSON input
{
  "settings": {
    "preferredChannel": "sms"
  },
  "customer": {}
}
Email fallbackNot run yet
Expected: "email" · string
Show JSON input
{
  "settings": {
    "preferredChannel": "push"
  },
  "customer": {
    "email": "a@example.test"
  }
}
Missing preferenceNot run yet
Expected: "email" · string
Show JSON input
{
  "settings": {},
  "customer": {
    "email": "a@example.test"
  }
}
No usable channelNot run yet
Expected: "none" · string
Show JSON input
{
  "settings": {},
  "customer": {}
}
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(settings.preferredChannel) && settings.preferredChannel in ['email', 'sms'] ? settings.preferredChannel : has(customer.email) && customer.email != '' ? 'email' : 'none'

Why the solution works

  1. The first condition validates both presence and allowed preference values.
  2. Its true branch returns the preference.
  3. The false branch contains a second conditional for the email fallback.
  4. The last branch supplies the explicit none result.

What the test cases prove

  • Valid preference: expected "sms" (string).
  • Email fallback: expected "email" (string).
  • Missing preference: expected "email" (string).
  • No usable channel: expected "none" (string).

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 presence checks with a multi-level result selection.

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.