UNPKG

zod-to-x

Version:

Multi language types generation from Zod schemas.

124 lines (123 loc) 4.81 kB
import { ASTAliasedTypes, ASTEnum, ASTIntersection, ASTNode, ASTObject, ASTUnion, Zod2X } from "../../core"; import { IZod2PyOpt } from "./options"; export declare class Zod2Py extends Zod2X<IZod2PyOpt> { protected readonly commentKey = "#"; protected lib: { baseModel: string; fieldImport: string; aliasGenerator: string; annotatedType: string; typeAliasType: string; genericType: string; typeVarType: string; enumType: string; anyType: string; listType: string; dictType: string; setType: string; tupleType: string; unionType: string; optionalType: string; literalType: string; datetimeType: string; }; private baseSchemaAdded; private typeVars; private pendingTypeVars; constructor(opt?: IZod2PyOpt); protected runAfter(): void; protected runBefore(): void; /** * Adds BaseSchema class definition if not already added. * This is the base class for all Pydantic models with shared configuration. */ private _addBaseSchema; private _flushPendingTypeVars; /** * Declares TypeVars that haven't been declared yet. * Adds them right before their first usage. * Ex: T = TypeVar('T') */ private _declareNewTypeVars; /** * Consolidates multiline imports from the same module and sorts them alphabetically. * Modifies this.imports Set to contain consolidated import statements. */ private _consolidateImports; protected addImportFromFile(filename: string, namespace: string): string; protected getTypeFromExternalNamespace(namespace: string, typeName: string): string; protected addExtendedType(name: string, parentNamespace: string, aliasOf: string, opt?: { type?: "union" | "d-union" | "alias"; isInternal?: boolean; templates?: string; isClass?: boolean; }): void; protected getGenericTemplatesTranslation(data: ASTNode): string | undefined; /** * Emits an alias/extension declaration early for layered references. * It keeps concrete template translations and falls back to declared templates (e.g. [T]) * for aliases of generic templates. */ protected checkExtendedTypeInclusion(data: ASTNode, type?: "alias" | "union" | "d-union"): boolean; protected getAnyType: () => string; protected getBooleanType: () => string; protected getDateType: () => string; /** Ex: Set[TypeA] */ protected getSetType: (itemType: string) => string; protected getStringType: () => string; /** Ex: Tuple[TypeA, TypeB] */ protected getTupleType: (itemsType: string[]) => string; /** Ex: Union[TypeA, TypeB] */ protected getUnionType: (itemsType: string[]) => string; /** Ex: TypeA & TypeB -> intersection handling */ protected getIntersectionType: (itemsType: string[]) => string; /** Ex: int or float depending on isInt flag */ protected getNumberType: (isInt: boolean, range: { min?: number; max?: number; }) => string; /** Ex: List[List[TypeA]] */ protected getArrayType(arrayType: string, arrayDeep: number): string; /** Ex: Literal["value"] or Literal[true] or EnumName.ENUM_VALUE */ protected getLiteralStringType(value: string | number | boolean, parentEnumNameKey?: [string, string]): string | number; /** Ex: Dict[TypeA, TypeB] */ protected getMapType(keyType: string, valueType: string): string; /** Ex: Dict[TypeA, TypeB] */ protected getRecordType(keyType: string, valueType: string): string; protected transpileAliasedType(data: ASTAliasedTypes): void; /** Ex: * class MyEnum(str, Enum): * ITEM_KEY1 = "ItemValue1" * ITEM_KEY2 = "ItemValue2" * * # Or for mixed types: * class MyEnum(Enum): * ITEM_KEY1 = 1 * ITEM_KEY2 = "ItemValue2" */ protected transpileEnum(data: ASTEnum): void; protected transpileIntersection(data: ASTIntersection): void; protected transpileStruct(data: ASTObject): void; protected transpileUnion(data: ASTUnion): void; /** * Creates a wrapper class for a union type. * Python/Pydantic needs this for proper serialization/deserialization of unions. * Ex: * class UnionItemWrapper(BaseSchema): * data: UnionItem */ private _createUnionWrapper; /** Ex: * class MyStruct(BaseSchema): * att1: TypeA * att2: Optional[TypeB] = None * * # Or with generics: * class MyGenericStruct(BaseSchema, Generic[T]): * data: T * */ private _transpileStructAsClass; /** For Class attributes. * Ex: attribute1: Optional[TypeA] = None */ private _transpileMember; }