@msugiura/rawsql-prisma
Version:
Prisma integration for rawsql-ts - Dynamic SQL generation with type safety and hierarchical JSON serialization
85 lines (84 loc) • 2.96 kB
TypeScript
/**
* Automatic Type Compatibility Validator
*
* Uses TypeScript compiler API to automatically validate JsonMapping compatibility
* with target TypeScript interfaces. Only requires interface name and import path!
*/
import * as ts from 'typescript';
import { EnhancedJsonMapping, TypeValidationResult } from './EnhancedJsonMapping';
export interface AutoTypeValidationOptions {
/**
* Base directory for resolving relative import paths
*/
baseDir?: string;
/**
* TypeScript compiler options
*/
compilerOptions?: ts.CompilerOptions;
/**
* Enable debug logging
*/
debug?: boolean;
}
/**
* Automatic type compatibility validator that reads TypeScript interfaces
* and validates JsonMapping structure compatibility
*/
export declare class AutoTypeCompatibilityValidator {
private options;
constructor(options?: AutoTypeValidationOptions);
/**
* Validate JsonMapping compatibility with TypeScript interface
*
* @param jsonMapping - Enhanced JsonMapping with typeInfo
* @returns Validation result with detailed compatibility information
*/
validateMapping(jsonMapping: EnhancedJsonMapping): Promise<TypeValidationResult>;
/**
* Resolve interface file path relative to base directory
*/
private resolveInterfacePath;
/**
* Generate candidate paths for resolution, handling redundant directory prefixes
*
* This method addresses the issue where import paths may contain redundant directory names
* that match the base directory name. For example:
* - baseDir: "/project/static-analysis"
* - importPath: "static-analysis/src/types.ts"
* - Result: First tries "/project/static-analysis/src/types.ts", then "/project/static-analysis/static-analysis/src/types.ts"
*
* @param importPath - The relative import path from the JSON mapping file
* @returns Array of candidate absolute paths to try, ordered by preference
*/
private generateCandidatePaths;
/**
* Try to resolve a path with common TypeScript extensions
*
* @param basePath - The base path to resolve (with or without extension)
* @returns The resolved path if found, null otherwise
*/
private resolveWithExtensions;
/**
* Parse TypeScript interface and extract structure
*/
private parseInterface;
/**
* Extract structure from TypeScript interface declaration
*/
private extractInterfaceStructure;
/**
* Extract type information from TypeScript type node
*/
private extractTypeInfo;
/**
* Generate expected structure from JsonMapping
*/
private generateStructureFromMapping;
/**
* Infer TypeScript type from SQL column name (basic heuristics)
*/
private inferTypeFromColumnName; /**
* Compare interface structure with mapping structure (structure-only validation)
*/
private compareStructures;
}