Workflow Contracts That Save You [FD2D #3]

Define contracts, stop fragility. Input contracts, output contracts, error contracts - validation before and after AI.

Table of Contents

Workflow contracts are a three-layer validation system for n8n automations: input contracts check data as it enters, output contracts verify AI-generated results before they publish, and error contracts route failures to Slack or email instead of letting them fail silently. Dheeraj reports this approach cut his workflow failures by about 90%.

What you will be able to do

  • Add an IF node validation gate right after a trigger to reject bad data before it reaches AI calls or API requests.
  • Write regex and expression conditions for email format, URL format, required fields, date ranges, and numeric ranges in n8n's IF node.
  • Write an AI prompt with a structure-enforcement block, then parse and validate the JSON output with a Set node and IF node for required fields, length limits, and item counts.
  • Set up a separate Error Handler workflow using n8n's built-in Error Trigger node to catch failures automatically.
  • Route validation and error failures to Slack or email notifications, including workflow name, node, and error message, instead of letting them fail silently.

Before you start

  • An existing n8n workflow (trigger plus processing steps) to add validation to.
  • A Slack or email node configured in n8n for failure notifications.
  • Basic familiarity with n8n's IF node, Set node, and expression syntax ({{ }}).
  • An AI node (such as Claude) in the workflow if building output contracts for AI-generated content.

Reference

Validation type n8n expression or step What it catches
Email format {{ $json.email.match(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/) }} Invalid email before it reaches the CRM
URL format {{ $json.url.match(/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/) }} Malformed link fields
Required field (not empty) {{ $json.field_name && $json.field_name.trim().length > 0 }} Missing or blank required data
Date is in the future {{ new Date($json.date_field) > new Date() }} Accidentally scheduling posts in the past
Number in range (10-10000) {{ $json.number_field >= 10 && $json.number_field <= 10000 }} Pricing typos like $10,000 instead of $100
String length minimum (10 chars) {{ $json.text_field && $json.text_field.length >= 10 }} Text fields that are too short to be valid
Error Trigger node First node in a separate "Error Handler" workflow Catches any workflow error automatically and reports it

Common errors and fixes

What goes wrong The fix
Workflow shows "success" but bad data (e.g. emoji in an email field) reaches the CRM, which then rejects it Add an Input Validation Gate (IF node) right after the trigger to check field format before processing continues
Claude wraps JSON in markdown code blocks or renames a field (e.g. "email_address" instead of "email"), breaking the workflow Add explicit structure rules to the AI prompt (no markdown wrapping, exact field names) and validate the parsed output with an IF node
A source row (e.g. in a spreadsheet) is deleted and the workflow runs successfully with missing data, unnoticed until someone complains Add required-field checks before processing so missing data is caught and reported instead of passed through silently
AI output technically succeeds but breaks a quality limit, like a Twitter post over 280 characters Add a second IF node after field-existence checks to validate length and item count against your limits
Failures happen with no visibility into which workflow or node broke Build a separate Error Handler workflow with an Error Trigger node that sends Slack or email notifications with workflow name, node, error message, and execution link

Read the full walkthrough

The complete lesson, with screenshots and any downloads, is published on Substack as part of From Demo to Dependable: Production n8n Workflows.

Read Lesson 3 on Substack →

More in this section

Continue the course

Browse all lessons in the From Demo to Dependable: Production n8n Workflows course, or subscribe to the GenAI Unplugged newsletter to get new lessons in your inbox.

Dheeraj Sharma

Dheeraj Sharma

AI Systems Builder
Creator of the n8n Zero to Hero course (42 lessons, 31+ hours). I help solopreneurs build AI systems that grow revenue without growing workload.

Get the n8n Mastery Bundle

All workflows, cheat sheets, and premium resources from the entire course in one package.

Read the full lesson on Substack