This Function Node Saves Every Document Workflow From Data Validation Errors 🔥
Built 20+ document workflows. Every one needs validation. Dates in future? Negative amounts? Missing fields?
Wrote same validation 20 times. Client changed rules. Had to update 20 workflows.
Built reusable validation function node. Logic lives in one place. Update once, applies everywhere.
THE FUNCTION:
function validateDocumentData(data) {
const errors = [];
if (data.invoice_date > new Date()) {
errors.push("Invoice date cannot be in future");
}
if (data.total_amount <= 0) {
errors.push("Total must be positive");
}
const required = ['vendor_name', 'invoice_number'];
required.forEach(field => {
if (!data[field]) errors.push(`Missing: ${field}`);
});
if (data.total_amount > 10000 && !data.approval_code) {
errors.push("Over $10K needs approval");
}
return {
isValid: errors.length === 0,
errors: errors,
data: data
};
}
Catches data quality issues before destination systems.
THE VALIDATION CATEGORIES:
Format: Dates formatted correctly, numbers are numbers
Range: Amounts positive, dates reasonable
Business rules: Purchase limits, approvals, vendor whitelist
Completeness: Required fields present
Logic: Math adds up, percentages sum to 100
REAL CATCHES:
Invoice dated 2026 (typo) - Caught
Negative amount -$1,500 - Caught
Missing vendor name - Caught
$85,000 without approval - Routed to approval
Duplicate invoice number - Prevented duplicate
Would have created reconciliation nightmares. Function caught all.
THE ERROR HANDLING:
Validation fails returns:
- isValid: false
- errors: Array of specific problems
- data: Original data
Downstream IF checks isValid. True → continue. False → route to fix.
THE CENTRALIZED BENEFIT:
Client changes rule: "$5K needs approval" (was $10K).
Without centralized: Update 20 workflows manually
With centralized: Update function once, 30 seconds
VALIDATION METRICS:
Monthly validations: ~6,000
Errors caught: ~180 (3%)
3% sounds low. But 180 documents monthly would have caused downstream problems.
Template n8n with validation here
THE LESSON:
Data validation isn't optional. Bad data creates expensive problems.
Build reusable validation functions. Don't copy logic between workflows.
What validation errors are slipping through your workflows?
7
10 comments
Duy Bui
7
This Function Node Saves Every Document Workflow From Data Validation Errors 🔥
AI Automation Society
skool.com/ai-automation-society
A community built to master no-code AI automations. Join to learn, discuss, and build the systems that will shape the future of work.
Leaderboard (30-day)
Powered by