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
145 lines (144 loc) • 4.14 kB
TypeScript
export interface PropertySchema {
type: "string" | "number" | "boolean" | "object" | "array";
required: boolean;
constraints: Record<string, any>;
properties?: Record<string, PropertySchema>;
arrayElementType?: PropertySchema;
}
export interface ParsedSchema {
[propertyName: string]: PropertySchema;
}
/**
* TypeParser class that can parse TypeScript types with constraints
* and convert them to schema objects
*/
export declare class TypeParser {
private project;
private sourceFile;
constructor(sourceCode: string);
/**
* Parse a type by name and return its schema
*/
parseType(typeName: string): ParsedSchema;
/**
* Find a type declaration (type alias or interface) by name
*/
private findTypeDeclaration;
/**
* Extract properties from a type declaration
*/
private extractPropertiesFromType;
/**
* Parse a property type node and extract base type and constraints
*/
private parsePropertyType;
/**
* Check if a type name represents a constraint type
* Dynamically loads constraint types from constraint-types.ts
*/
private isConstraintType;
/**
* Get constraint type names from constraint-types.ts file
*/
private getConstraintTypeNames;
/**
* Parse a constraint type reference and extract its value
*/
private parseConstraintType;
/**
* Convert a constraint type name to camelCase
*/
private toCamelCase;
/**
* Get the base type from a type node
*/
private getBaseType;
/**
* Infer base type from constraints (fallback method)
*/
private inferBaseTypeFromConstraints;
/**
* Parse array element type from an array type node
*/
private parseArrayElementType;
/**
* Parse nested object type from a type node
*/
private parseNestedObjectType;
}
/**
* Convenience function to parse a type from the current file
*/
export declare function parseTypeFromSource(typeName: string, sourceCode: string): ParsedSchema;
/**
* USAGE EXAMPLES:
*
* Basic usage:
* ```typescript
* const parser = new TypeParser(sourceCode);
* const schema = parser.parseType("MyType");
* ```
*
* Using the convenience function:
* ```typescript
* const schema = parseTypeFromSource("MyType", sourceCode);
* ```
*
* SUPPORTED CONSTRAINT TYPES:
* - Min<N>: Minimum value for numbers
* - Max<N>: Maximum value for numbers
* - MinLength<N>: Minimum length for strings and arrays
* - MaxLength<N>: Maximum length for strings and arrays
* - Email: Email validation flag
* - UUID: UUID validation flag
* - URL: URL validation flag
* - And many more...
*
* ARRAY CONSTRAINT SUPPORT:
* Arrays can have length constraints applied:
* ```typescript
* type MyType = {
* tags: string[] & MinLength<1> & MaxLength<10>;
* scores: number[] & MinLength<3>;
* }
* ```
*
* NESTED OBJECT SUPPORT:
* The parser supports nested objects and arrays of objects:
* ```typescript
* type Person = {
* name: string & MinLength<2>;
* contact: {
* email: string & Email;
* phone?: string;
* };
* };
*
* type Company = {
* employees: Person[] & MinLength<1>;
* settings: {
* theme: string;
* limits: {
* maxUsers: number & Max<1000>;
* };
* };
* };
* ```
*
* EXTENSIBILITY:
* To add new constraint types:
* 1. Define the constraint type: `type MyConstraint<T> = Constraint<T>;`
* 2. Add it to the `isConstraintType` method's array
* 3. The constraint name will be automatically converted to camelCase (e.g., MyConstraint -> myConstraint)
*
* The parser automatically handles:
* - Intersection types (string & Min<10> & Max<80>)
* - Array constraints (string[] & MinLength<1> & MaxLength<10>)
* - Nested objects with full constraint support
* - Arrays of objects with element type parsing
* - Type references to other defined types
* - Inline object definitions
* - Optional properties (property?: type)
* - Base type detection (string, number, boolean, array, object)
* - Constraint value extraction from generic parameters
*/