Validate Ticket Output
Exercise
Validate Ticket Output
A support tool asks a language model to classify each ticket and copy its order ID, and the model replies with JSON text. Before the application stores or routes anything, it must check that reply. Write the check.
Implement validateTicket(raw, source). raw is the model's reply as a string, and source is the original ticket text. Return:
{ ok: true, value: { category, order_id } }when the reply is accepted{ ok: false, kind, reason }for the first failing layer, wherekindis"parse","schema", or"source"andreasonis a short, non-empty message of your choice
The contract
Check the layers in this order and stop at the first failure:
| Order | Kind | Fails when |
|---|---|---|
| 1 | "parse" | raw is not valid JSON |
| 2 | "schema" | the value is not a plain object (an array or null does not count); its keys are not exactly category and order_id; category is not one of "billing", "technical", "other"; or order_id is neither null nor a string that is exactly ORD- followed by three digits |
| 3 | "source" | order_id breaks the ID policy below |
ID policy. Find the order IDs in source: ORD- followed by exactly three digits, as a whole word (so ORD-1234 contains no valid ID). Count distinct IDs:
- none:
order_idmust benull - exactly one (repeats count once):
order_idmust be that ID - more than one:
order_idmust benullandcategorymust be"other"
Fixtures
| Source | Raw output | Expected |
|---|---|---|
| Refund ORD-204. | {"category":"billing","order_id":"ORD-204"} | ok |
| App crashes. | {"category":"technical","order_id":null,} | "parse" |
| App crashes. | {"category":"urgent","order_id":null} | "schema" |
| App crashes. | {"category":"technical"} | "schema" |
| App crashes. | {"category":"technical","order_id":"ORD-204"} | "source" |
| Refund ORD-101 and ORD-202. | {"category":"billing","order_id":"ORD-101"} | "source" |
| Refund ORD-204. | {"category":"technical","order_id":"ORD-204"} | ok |
The Expected column is what the validator returns. Whether each category is the right one for its ticket is a separate question.
Notes
- An empty string is not a substitute for
null:"order_id": ""is a schema failure. - A reply wrapped in a Markdown code fence is not valid JSON.
In Python, implement validate_ticket(raw, source) and return dicts with the same keys ("ok", "value", "kind", "reason").
0 views
Solution
Hints
