typescript-runtime-schemas
Version:
A TypeScript schema generation tool that extracts Zod schemas from TypeScript source files with runtime validation support. Generate validation schemas directly from your existing TypeScript types with support for computed types and constraint-based valid
102 lines • 3.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatOutput = formatOutput;
/**
* Format output based on the specified format
*/
function formatOutput(schemas, format) {
switch (format.toLowerCase()) {
case 'json':
return formatAsJson(schemas);
case 'yaml':
return formatAsYaml(schemas);
case 'table':
return formatAsTable(schemas);
default:
throw new Error(`Unsupported format: ${format}`);
}
}
/**
* Format schemas as JSON
*/
function formatAsJson(schemas) {
return JSON.stringify(schemas, null, 2);
}
/**
* Format schemas as YAML
*/
function formatAsYaml(schemas) {
// Simple YAML formatting - could be enhanced with a proper YAML library
const lines = [];
schemas.forEach((schema, index) => {
if (index > 0)
lines.push('');
lines.push(`- typeName: ${schema.typeName}`);
if (schema.sourceInfo) {
lines.push(` sourceInfo:`);
lines.push(` filePath: ${schema.sourceInfo.filePath}`);
lines.push(` line: ${schema.sourceInfo.line}`);
}
lines.push(` schema:`);
Object.entries(schema.schema).forEach(([propName, propSchema]) => {
lines.push(` ${propName}:`);
lines.push(` type: ${propSchema.type}`);
lines.push(` required: ${propSchema.required}`);
if (Object.keys(propSchema.constraints).length > 0) {
lines.push(` constraints:`);
Object.entries(propSchema.constraints).forEach(([key, value]) => {
lines.push(` ${key}: ${JSON.stringify(value)}`);
});
}
});
});
return lines.join('\n');
}
/**
* Format schemas as a table
*/
function formatAsTable(schemas) {
if (schemas.length === 0) {
return 'No schemas found.';
}
const lines = [];
schemas.forEach((schema, index) => {
if (index > 0) {
lines.push('');
lines.push('─'.repeat(80));
lines.push('');
}
// Header
lines.push(`📋 Type: ${schema.typeName}`);
if (schema.sourceInfo) {
lines.push(`📁 Source: ${schema.sourceInfo.filePath}:${schema.sourceInfo.line}`);
}
lines.push('');
// Properties table
const properties = Object.entries(schema.schema);
if (properties.length === 0) {
lines.push(' (No properties)');
}
else {
// Calculate column widths
const nameWidth = Math.max(8, ...properties.map(([name]) => name.length));
const typeWidth = Math.max(4, ...properties.map(([, prop]) => prop.type.length));
// Header row
const headerRow = ` ${'Property'.padEnd(nameWidth)} | ${'Type'.padEnd(typeWidth)} | Required | Constraints`;
lines.push(headerRow);
lines.push(` ${'-'.repeat(nameWidth)}-+-${'-'.repeat(typeWidth)}-+----------+------------`);
// Property rows
properties.forEach(([propName, propSchema]) => {
const constraints = Object.keys(propSchema.constraints).length > 0
? Object.entries(propSchema.constraints)
.map(([k, v]) => `${k}:${v}`)
.join(', ')
: '-';
const row = ` ${propName.padEnd(nameWidth)} | ${propSchema.type.padEnd(typeWidth)} | ${propSchema.required ? 'Yes ' : 'No '} | ${constraints}`;
lines.push(row);
});
}
});
return lines.join('\n');
}
//# sourceMappingURL=formatters.js.map