validation-demo.js•4.21 kB
/**
* Simple Schema Validation Demo
* Demonstrates the validation capabilities of the MCP server
*/
import Ajv from 'ajv';
const ajv = new Ajv({ allErrors: true });
// Example schema from our MCP server
const fetchNotionScenariosSchema = {
type: 'object',
properties: {
database_id: {
type: 'string',
description: 'The Notion database ID containing test scenarios',
},
filter: {
type: 'object',
description: 'Optional filter criteria for scenarios',
properties: {
tags: {
type: 'array',
items: { type: 'string' },
description: 'Filter by specific test_id tags',
},
status: {
type: 'string',
description: 'Filter by scenario status',
},
},
},
},
required: ['database_id'],
additionalProperties: false,
};
const generateFeatureFileSchema = {
type: 'object',
properties: {
scenario_title: {
type: 'string',
description: 'Title of the test scenario',
minLength: 1,
},
gherkin_syntax: {
type: 'string',
description: 'Gherkin syntax content for the feature',
minLength: 1,
},
tags: {
type: 'array',
items: { type: 'string' },
description: 'Test ID tags for the scenario',
},
output_path: {
type: 'string',
description: 'Path where the feature file should be saved',
minLength: 1,
},
},
required: ['scenario_title', 'gherkin_syntax', 'output_path'],
additionalProperties: false,
};
function validateAndLog(schemaName, schema, testData, shouldPass = true) {
const validate = ajv.compile(schema);
const valid = validate(testData);
if (valid) {
console.log(`✅ ${shouldPass ? 'PASS' : 'UNEXPECTED PASS'}: ${schemaName}`);
if (!shouldPass) {
console.log(` Data: ${JSON.stringify(testData, null, 2)}`);
}
} else {
console.log(`${shouldPass ? '❌ UNEXPECTED FAIL' : '✅ EXPECTED FAIL'}: ${schemaName}`);
const errors = validate.errors
.map(err => `${err.instancePath || 'root'}: ${err.message}`)
.join(', ');
console.log(` Errors: ${errors}`);
console.log(` Data: ${JSON.stringify(testData, null, 2)}\n`);
}
}
console.log('🧪 MCP Server Schema Validation Demo\n');
// Test fetch_notion_scenarios
console.log('📋 Testing fetch_notion_scenarios schema:');
validateAndLog('Valid request', fetchNotionScenariosSchema, {
database_id: 'test-db-123',
filter: {
tags: ['LOGIN'],
status: 'ready'
}
});
validateAndLog('Missing required database_id', fetchNotionScenariosSchema, {
filter: { tags: ['LOGIN'] }
}, false);
validateAndLog('Extra properties not allowed', fetchNotionScenariosSchema, {
database_id: 'test-db-123',
invalid_property: 'should fail'
}, false);
console.log('\n📝 Testing generate_feature_file schema:');
validateAndLog('Valid feature generation', generateFeatureFileSchema, {
scenario_title: 'User Login',
gherkin_syntax: 'Given I am on login page\nWhen I enter credentials\nThen I should be logged in',
tags: ['@login', '@smoke'],
output_path: './test.feature'
});
validateAndLog('Empty scenario_title', generateFeatureFileSchema, {
scenario_title: '',
gherkin_syntax: 'Given I am on login page',
output_path: './test.feature'
}, false);
validateAndLog('Missing required fields', generateFeatureFileSchema, {
scenario_title: 'User Login'
// Missing gherkin_syntax and output_path
}, false);
console.log('\n🎯 Schema validation provides:');
console.log(' • Type checking (string, object, array)');
console.log(' • Required field validation');
console.log(' • String length constraints (minLength)');
console.log(' • Additional properties prevention');
console.log(' • Detailed error messages');
console.log(' • Enum validation for specific values');