zod-to-x
Version:
Multi language types generation from Zod schemas.
99 lines (98 loc) • 3.49 kB
TypeScript
import { ZodObject, ZodRawShape } from "zod";
import { IZod2xLayerMetadata } from "../lib/zod_ext";
import { ASTNodes } from "./ast_types";
export interface IZod2AstOpt {
/**
* When true, it will throw an error if a bad data modeling practice is detected.
* Default is true.
*/
strict?: boolean;
/**
* Metadata used to provide additional information to the AST nodes about layers modeling of
* the schema.
*/
layer?: IZod2xLayerMetadata;
}
/**
* This class creates AST nodes used to transpile Zod Schemas to other languages.
* Simply create an instance and call build with a ZodObject to obtain a list with transpilerable
* nodes.
*/
export declare class Zod2Ast {
/**
* Transpilerable nodes of current data model
*/
private nodes;
/**
* Lazy schemas for further analysis
*/
private lazyPointers;
/**
* Warnings generated during the AST creation to aware user about bad practices
*/
private warnings;
private opt;
constructor(opt?: IZod2AstOpt);
/**
* Check if the layer of the item is compatible with the layer of the schema. If does and the
* transpilerable item is in a different file, it returns the file name.
*
* @param itemName
* @param layerMetadata
* @returns
*/
private _getTranspilerableFile;
/**
* Transpilerable items are treated as references in the AST
* @param ref
* @param refType
* @param discriminantValue
* @returns
*/
private _createDefinition;
/**
* Extracts and formats the enumeration values from a given ZodEnum or ZodNativeEnum schema.
* @param schema - A ZodEnum or ZodNativeEnum schema containing the enumeration values.
* @returns A list of key-value pairs where the key is a formatted string and the value
* is either a string or a number.
*/
private _getEnumValues;
/**
* Intersects the properties of two AST nodes and returns the combined properties.
*
* @param left - The left AST definition to intersect.
* @param right - The right AST definition to intersect.
* @returns An object containing the combined properties of the left and right AST nodes.
*/
private _intersectAstNodes;
/**
* Merges multiple AST definitions into a single AST object containing combined properties.
* - Equal properties mush have the same type and array dimension.
* - If a property is optional in one definition and required in another, it will be considered
* optional in the merged object.
* - If a property is nullable in one definition and non-nullable in another, it will be
* considered nullable in the merged object.
*
* @param options - An array of AST definitions to be merged.
* @returns An object containing the merged properties.
* @throws AstNodeError - If properties with different types or array dimensions are encountered.
*/
private _unionAstNodes;
private _getNames;
private _getEnumAst;
private _getObjectAst;
private _getUnionAst;
private _getIntersectionAst;
/**
* Build the AST node of provided Zod Schema
* @param schema
* @returns
*/
private _zodToAST;
/**
* Create the AST identifying the nodes that can be transpiled.
* @param schema
* @returns Transpilerable nodes.
*/
build<T extends ZodRawShape>(schema: ZodObject<T>): ASTNodes;
}