UNPKG

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

155 lines (154 loc) 5.93 kB
import { SourceFile, TypeAliasDeclaration, InterfaceDeclaration } from "ts-morph"; import { FileDiscoveryOptions } from "./file-discovery"; import { ParsedSchema } from "./type-parser"; import { GeneratedZodSchema } from "./zod-schema-generator"; import { SchemaOutputOptions } from "./schema-file-writer"; export interface SchemaExtractionOptions extends FileDiscoveryOptions { compilerOptions?: any; includeSourceInfo?: boolean; output?: SchemaOutputOptions; } export interface ExtractedSchema { typeName: string; schema: ParsedSchema; sourceInfo?: { filePath: string; line: number; }; } export interface SchemaExtractionResult { schemas: ExtractedSchema[]; totalTypesFound: number; typesWithSupportsRuntimeValidation: number; processingTime: number; } export interface TypeDeclarationInfo { name: string; declaration: TypeAliasDeclaration | InterfaceDeclaration; sourceFile: SourceFile; extendsSupportsRuntimeValidation: boolean; } /** * SchemaExtractor - Integrated pipeline for extracting constraint schemas * from types that extend SupportsRuntimeValidation */ export declare class SchemaExtractor { private project; private sourceFiles; private typeResolver; private typeParser; private constructor(); /** * Main extraction method - extracts schemas from types that extend SupportsRuntimeValidation */ static extract(input: string, options?: SchemaExtractionOptions): Promise<SchemaExtractionResult>; /** * Extract only the schemas without metadata */ static extractSchemas(input: string, options?: SchemaExtractionOptions): Promise<ExtractedSchema[]>; /** * Extract schema for a single specific type */ static extractSingleSchema(input: string, typeName: string, options?: SchemaExtractionOptions): Promise<ExtractedSchema | null>; /** * Get all types that extend SupportsRuntimeValidation without extracting schemas */ static getValidationTypes(input: string, options?: SchemaExtractionOptions): Promise<string[]>; /** * Extract and generate Zod schemas from types that extend SupportsRuntimeValidation */ static extractZodSchemas(input: string, options?: SchemaExtractionOptions): Promise<GeneratedZodSchema[]>; /** * Extract and generate a single Zod schema for a specific type */ static extractSingleZodSchema(input: string, typeName: string, options?: SchemaExtractionOptions): Promise<GeneratedZodSchema | null>; /** * Extract Zod schemas and write them to disk */ static extractAndWriteZodSchemas(input: string, options?: SchemaExtractionOptions): Promise<{ schemas: GeneratedZodSchema[]; writtenFiles: string[]; }>; /** * Create an extractor instance from input */ private static createExtractor; /** * Discover all type declarations across all source files */ private discoverAllTypes; /** * Extract schemas from a list of type declarations */ private extractSchemasFromTypes; /** * Create combined source code from all source files for the type parser */ private createCombinedSource; /** * Get the constraint types and imports needed for parsing * Dynamically loads from constraint-types.ts file */ private getImportsAndConstraintTypes; /** * Get fallback constraint types when the file cannot be loaded */ private getFallbackConstraintTypes; /** * Get statistics about the types in the project */ getTypeStatistics(): Promise<{ totalTypes: number; typesWithSupportsRuntimeValidation: number; typesByFile: Record<string, number>; validationTypesByFile: Record<string, number>; }>; } /** * Extract schemas from source code string */ export declare function extractSchemasFromSource(sourceCode: string, options?: Omit<SchemaExtractionOptions, keyof FileDiscoveryOptions>): Promise<ExtractedSchema[]>; /** * Extract schemas from a file */ export declare function extractSchemasFromFile(filePath: string, options?: SchemaExtractionOptions): Promise<ExtractedSchema[]>; /** * Extract schemas from a directory */ export declare function extractSchemasFromDirectory(dirPath: string, options?: SchemaExtractionOptions): Promise<ExtractedSchema[]>; /** * Extract schemas using glob patterns */ export declare function extractSchemasFromGlob(dirPath: string, glob: string | string[], exclude?: string | string[], options?: Omit<SchemaExtractionOptions, "glob" | "exclude">): Promise<ExtractedSchema[]>; /** * Get full extraction results with metadata */ export declare function extractWithMetadata(input: string, options?: SchemaExtractionOptions): Promise<SchemaExtractionResult>; /** * Extract and write Zod schemas from source code string */ export declare function extractAndWriteZodSchemasFromSource(sourceCode: string, options?: Omit<SchemaExtractionOptions, keyof FileDiscoveryOptions>): Promise<{ schemas: GeneratedZodSchema[]; writtenFiles: string[]; }>; /** * Extract and write Zod schemas from a file */ export declare function extractAndWriteZodSchemasFromFile(filePath: string, options?: SchemaExtractionOptions): Promise<{ schemas: GeneratedZodSchema[]; writtenFiles: string[]; }>; /** * Extract and write Zod schemas from a directory */ export declare function extractAndWriteZodSchemasFromDirectory(dirPath: string, options?: SchemaExtractionOptions): Promise<{ schemas: GeneratedZodSchema[]; writtenFiles: string[]; }>; /** * Extract and write Zod schemas using glob patterns */ export declare function extractAndWriteZodSchemasFromGlob(dirPath: string, glob: string | string[], exclude?: string | string[], options?: Omit<SchemaExtractionOptions, "glob" | "exclude">): Promise<{ schemas: GeneratedZodSchema[]; writtenFiles: string[]; }>;