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
127 lines • 4.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isTopLevelIntersectionWithSupportsRuntimeValidation = isTopLevelIntersectionWithSupportsRuntimeValidation;
exports.checkTypeInSourceCode = checkTypeInSourceCode;
const ts_morph_1 = require("ts-morph");
/**
* Utility function to determine whether a type or typeAlias object is a top-level
* intersection with SupportsRuntimeValidation.
*
* Examples:
* - type A = { id: number, title: string } → false
* - type B = A & SupportsRuntimeValidation → true
* - type C = Pick<B, 'id'> → false
* - type D = Pick<B, 'id'> & SupportsRuntimeValidation → true
*
* @param typeDeclaration - The TypeAliasDeclaration or InterfaceDeclaration to check
* @returns true if the type is a top-level intersection with SupportsRuntimeValidation, false otherwise
*/
function isTopLevelIntersectionWithSupportsRuntimeValidation(typeDeclaration) {
// Handle InterfaceDeclaration
if (ts_morph_1.Node.isInterfaceDeclaration(typeDeclaration)) {
return checkInterfaceForSupportsRuntimeValidation(typeDeclaration);
}
// Handle TypeAliasDeclaration
if (ts_morph_1.Node.isTypeAliasDeclaration(typeDeclaration)) {
return checkTypeAliasForSupportsRuntimeValidation(typeDeclaration);
}
return false;
}
/**
* Check if an interface extends SupportsRuntimeValidation
*/
function checkInterfaceForSupportsRuntimeValidation(interfaceDecl) {
const extendsClauses = interfaceDecl.getExtends();
for (const extendsClause of extendsClauses) {
// Get the expression from the extends clause
const expression = extendsClause.getExpression();
if (isSupportsRuntimeValidationReference(expression)) {
return true;
}
}
return false;
}
/**
* Check if a type alias is a top-level intersection with SupportsRuntimeValidation
*/
function checkTypeAliasForSupportsRuntimeValidation(typeAlias) {
const typeNode = typeAlias.getTypeNode();
if (!typeNode) {
return false;
}
return checkTypeNodeForSupportsRuntimeValidation(typeNode);
}
/**
* Check if a type node contains SupportsRuntimeValidation in a top-level intersection
*/
function checkTypeNodeForSupportsRuntimeValidation(typeNode) {
// Check if it's an intersection type (A & B & C)
if (ts_morph_1.Node.isIntersectionTypeNode(typeNode)) {
const intersectionTypes = typeNode.getTypeNodes();
// Check each member of the intersection
for (const intersectionMember of intersectionTypes) {
if (isSupportsRuntimeValidationReference(intersectionMember)) {
return true;
}
}
}
// Check if it's a direct reference to SupportsRuntimeValidation (edge case)
if (isSupportsRuntimeValidationReference(typeNode)) {
return true;
}
return false;
}
/**
* Check if a node is a reference to SupportsRuntimeValidation
*/
function isSupportsRuntimeValidationReference(node) {
// Check if it's a type reference
if (ts_morph_1.Node.isTypeReference(node)) {
const typeName = node.getTypeName();
// Handle simple identifier (SupportsRuntimeValidation)
if (ts_morph_1.Node.isIdentifier(typeName)) {
return typeName.getText() === "SupportsRuntimeValidation";
}
// Handle qualified name (e.g., SomeModule.SupportsRuntimeValidation)
if (ts_morph_1.Node.isQualifiedName(typeName)) {
const right = typeName.getRight();
return right.getText() === "SupportsRuntimeValidation";
}
}
// Handle direct identifier reference
if (ts_morph_1.Node.isIdentifier(node)) {
return node.getText() === "SupportsRuntimeValidation";
}
return false;
}
/**
* Convenience function that works with source code strings
*
* @param sourceCode - TypeScript source code containing type definitions
* @param typeName - Name of the type to check
* @returns true if the specified type is a top-level intersection with SupportsRuntimeValidation
*/
function checkTypeInSourceCode(sourceCode, typeName) {
const { Project } = require("ts-morph");
const project = new Project({
compilerOptions: {
target: "ES2020",
module: "CommonJS",
strict: true,
},
useInMemoryFileSystem: true,
});
const sourceFile = project.createSourceFile("temp.ts", sourceCode);
// Try to find as type alias first
const typeAlias = sourceFile.getTypeAlias(typeName);
if (typeAlias) {
return isTopLevelIntersectionWithSupportsRuntimeValidation(typeAlias);
}
// Try to find as interface
const interfaceDecl = sourceFile.getInterface(typeName);
if (interfaceDecl) {
return isTopLevelIntersectionWithSupportsRuntimeValidation(interfaceDecl);
}
throw new Error(`Type ${typeName} not found in source code`);
}
//# sourceMappingURL=supports-runtime-validation-checker.js.map