Files
n8n-mcp/scripts/test-jmespath-validation.ts
czlonkowski 99e74cf22a fix: correct misleading Code node documentation based on real-world testing
Critical fixes based on Claude Desktop feedback:

1. Fixed crypto documentation: require('crypto') IS available despite editor warnings
   - Added clear examples of crypto usage
   - Updated validation to guide correct require() usage

2. Clarified $helpers vs standalone functions
   - $getWorkflowStaticData() is standalone, NOT $helpers.getWorkflowStaticData()
   - Added validation to catch incorrect usage (prevents '$helpers is not defined' errors)
   - Enhanced examples showing proper $helpers availability checks

3. Fixed JMESPath numeric literal documentation
   - n8n requires backticks around numbers in filters: [?age >= `18`]
   - Added multiple examples and validation to detect missing backticks
   - Prevents 'JMESPath syntax error' that Claude Desktop encountered

4. Fixed webhook data access gotcha
   - Webhook payload is at items[0].json.body, NOT items[0].json
   - Added dedicated 'Webhook Data Access' section with clear examples
   - Created process_webhook_data task template
   - Added validation to detect incorrect webhook data access patterns

All fixes based on production workflows TaNqYoZNNeHC4Hne and JZ9urD7PNClDZ1bm

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-10 09:22:34 +02:00

114 lines
3.5 KiB
TypeScript

#!/usr/bin/env npx tsx
import { EnhancedConfigValidator } from '../src/services/enhanced-config-validator.js';
console.log('🧪 Testing JMESPath Validation\n');
const testCases = [
{
name: 'JMESPath with unquoted numeric literal',
config: {
language: 'javaScript',
jsCode: `const data = { users: [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }] };
const adults = $jmespath(data, 'users[?age >= 18]');
return [{json: {adults}}];`
},
expectError: true
},
{
name: 'JMESPath with properly quoted numeric literal',
config: {
language: 'javaScript',
jsCode: `const data = { users: [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }] };
const adults = $jmespath(data, 'users[?age >= \`18\`]');
return [{json: {adults}}];`
},
expectError: false
},
{
name: 'Multiple JMESPath filters with unquoted numbers',
config: {
language: 'javaScript',
jsCode: `const products = items.map(item => item.json);
const expensive = $jmespath(products, '[?price > 100]');
const lowStock = $jmespath(products, '[?quantity < 10]');
const highPriority = $jmespath(products, '[?priority == 1]');
return [{json: {expensive, lowStock, highPriority}}];`
},
expectError: true
},
{
name: 'JMESPath with string comparison (no backticks needed)',
config: {
language: 'javaScript',
jsCode: `const data = { users: [{ name: 'John', status: 'active' }, { name: 'Jane', status: 'inactive' }] };
const activeUsers = $jmespath(data, 'users[?status == "active"]');
return [{json: {activeUsers}}];`
},
expectError: false
},
{
name: 'Python JMESPath with unquoted numeric literal',
config: {
language: 'python',
pythonCode: `data = { 'users': [{ 'name': 'John', 'age': 30 }, { 'name': 'Jane', 'age': 25 }] }
adults = _jmespath(data, 'users[?age >= 18]')
return [{'json': {'adults': adults}}]`
},
expectError: true
},
{
name: 'Complex filter with decimal numbers',
config: {
language: 'javaScript',
jsCode: `const items = [{ price: 99.99 }, { price: 150.50 }, { price: 200 }];
const expensive = $jmespath(items, '[?price >= 99.95]');
return [{json: {expensive}}];`
},
expectError: true
}
];
let passCount = 0;
let failCount = 0;
for (const test of testCases) {
console.log(`Test: ${test.name}`);
const result = EnhancedConfigValidator.validateWithMode(
'nodes-base.code',
test.config,
[
{ name: 'language', type: 'options', options: ['javaScript', 'python'] },
{ name: 'jsCode', type: 'string' },
{ name: 'pythonCode', type: 'string' }
],
'operation',
'strict'
);
const hasJMESPathError = result.errors.some(e =>
e.message.includes('JMESPath numeric literal') ||
e.message.includes('must be wrapped in backticks')
);
const passed = hasJMESPathError === test.expectError;
console.log(` Expected error: ${test.expectError}`);
console.log(` Has JMESPath error: ${hasJMESPathError}`);
console.log(` Result: ${passed ? '✅ PASS' : '❌ FAIL'}`);
if (result.errors.length > 0) {
console.log(` Errors: ${result.errors.map(e => e.message).join(', ')}`);
}
if (result.warnings.length > 0) {
console.log(` Warnings: ${result.warnings.slice(0, 2).map(w => w.message).join(', ')}`);
}
if (passed) passCount++;
else failCount++;
console.log();
}
console.log(`\n📊 Results: ${passCount} passed, ${failCount} failed`);
console.log(failCount === 0 ? '✅ All JMESPath validation tests passed!' : '❌ Some tests failed');