MCPcopy
hub / github.com/drizzle-team/drizzle-orm / columnToSchema

Function columnToSchema

drizzle-zod/src/column.ts:70–135  ·  view source on GitHub ↗
(
	column: Column,
	factory:
		| CreateSchemaFactoryOptions<
			Partial<Record<'bigint' | 'boolean' | 'date' | 'number' | 'string', true>> | true | undefined
		>
		| undefined,
)

Source from the content-addressed store, hash-verified

68export const bufferSchema: zod.ZodType<Buffer> = zod.custom<Buffer>((v) => v instanceof Buffer); // eslint-disable-line no-instanceof/no-instanceof
69
70export function columnToSchema(
71 column: Column,
72 factory:
73 | CreateSchemaFactoryOptions<
74 Partial<Record<'bigint' | 'boolean' | 'date' | 'number' | 'string', true>> | true | undefined
75 >
76 | undefined,
77): zod.ZodType {
78 const z: typeof zod = factory?.zodInstance ?? zod;
79 const coerce = factory?.coerce ?? {};
80 let schema!: zod.ZodType;
81
82 if (isWithEnum(column)) {
83 schema = column.enumValues.length ? z.enum(column.enumValues) : z.string();
84 }
85
86 if (!schema) {
87 // Handle specific types
88 if (isColumnType<PgGeometry<any> | PgPointTuple<any>>(column, ['PgGeometry', 'PgPointTuple'])) {
89 schema = z.tuple([z.number(), z.number()]);
90 } else if (
91 isColumnType<PgPointObject<any> | PgGeometryObject<any>>(column, ['PgGeometryObject', 'PgPointObject'])
92 ) {
93 schema = z.object({ x: z.number(), y: z.number() });
94 } else if (isColumnType<PgHalfVector<any> | PgVector<any>>(column, ['PgHalfVector', 'PgVector'])) {
95 schema = z.array(z.number());
96 schema = column.dimensions ? (schema as zod.ZodArray<any>).length(column.dimensions) : schema;
97 } else if (isColumnType<PgLineTuple<any>>(column, ['PgLine'])) {
98 schema = z.tuple([z.number(), z.number(), z.number()]);
99 } else if (isColumnType<PgLineABC<any>>(column, ['PgLineABC'])) {
100 schema = z.object({
101 a: z.number(),
102 b: z.number(),
103 c: z.number(),
104 });
105 } // Handle other types
106 else if (isColumnType<PgArray<any, any>>(column, ['PgArray'])) {
107 schema = z.array(columnToSchema(column.baseColumn, factory));
108 schema = column.size ? (schema as zod.ZodArray<any>).length(column.size) : schema;
109 } else if (column.dataType === 'array') {
110 schema = z.array(z.any());
111 } else if (column.dataType === 'number') {
112 schema = numberColumnToSchema(column, z, coerce);
113 } else if (column.dataType === 'bigint') {
114 schema = bigintColumnToSchema(column, z, coerce);
115 } else if (column.dataType === 'boolean') {
116 schema = coerce === true || coerce.boolean ? z.coerce.boolean() : z.boolean();
117 } else if (column.dataType === 'date') {
118 schema = coerce === true || coerce.date ? z.coerce.date() : z.date();
119 } else if (column.dataType === 'string') {
120 schema = stringColumnToSchema(column, z, coerce);
121 } else if (column.dataType === 'json') {
122 schema = jsonSchema;
123 } else if (column.dataType === 'custom') {
124 schema = z.any();
125 } else if (column.dataType === 'buffer') {
126 schema = bufferSchema;
127 }

Callers 1

handleColumnsFunction · 0.90

Calls 6

isWithEnumFunction · 0.90
isColumnTypeFunction · 0.90
enumMethod · 0.80
numberColumnToSchemaFunction · 0.70
bigintColumnToSchemaFunction · 0.70
stringColumnToSchemaFunction · 0.70

Tested by

no test coverage detected