orchid-orm-schema-to-zod 0.4.44 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,93 +1,127 @@
1
- import * as orchid_core from 'orchid-core';
2
- import { JSONDiscriminatedUnion, JSONType, ColumnTypeBase, JSONUnion, JSONArray, JSONEnum, JSONLiteral, JSONNativeEnum, JSONTuple, JSONTupleItems, JSONObject, JSONObjectShape, UnknownKeysParam, JSONRecord, JSONString, JSONNumber, JSONIntersection, ColumnsShapeBase } from 'orchid-core';
3
- import { EnumColumn, ArrayColumn, JSONColumn, VirtualColumn } from 'pqb';
4
- import { z } from 'zod';
5
- import { Buffer } from 'node:buffer';
1
+ import { ParseColumn, EncodeColumn, AsTypeArg, ColumnTypeBase, ErrorMessage, NullableColumn, StringTypeData, ColumnSchemaGetterTableClass, ErrorMessages, ColumnSchemaGetterColumns, Code } from 'orchid-core';
2
+ import { EnumColumn, ArrayColumnValue, DateBaseColumn, ColumnType, OperatorsJson, ColumnData, ArrayColumn } from 'pqb';
3
+ import { ZodTypeAny, ZodEnum, ZodNullable, ZodUnknown, ZodBoolean, ZodType, ZodNever, ZodString, ZodNumber, ZodDate, ZodObject, ZodOptional, ZodArray } from 'zod';
6
4
 
7
- type NumberType = 'smallint' | 'integer' | 'real' | 'smallserial' | 'serial' | 'money';
8
- type BigIntType = 'bigint' | 'numeric' | 'decimal' | 'double precision' | 'bigserial';
9
- type StringType = 'varchar' | 'char' | 'text' | 'string' | 'xml' | 'json';
10
- type DateTimeType = 'date' | 'timestamp' | 'timestamptz';
11
- type TimeType = 'time' | 'time with time zone';
12
- type GeometryType = 'point' | 'line' | 'lseg' | 'box' | 'path' | 'polygon' | 'circle';
13
- type NetworkType = 'cidr' | 'inet' | 'macaddr' | 'macaddr8';
14
- type BitStringType = 'bit' | 'bit varying';
15
- type FullTextSearchType = 'tsvector' | 'tsquery';
16
- type UUIDType = 'uuid';
17
- type ByteaType = 'bytea';
18
- type SchemaToZod<T extends ColumnTypeBase, D = T['dataType']> = T['data']['isNullable'] extends true ? z.ZodNullable<SchemaToZod<Omit<T, 'data'> & {
19
- data: Omit<T['data'], 'isNullable'> & {
20
- isNullable: false;
21
- };
22
- }>> : D extends NumberType ? z.ZodNumber : D extends BigIntType | StringType | TimeType | GeometryType | NetworkType | BitStringType | FullTextSearchType | UUIDType ? z.ZodString : D extends ByteaType ? z.ZodType<Buffer> : D extends DateTimeType ? z.ZodDate : D extends 'interval' ? typeof interval : D extends 'boolean' ? z.ZodBoolean : T extends EnumColumn<any, any> ? z.ZodEnum<T['options']> : T extends ArrayColumn<any> ? z.ZodArray<SchemaToZod<T['data']['item']>> : T extends JSONColumn<JSONType> ? JsonToZod<T['data']['schema']> : T extends VirtualColumn ? z.ZodNever : never;
23
- declare module 'orchid-core' {
24
- interface JSONType {
25
- zod: z.ZodTypeAny;
26
- }
27
- interface JSONUnknown {
28
- zod: z.ZodUnknown;
29
- }
30
- interface JSONBoolean {
31
- zod: z.ZodBoolean;
32
- }
33
- interface JSONNull {
34
- zod: z.ZodNull;
35
- }
36
- interface JSONNumber {
37
- zod: z.ZodNumber;
38
- }
39
- interface JSONString {
40
- zod: z.ZodString;
41
- }
5
+ type ParseDateToNumber = ParseColumn<DateBaseColumn<ZodSchemaConfig>, ZodNumber, number>;
6
+ type ParseDateToDate = ParseColumn<DateBaseColumn<ZodSchemaConfig>, ZodDate, Date>;
7
+ declare class ZodJSONColumn<ZodSchema extends ZodTypeAny> extends ColumnType<ZodSchemaConfig, ZodSchema['_output'], ZodSchema, OperatorsJson> {
8
+ dataType: "jsonb";
9
+ operators: OperatorsJson;
10
+ data: ColumnData;
11
+ constructor(schema: ZodSchema);
12
+ toCode(t: string): Code;
42
13
  }
43
- type JsonToZod<T extends JSONType> = T['data']['nullable'] extends true ? z.ZodNullable<JsonNotNullableToZod<T>> : JsonNotNullableToZod<T>;
44
- type JsonNotNullableToZod<T extends JSONType> = T['data']['optional'] extends true ? z.ZodOptional<JsonNotOptionalToZod<T>> : JsonNotOptionalToZod<T>;
45
- type JsonNotOptionalToZod<T extends JSONType> = T extends JSONUnion<any> ? z.ZodUnion<MapUnionArgs<T['data']['types']>> : T extends JSONArray<JSONType> ? z.ZodArray<JsonToZod<T['data']['item']>> : T extends JSONEnum<any, any> ? z.ZodEnum<T['data']['options']> : T extends JSONLiteral<any> ? z.ZodLiteral<T['data']['value']> : T extends JSONNativeEnum<any> ? z.ZodNativeEnum<T['data']['enum']> : T extends JSONTuple<JSONTupleItems, JSONType | undefined> ? z.ZodTuple<MapJsonTuple<T['data']['items']>, T['data']['rest'] extends JSONType ? JsonToZod<T['data']['rest']> : null> : T extends JSONObject<JSONObjectShape, UnknownKeysParam> ? z.ZodObject<{
46
- [K in keyof T['data']['shape']]: JsonToZod<T['data']['shape'][K]>;
47
- }, T['data']['unknownKeys'], JsonToZod<T['data']['catchAll']>> : T extends JSONRecord<JSONString | JSONNumber, JSONType> ? T['data']['key'] extends JSONString ? z.ZodRecord<z.ZodString, JsonToZod<T['data']['value']>> : z.ZodRecord<z.ZodNumber, JsonToZod<T['data']['value']>> : T extends JSONIntersection<JSONType, JSONType> ? z.ZodIntersection<JsonToZod<T['data']['left']>, JsonToZod<T['data']['right']>> : T extends JSONDiscriminatedUnion<string, any> ? MapDiscriminatedUnion<T> : T['zod'];
48
- type MapDiscriminatedUnion<T extends JSONDiscriminatedUnion<string, any>, Options = MapJsonTuple<T['data']['types']>> = z.ZodDiscriminatedUnion<T['data']['discriminator'], Options extends z.ZodDiscriminatedUnionOption<T['data']['discriminator']>[] ? Options : never>;
49
- type MapUnionArgs<T extends unknown[]> = T extends [
50
- infer F extends JSONType,
51
- infer S extends JSONType,
52
- ...infer Tail
53
- ] ? [JsonToZod<F>, JsonToZod<S>, ...MapJsonTuple<Tail>] : never;
54
- type MapJsonTuple<T extends unknown[]> = T extends [infer Head, ...infer Tail] ? [Head extends JSONType ? JsonToZod<Head> : never, ...MapJsonTuple<Tail>] : [];
55
- type Columns = {
56
- shape: ColumnsShapeBase;
57
- };
58
- type Table = {
59
- columns: ColumnsShapeBase;
14
+ type ArrayMethods<Value> = {
15
+ min<T extends ColumnTypeBase>(this: T, value: Value, params?: ErrorMessage): T;
16
+ max<T extends ColumnTypeBase>(this: T, value: Value, params?: ErrorMessage): T;
17
+ length<T extends ColumnTypeBase>(this: T, value: Value, params?: ErrorMessage): T;
18
+ nonEmpty<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
60
19
  };
61
- type TableClass<T extends Table> = {
62
- new (): T;
63
- instance(): unknown;
20
+ interface ZodArrayColumn<Item extends ArrayColumnValue> extends ArrayColumn<ZodSchemaConfig, Item, ZodArray<Item['inputSchema']>, ZodArray<Item['outputSchema']>, ZodArray<Item['querySchema']>>, ArrayMethods<number> {
21
+ }
22
+ declare class ZodArrayColumn<Item extends ArrayColumnValue> extends ArrayColumn<ZodSchemaConfig, Item, ZodArray<Item['inputSchema']>, ZodArray<Item['outputSchema']>, ZodArray<Item['querySchema']>> {
23
+ constructor(item: Item);
24
+ }
25
+ type ZodSchemaConfig = {
26
+ type: ZodTypeAny;
27
+ parse<T extends {
28
+ type: unknown;
29
+ }, OutputSchema extends ZodTypeAny, Output = OutputSchema['_output']>(this: T, _schema: OutputSchema, fn: (input: T['type']) => Output): ParseColumn<T, OutputSchema, Output>;
30
+ encode<T extends {
31
+ type: unknown;
32
+ }, InputSchema extends ZodTypeAny, Input = InputSchema['_output']>(this: T, _schema: InputSchema, fn: (input: Input) => unknown): EncodeColumn<T, InputSchema, Input>;
33
+ asType<T, Types extends AsTypeArg<ZodTypeAny>, TypeSchema extends ZodTypeAny = Types extends {
34
+ type: ZodTypeAny;
35
+ } ? Types['type'] : never, Type = TypeSchema['_output']>(this: T, types: Types): Omit<T, 'type' | 'inputType' | 'inputSchema' | 'outputType' | 'outputSchema' | 'queryType' | 'querySchema'> & {
36
+ type: Type;
37
+ inputType: Types['input'] extends ZodTypeAny ? Types['input']['_output'] : Type;
38
+ inputSchema: Types['input'] extends ZodTypeAny ? Types['input'] : TypeSchema;
39
+ outputType: Types['output'] extends ZodTypeAny ? Types['output']['_output'] : Type;
40
+ outputSchema: Types['output'] extends ZodTypeAny ? Types['output'] : TypeSchema;
41
+ queryType: Types['query'] extends ZodTypeAny ? Types['query']['_output'] : Type;
42
+ querySchema: Types['query'] extends ZodTypeAny ? Types['query'] : TypeSchema;
43
+ };
44
+ dateAsNumber(): ParseDateToNumber;
45
+ dateAsDate(): ParseDateToDate;
46
+ dateMethods: {
47
+ min<T extends ColumnTypeBase>(this: T, value: Date, params?: ErrorMessage): T;
48
+ max<T extends ColumnTypeBase>(this: T, value: Date, params?: ErrorMessage): T;
49
+ };
50
+ enum<U extends string, T extends [U, ...U[]]>(dataType: string, type: T): EnumColumn<ZodSchemaConfig, ZodEnum<T>, U, T>;
51
+ array<Item extends ArrayColumnValue>(item: Item): ZodArrayColumn<Item>;
52
+ nullable<T extends ColumnTypeBase>(this: T): NullableColumn<T, ZodNullable<T['inputSchema']>, ZodNullable<T['outputSchema']>, ZodNullable<T['querySchema']>>;
53
+ json<ZodSchema extends ZodTypeAny = ZodUnknown>(schema?: ZodSchema): ZodJSONColumn<ZodSchema>;
54
+ boolean: ZodBoolean;
55
+ buffer: ZodType<Buffer>;
56
+ unknown: ZodUnknown;
57
+ never: ZodNever;
58
+ string: ZodString;
59
+ stringMin(max: number): ZodString;
60
+ stringMax(max: number): ZodString;
61
+ stringMinMax(min: number, max: number): ZodString;
62
+ stringMethods: ArrayMethods<number> & {
63
+ email<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
64
+ url<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
65
+ emoji<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
66
+ uuid<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
67
+ cuid<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
68
+ cuid2<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
69
+ ulid<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
70
+ regex<T extends ColumnTypeBase>(this: T, value: RegExp, params?: ErrorMessage): T;
71
+ includes<T extends ColumnTypeBase, Value extends string>(this: T, value: Value, params?: ErrorMessage): T;
72
+ startsWith<T extends ColumnTypeBase, Value extends string>(this: T, value: Value, params?: ErrorMessage): T;
73
+ endsWith<T extends ColumnTypeBase, Value extends string>(this: T, value: Value, params?: ErrorMessage): T;
74
+ datetime<T extends ColumnTypeBase>(this: T, params?: StringTypeData['datetime'] & Exclude<ErrorMessage, string>): T;
75
+ ip<T extends ColumnTypeBase>(this: T, params?: StringTypeData['ip'] & Exclude<ErrorMessage, string>): T;
76
+ trim<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
77
+ toLowerCase<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
78
+ toUpperCase<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
79
+ };
80
+ number: ZodNumber;
81
+ numberMethods: {
82
+ lt<T extends ColumnTypeBase>(this: T, value: number, params?: ErrorMessage): T;
83
+ lte<T extends ColumnTypeBase>(this: T, value: number, params?: ErrorMessage): T;
84
+ max<T extends ColumnTypeBase>(this: T, value: number, params?: ErrorMessage): T;
85
+ gt<T extends ColumnTypeBase>(this: T, value: number, params?: ErrorMessage): T;
86
+ gte<T extends ColumnTypeBase>(this: T, value: number, params?: ErrorMessage): T;
87
+ min<T extends ColumnTypeBase>(this: T, value: number, params?: ErrorMessage): T;
88
+ positive<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
89
+ nonNegative<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
90
+ negative<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
91
+ nonPositive<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
92
+ step<T extends ColumnTypeBase>(this: T, value: number, params?: ErrorMessage): T;
93
+ int<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
94
+ finite<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
95
+ safe<T extends ColumnTypeBase>(this: T, params?: ErrorMessage): T;
96
+ };
97
+ int: ZodNumber;
98
+ stringNumberDate: ZodDate;
99
+ timeInterval: ZodObject<{
100
+ years: ZodOptional<ZodNumber>;
101
+ months: ZodOptional<ZodNumber>;
102
+ days: ZodOptional<ZodNumber>;
103
+ hours: ZodOptional<ZodNumber>;
104
+ minutes: ZodOptional<ZodNumber>;
105
+ seconds: ZodOptional<ZodNumber>;
106
+ }>;
107
+ bit(max: number): ZodString;
108
+ uuid: ZodString;
109
+ inputSchema<T extends ColumnSchemaGetterTableClass>(this: T): MapSchema<T, 'inputSchema'>;
110
+ outputSchema<T extends ColumnSchemaGetterTableClass>(this: T): MapSchema<T, 'outputSchema'>;
111
+ querySchema<T extends ColumnSchemaGetterTableClass>(this: T): MapSchema<T, 'querySchema'>;
112
+ updateSchema<T extends ColumnSchemaGetterTableClass>(this: T): UpdateSchema<T>;
113
+ pkeySchema<T extends ColumnSchemaGetterTableClass>(this: T): PkeySchema<T>;
114
+ errors<T extends ColumnTypeBase>(this: T, errors: ErrorMessages): T;
64
115
  };
65
- type InstanceToZod<T extends Columns> = z.ZodObject<{
66
- [K in keyof T['shape']]: SchemaToZod<T['shape'][K]>;
67
- }>;
68
- declare const instanceToZod: <T extends Columns>({ shape, }: T) => InstanceToZod<T>;
69
- declare const zodSchemaProvider: <T extends Table>(this: TableClass<T>) => InstanceToZod<{
70
- shape: T["columns"];
71
- }>;
72
- declare const columnToZod: <T extends ColumnTypeBase<unknown, orchid_core.BaseOperators, unknown, unknown, unknown, orchid_core.ColumnDataBase>>(column: T) => SchemaToZod<T, T["dataType"]>;
73
- declare const interval: z.ZodObject<{
74
- years: z.ZodOptional<z.ZodNumber>;
75
- months: z.ZodOptional<z.ZodNumber>;
76
- days: z.ZodOptional<z.ZodNumber>;
77
- hours: z.ZodOptional<z.ZodNumber>;
78
- seconds: z.ZodOptional<z.ZodNumber>;
79
- }, "strict", z.ZodTypeAny, {
80
- years?: number | undefined;
81
- months?: number | undefined;
82
- days?: number | undefined;
83
- hours?: number | undefined;
84
- seconds?: number | undefined;
85
- }, {
86
- years?: number | undefined;
87
- months?: number | undefined;
88
- days?: number | undefined;
89
- hours?: number | undefined;
90
- seconds?: number | undefined;
91
- }>;
116
+ declare const zodSchemaConfig: ZodSchemaConfig;
117
+ type MapSchema<T extends ColumnSchemaGetterTableClass, Key extends 'inputSchema' | 'outputSchema' | 'querySchema'> = ZodObject<{
118
+ [K in keyof ColumnSchemaGetterColumns<T>]: ColumnSchemaGetterColumns<T>[K][Key];
119
+ }, 'strip'>;
120
+ type UpdateSchema<T extends ColumnSchemaGetterTableClass> = ZodObject<{
121
+ [K in keyof ColumnSchemaGetterColumns<T>]: ZodOptional<ColumnSchemaGetterColumns<T>[K]['inputSchema']>;
122
+ }, 'strip'>;
123
+ type PkeySchema<T extends ColumnSchemaGetterTableClass> = ZodObject<{
124
+ [K in keyof ColumnSchemaGetterColumns<T> as ColumnSchemaGetterColumns<T>[K]['data']['isPrimaryKey'] extends true ? K : never]: ColumnSchemaGetterColumns<T>[K]['inputSchema'];
125
+ }, 'strip'>;
92
126
 
93
- export { InstanceToZod, MapDiscriminatedUnion, columnToZod, instanceToZod, zodSchemaProvider };
127
+ export { ZodSchemaConfig, zodSchemaConfig };