@bodheesh/create-bodhi-node-app
Version:
Create a production-ready Node.js REST API with zero configuration
46 lines (38 loc) • 1.21 kB
JavaScript
const fs = require('fs').promises;
async function validateSchema(schemaPath) {
try {
let schema;
if (typeof schemaPath === 'string') {
const content = await fs.readFile(schemaPath, 'utf8');
schema = JSON.parse(content);
} else {
schema = schemaPath;
}
// Validate required fields
if (!schema.name) {
throw new Error('Schema must have a name property');
}
if (!schema.fields || typeof schema.fields !== 'object') {
throw new Error('Schema must have a fields object');
}
// Validate field types
for (const [fieldName, field] of Object.entries(schema.fields)) {
if (!field.type) {
throw new Error(`Field ${fieldName} must have a type`);
}
const validTypes = ['String', 'Number', 'Boolean', 'Date', 'ObjectId', 'Array', 'Object'];
if (!validTypes.includes(field.type)) {
throw new Error(`Field ${fieldName} has invalid type. Valid types are: ${validTypes.join(', ')}`);
}
}
return schema;
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(`Schema file not found: ${schemaPath}`);
}
throw error;
}
}
module.exports = {
validateSchema
};