Step 2: Route Validation Failures to a DLQ
In the previous step, invalid messages were simply rejected. In a real-world system, you want to capture these malformed messages for debugging. This is called a Dead Letter Queue (DLQ).
This step teaches you how to catch validation failures and route them to a separate destination.
The Goal
You will modify your pipeline to:
- Catch validation errors instead of rejecting the message.
- Add a
validation_statusfield to each message. - Use a
switchoutput to sendpassedmessages tostdoutandfailedmessages to a DLQ file.
Implementation
-
Start with the Previous Pipeline: Copy the
schema-validator.yamlfile from Step 1 to a new file nameddlq-router.yaml.cp schema-validator.yaml dlq-router.yaml -
Catch Validation Failures: Open
dlq-router.yamland modify thepipelinesection. You will wrap thejson_schemaprocessor in atry/catchblock.
pipeline: processors:
Use a try/catch block to prevent the pipeline from failing
-
try:
This is the same processor from Step 1
- json_schema: schema_path: "file://./sensor-schema.json"
If it succeeds, we add a 'passed' status
- mapping: | root = this meta validation_status = "passed"
If the json_schema processor fails, this block is executed
catch:
- mapping: |
root = this
meta validation_status = "failed"
Add the error message for debugging
root.validation_error = error()
3. **Add the Routing Output:** Now, replace the entire `output` section with the `switch` block below. This will route the message based on the `validation_status` set in the previous step.
```yaml title="Replace the 'output' section in dlq-router.yaml"
output:
switch:
cases:
# If validation passed, send to stdout
- check: 'meta("validation_status") == "passed"'
output:
stdout:
codec: lines
# Otherwise, send to a DLQ file
- output:
file:
path: ./validation_dlq.jsonl
codec: lines
-
Deploy and Test:
# --- Test 1: Send VALID data ---
curl -X POST http://localhost:8080/sensor/readings \
-H "Content-Type: application/json" \
-d '{"sensor_id": "sensor-42", "timestamp": "2025-10-20T14:30:00Z", "reading": 23.5}'
# --- Test 2: Send INVALID data ---
curl -X POST http://localhost:8080/sensor/readings \
-H "Content-Type: application/json" \
-d '{"id": "sensor-42", "reading": "hot"}' -
Verify:
- The valid message will be printed to your console (
stdout). - The invalid message will not be rejected. Instead, it will be written to the
validation_dlq.jsonlfile, along with avalidation_errorfield that explains why it failed.
- The valid message will be printed to your console (
You have now built a resilient validation pipeline that separates good data from bad data without losing any messages.