drizzle-valibot 1.0.0-beta.6-ea4d663 → 1.0.0-beta.6-b046b02
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/index.cjs +332 -366
- package/index.cjs.map +1 -1
- package/index.d.cts +101 -6
- package/index.d.mts +101 -6
- package/index.d.ts +101 -6
- package/index.mjs +304 -345
- package/index.mjs.map +1 -1
- package/package.json +2 -3
- package/column.d.cts +0 -10
- package/column.d.mts +0 -10
- package/column.d.ts +0 -10
- package/column.types.d.cts +0 -57
- package/column.types.d.mts +0 -57
- package/column.types.d.ts +0 -57
- package/constants.d.cts +0 -20
- package/constants.d.mts +0 -20
- package/constants.d.ts +0 -20
- package/schema.d.cts +0 -4
- package/schema.d.mts +0 -4
- package/schema.d.ts +0 -4
- package/schema.types.d.cts +0 -21
- package/schema.types.d.mts +0 -21
- package/schema.types.d.ts +0 -21
- package/schema.types.internal.d.cts +0 -22
- package/schema.types.internal.d.mts +0 -22
- package/schema.types.internal.d.ts +0 -22
- package/utils.d.cts +0 -28
- package/utils.d.mts +0 -28
- package/utils.d.ts +0 -28
package/index.mjs
CHANGED
|
@@ -1,386 +1,345 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import * as v from
|
|
1
|
+
import { Column, SQL, extractExtendedColumnType, getColumnTable, getTableColumns, getTableName, getViewSelectedFields, is, isTable, isView } from "drizzle-orm";
|
|
2
|
+
import * as v from "valibot";
|
|
3
3
|
|
|
4
|
+
//#region src/constants.ts
|
|
4
5
|
const CONSTANTS = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
6
|
+
INT8_MIN: -128,
|
|
7
|
+
INT8_MAX: 127,
|
|
8
|
+
INT8_UNSIGNED_MAX: 255,
|
|
9
|
+
INT16_MIN: -32768,
|
|
10
|
+
INT16_MAX: 32767,
|
|
11
|
+
INT16_UNSIGNED_MAX: 65535,
|
|
12
|
+
INT24_MIN: -8388608,
|
|
13
|
+
INT24_MAX: 8388607,
|
|
14
|
+
INT24_UNSIGNED_MAX: 16777215,
|
|
15
|
+
INT32_MIN: -2147483648,
|
|
16
|
+
INT32_MAX: 2147483647,
|
|
17
|
+
INT32_UNSIGNED_MAX: 4294967295,
|
|
18
|
+
INT48_MIN: -0x800000000000,
|
|
19
|
+
INT48_MAX: 0x7fffffffffff,
|
|
20
|
+
INT48_UNSIGNED_MAX: 0xffffffffffff,
|
|
21
|
+
INT64_MIN: -9223372036854775808n,
|
|
22
|
+
INT64_MAX: 9223372036854775807n,
|
|
23
|
+
INT64_UNSIGNED_MAX: 18446744073709551615n
|
|
23
24
|
};
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/column.ts
|
|
28
|
+
const literalSchema = v.union([
|
|
29
|
+
v.string(),
|
|
30
|
+
v.number(),
|
|
31
|
+
v.boolean(),
|
|
32
|
+
v.null()
|
|
33
|
+
]);
|
|
26
34
|
const jsonSchema = v.union([
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
35
|
+
literalSchema,
|
|
36
|
+
v.array(v.any()),
|
|
37
|
+
v.record(v.string(), v.any())
|
|
30
38
|
]);
|
|
31
|
-
const bufferSchema = v.custom((v) => v instanceof Buffer);
|
|
39
|
+
const bufferSchema = v.custom((v$1) => v$1 instanceof Buffer);
|
|
32
40
|
function mapEnumValues(values) {
|
|
33
|
-
|
|
41
|
+
return Object.fromEntries(values.map((value) => [value, value]));
|
|
34
42
|
}
|
|
35
43
|
function columnToSchema(column) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
default: {
|
|
68
|
-
schema = v.any();
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return schema;
|
|
44
|
+
let schema;
|
|
45
|
+
const dimensions = column.dimensions;
|
|
46
|
+
if (typeof dimensions === "number" && dimensions > 0) return pgArrayColumnToSchema(column, dimensions);
|
|
47
|
+
const { type, constraint } = extractExtendedColumnType(column);
|
|
48
|
+
switch (type) {
|
|
49
|
+
case "array":
|
|
50
|
+
schema = arrayColumnToSchema(column, constraint);
|
|
51
|
+
break;
|
|
52
|
+
case "object":
|
|
53
|
+
schema = objectColumnToSchema(column, constraint);
|
|
54
|
+
break;
|
|
55
|
+
case "number":
|
|
56
|
+
schema = numberColumnToSchema(column, constraint);
|
|
57
|
+
break;
|
|
58
|
+
case "bigint":
|
|
59
|
+
schema = bigintColumnToSchema(column, constraint);
|
|
60
|
+
break;
|
|
61
|
+
case "boolean":
|
|
62
|
+
schema = v.boolean();
|
|
63
|
+
break;
|
|
64
|
+
case "string":
|
|
65
|
+
schema = stringColumnToSchema(column, constraint);
|
|
66
|
+
break;
|
|
67
|
+
case "custom":
|
|
68
|
+
schema = v.any();
|
|
69
|
+
break;
|
|
70
|
+
default: schema = v.any();
|
|
71
|
+
}
|
|
72
|
+
return schema;
|
|
72
73
|
}
|
|
73
74
|
function numberColumnToSchema(column, constraint) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
integer = true;
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
case 'unsigned': {
|
|
165
|
-
min = 0;
|
|
166
|
-
max = Number.MAX_SAFE_INTEGER;
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
default: {
|
|
170
|
-
min = Number.MIN_SAFE_INTEGER;
|
|
171
|
-
max = Number.MAX_SAFE_INTEGER;
|
|
172
|
-
break;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
const actions = [v.minValue(min), v.maxValue(max)];
|
|
176
|
-
if (integer) {
|
|
177
|
-
actions.push(v.integer());
|
|
178
|
-
}
|
|
179
|
-
return v.pipe(v.number(), ...actions);
|
|
75
|
+
let min;
|
|
76
|
+
let max;
|
|
77
|
+
let integer = false;
|
|
78
|
+
switch (constraint) {
|
|
79
|
+
case "int8":
|
|
80
|
+
min = CONSTANTS.INT8_MIN;
|
|
81
|
+
max = CONSTANTS.INT8_MAX;
|
|
82
|
+
integer = true;
|
|
83
|
+
break;
|
|
84
|
+
case "uint8":
|
|
85
|
+
min = 0;
|
|
86
|
+
max = CONSTANTS.INT8_UNSIGNED_MAX;
|
|
87
|
+
integer = true;
|
|
88
|
+
break;
|
|
89
|
+
case "int16":
|
|
90
|
+
min = CONSTANTS.INT16_MIN;
|
|
91
|
+
max = CONSTANTS.INT16_MAX;
|
|
92
|
+
integer = true;
|
|
93
|
+
break;
|
|
94
|
+
case "uint16":
|
|
95
|
+
min = 0;
|
|
96
|
+
max = CONSTANTS.INT16_UNSIGNED_MAX;
|
|
97
|
+
integer = true;
|
|
98
|
+
break;
|
|
99
|
+
case "int24":
|
|
100
|
+
min = CONSTANTS.INT24_MIN;
|
|
101
|
+
max = CONSTANTS.INT24_MAX;
|
|
102
|
+
integer = true;
|
|
103
|
+
break;
|
|
104
|
+
case "uint24":
|
|
105
|
+
min = 0;
|
|
106
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
107
|
+
integer = true;
|
|
108
|
+
break;
|
|
109
|
+
case "int32":
|
|
110
|
+
min = CONSTANTS.INT32_MIN;
|
|
111
|
+
max = CONSTANTS.INT32_MAX;
|
|
112
|
+
integer = true;
|
|
113
|
+
break;
|
|
114
|
+
case "uint32":
|
|
115
|
+
min = 0;
|
|
116
|
+
max = CONSTANTS.INT32_UNSIGNED_MAX;
|
|
117
|
+
integer = true;
|
|
118
|
+
break;
|
|
119
|
+
case "int53":
|
|
120
|
+
min = Number.MIN_SAFE_INTEGER;
|
|
121
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
122
|
+
integer = true;
|
|
123
|
+
break;
|
|
124
|
+
case "uint53":
|
|
125
|
+
min = 0;
|
|
126
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
127
|
+
integer = true;
|
|
128
|
+
break;
|
|
129
|
+
case "float":
|
|
130
|
+
min = CONSTANTS.INT24_MIN;
|
|
131
|
+
max = CONSTANTS.INT24_MAX;
|
|
132
|
+
break;
|
|
133
|
+
case "ufloat":
|
|
134
|
+
min = 0;
|
|
135
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
136
|
+
break;
|
|
137
|
+
case "double":
|
|
138
|
+
min = CONSTANTS.INT48_MIN;
|
|
139
|
+
max = CONSTANTS.INT48_MAX;
|
|
140
|
+
break;
|
|
141
|
+
case "udouble":
|
|
142
|
+
min = 0;
|
|
143
|
+
max = CONSTANTS.INT48_UNSIGNED_MAX;
|
|
144
|
+
break;
|
|
145
|
+
case "year":
|
|
146
|
+
min = 1901;
|
|
147
|
+
max = 2155;
|
|
148
|
+
integer = true;
|
|
149
|
+
break;
|
|
150
|
+
case "unsigned":
|
|
151
|
+
min = 0;
|
|
152
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
153
|
+
break;
|
|
154
|
+
default:
|
|
155
|
+
min = Number.MIN_SAFE_INTEGER;
|
|
156
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
const actions = [v.minValue(min), v.maxValue(max)];
|
|
160
|
+
if (integer) actions.push(v.integer());
|
|
161
|
+
return v.pipe(v.number(), ...actions);
|
|
180
162
|
}
|
|
181
163
|
/** @internal */
|
|
182
|
-
const bigintStringModeSchema = v.pipe(v.string(), v.regex(/^-?\d+$/),
|
|
183
|
-
// eslint-disable-next-line unicorn/prefer-native-coercion-functions
|
|
184
|
-
v.transform((v) => BigInt(v)), v.minValue(CONSTANTS.INT64_MIN), v.maxValue(CONSTANTS.INT64_MAX), v.transform((v) => v.toString()));
|
|
164
|
+
const bigintStringModeSchema = v.pipe(v.string(), v.regex(/^-?\d+$/), v.transform((v$1) => BigInt(v$1)), v.minValue(CONSTANTS.INT64_MIN), v.maxValue(CONSTANTS.INT64_MAX), v.transform((v$1) => v$1.toString()));
|
|
185
165
|
/** @internal */
|
|
186
|
-
const unsignedBigintStringModeSchema = v.pipe(v.string(), v.regex(/^\d+$/),
|
|
187
|
-
// eslint-disable-next-line unicorn/prefer-native-coercion-functions
|
|
188
|
-
v.transform((v) => BigInt(v)), v.minValue(0n), v.maxValue(CONSTANTS.INT64_MAX), v.transform((v) => v.toString()));
|
|
166
|
+
const unsignedBigintStringModeSchema = v.pipe(v.string(), v.regex(/^\d+$/), v.transform((v$1) => BigInt(v$1)), v.minValue(0n), v.maxValue(CONSTANTS.INT64_MAX), v.transform((v$1) => v$1.toString()));
|
|
189
167
|
function bigintColumnToSchema(column, constraint) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
168
|
+
let min;
|
|
169
|
+
let max;
|
|
170
|
+
switch (constraint) {
|
|
171
|
+
case "int64":
|
|
172
|
+
min = CONSTANTS.INT64_MIN;
|
|
173
|
+
max = CONSTANTS.INT64_MAX;
|
|
174
|
+
break;
|
|
175
|
+
case "uint64":
|
|
176
|
+
min = 0n;
|
|
177
|
+
max = CONSTANTS.INT64_UNSIGNED_MAX;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
const actions = [];
|
|
181
|
+
if (min !== void 0) actions.push(v.minValue(min));
|
|
182
|
+
if (max !== void 0) actions.push(v.maxValue(max));
|
|
183
|
+
return actions.length > 0 ? v.pipe(v.bigint(), ...actions) : v.bigint();
|
|
184
|
+
}
|
|
185
|
+
function pgArrayColumnToSchema(column, dimensions) {
|
|
186
|
+
const [baseType, baseConstraint] = column.dataType.split(" ");
|
|
187
|
+
let baseSchema;
|
|
188
|
+
switch (baseType) {
|
|
189
|
+
case "number":
|
|
190
|
+
baseSchema = numberColumnToSchema(column, baseConstraint);
|
|
191
|
+
break;
|
|
192
|
+
case "bigint":
|
|
193
|
+
baseSchema = bigintColumnToSchema(column, baseConstraint);
|
|
194
|
+
break;
|
|
195
|
+
case "boolean":
|
|
196
|
+
baseSchema = v.boolean();
|
|
197
|
+
break;
|
|
198
|
+
case "string":
|
|
199
|
+
baseSchema = stringColumnToSchema(column, baseConstraint);
|
|
200
|
+
break;
|
|
201
|
+
case "object":
|
|
202
|
+
baseSchema = objectColumnToSchema(column, baseConstraint);
|
|
203
|
+
break;
|
|
204
|
+
case "array":
|
|
205
|
+
baseSchema = arrayColumnToSchema(column, baseConstraint);
|
|
206
|
+
break;
|
|
207
|
+
default: baseSchema = v.any();
|
|
208
|
+
}
|
|
209
|
+
let schema = v.array(baseSchema);
|
|
210
|
+
for (let i = 1; i < dimensions; i++) schema = v.array(schema);
|
|
211
|
+
return schema;
|
|
210
212
|
}
|
|
211
213
|
function arrayColumnToSchema(column, constraint) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
}
|
|
242
|
-
default: {
|
|
243
|
-
return v.array(v.any());
|
|
244
|
-
}
|
|
245
|
-
}
|
|
214
|
+
switch (constraint) {
|
|
215
|
+
case "geometry":
|
|
216
|
+
case "point": return v.tuple([v.number(), v.number()]);
|
|
217
|
+
case "line": return v.tuple([
|
|
218
|
+
v.number(),
|
|
219
|
+
v.number(),
|
|
220
|
+
v.number()
|
|
221
|
+
]);
|
|
222
|
+
case "vector":
|
|
223
|
+
case "halfvector": {
|
|
224
|
+
const { length } = column;
|
|
225
|
+
return length ? v.pipe(v.array(v.number()), v.length(length)) : v.array(v.number());
|
|
226
|
+
}
|
|
227
|
+
case "int64vector": {
|
|
228
|
+
const length = column.length;
|
|
229
|
+
return length ? v.pipe(v.array(v.pipe(v.bigint(), v.minValue(CONSTANTS.INT64_MIN), v.maxValue(CONSTANTS.INT64_MAX))), v.length(length)) : v.array(v.pipe(v.bigint(), v.minValue(CONSTANTS.INT64_MIN), v.maxValue(CONSTANTS.INT64_MAX)));
|
|
230
|
+
}
|
|
231
|
+
case "basecolumn": {
|
|
232
|
+
const baseColumn = column.baseColumn;
|
|
233
|
+
if (baseColumn) {
|
|
234
|
+
const { length } = column;
|
|
235
|
+
const schema = v.array(columnToSchema(baseColumn));
|
|
236
|
+
if (length) return v.pipe(schema, v.length(length));
|
|
237
|
+
return schema;
|
|
238
|
+
}
|
|
239
|
+
return v.array(v.any());
|
|
240
|
+
}
|
|
241
|
+
default: return v.array(v.any());
|
|
242
|
+
}
|
|
246
243
|
}
|
|
247
244
|
function objectColumnToSchema(column, constraint) {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
}
|
|
265
|
-
case 'line': {
|
|
266
|
-
return v.object({
|
|
267
|
-
a: v.number(),
|
|
268
|
-
b: v.number(),
|
|
269
|
-
c: v.number(),
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
default: {
|
|
273
|
-
return v.looseObject({});
|
|
274
|
-
}
|
|
275
|
-
}
|
|
245
|
+
switch (constraint) {
|
|
246
|
+
case "buffer": return bufferSchema;
|
|
247
|
+
case "date": return v.date();
|
|
248
|
+
case "geometry":
|
|
249
|
+
case "point": return v.object({
|
|
250
|
+
x: v.number(),
|
|
251
|
+
y: v.number()
|
|
252
|
+
});
|
|
253
|
+
case "json": return jsonSchema;
|
|
254
|
+
case "line": return v.object({
|
|
255
|
+
a: v.number(),
|
|
256
|
+
b: v.number(),
|
|
257
|
+
c: v.number()
|
|
258
|
+
});
|
|
259
|
+
default: return v.looseObject({});
|
|
260
|
+
}
|
|
276
261
|
}
|
|
277
262
|
function stringColumnToSchema(column, constraint) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
}
|
|
295
|
-
if (constraint === 'uint64') {
|
|
296
|
-
return unsignedBigintStringModeSchema;
|
|
297
|
-
}
|
|
298
|
-
const actions = [];
|
|
299
|
-
if (regex) {
|
|
300
|
-
actions.push(v.regex(regex));
|
|
301
|
-
}
|
|
302
|
-
if (length && isLengthExact) {
|
|
303
|
-
actions.push(v.length(length));
|
|
304
|
-
}
|
|
305
|
-
else if (length) {
|
|
306
|
-
actions.push(v.maxLength(length));
|
|
307
|
-
}
|
|
308
|
-
return actions.length > 0 ? v.pipe(v.string(), ...actions) : v.string();
|
|
263
|
+
const { name: columnName, length, isLengthExact } = column;
|
|
264
|
+
let regex;
|
|
265
|
+
if (constraint === "binary") regex = /^[01]*$/;
|
|
266
|
+
if (constraint === "uuid") return v.pipe(v.string(), v.uuid());
|
|
267
|
+
if (constraint === "enum") {
|
|
268
|
+
const enumValues = column.enumValues;
|
|
269
|
+
if (!enumValues) throw new Error(`Column "${getTableName(getColumnTable(column))}"."${columnName}" is of 'enum' type, but lacks enum values`);
|
|
270
|
+
return v.enum(mapEnumValues(enumValues));
|
|
271
|
+
}
|
|
272
|
+
if (constraint === "int64") return bigintStringModeSchema;
|
|
273
|
+
if (constraint === "uint64") return unsignedBigintStringModeSchema;
|
|
274
|
+
const actions = [];
|
|
275
|
+
if (regex) actions.push(v.regex(regex));
|
|
276
|
+
if (length && isLengthExact) actions.push(v.length(length));
|
|
277
|
+
else if (length) actions.push(v.maxLength(length));
|
|
278
|
+
return actions.length > 0 ? v.pipe(v.string(), ...actions) : v.string();
|
|
309
279
|
}
|
|
310
280
|
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/utils.ts
|
|
311
283
|
function isColumnType(column, columnTypes) {
|
|
312
|
-
|
|
284
|
+
return columnTypes.includes(column.columnType);
|
|
313
285
|
}
|
|
314
286
|
function isWithEnum(column) {
|
|
315
|
-
|
|
287
|
+
return "enumValues" in column && Array.isArray(column.enumValues) && column.enumValues.length > 0;
|
|
316
288
|
}
|
|
317
289
|
const isPgEnum = isWithEnum;
|
|
318
290
|
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region src/schema.ts
|
|
319
293
|
function getColumns(tableLike) {
|
|
320
|
-
|
|
294
|
+
return isTable(tableLike) ? getTableColumns(tableLike) : getViewSelectedFields(tableLike);
|
|
321
295
|
}
|
|
322
296
|
function handleColumns(columns, refinements, conditions) {
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
if (conditions.nullable(column)) {
|
|
346
|
-
columnSchemas[key] = v.nullable(columnSchemas[key]);
|
|
347
|
-
}
|
|
348
|
-
if (conditions.optional(column)) {
|
|
349
|
-
columnSchemas[key] = v.optional(columnSchemas[key]);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
return v.object(columnSchemas);
|
|
297
|
+
const columnSchemas = {};
|
|
298
|
+
for (const [key, selected] of Object.entries(columns)) {
|
|
299
|
+
if (!is(selected, Column) && !is(selected, SQL) && !is(selected, SQL.Aliased) && typeof selected === "object") {
|
|
300
|
+
columnSchemas[key] = handleColumns(isTable(selected) || isView(selected) ? getColumns(selected) : selected, refinements[key] ?? {}, conditions);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
const refinement = refinements[key];
|
|
304
|
+
if (refinement !== void 0 && typeof refinement !== "function") {
|
|
305
|
+
columnSchemas[key] = refinement;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const column = is(selected, Column) ? selected : void 0;
|
|
309
|
+
const schema = column ? columnToSchema(column) : v.any();
|
|
310
|
+
const refined = typeof refinement === "function" ? refinement(schema) : schema;
|
|
311
|
+
if (conditions.never(column)) continue;
|
|
312
|
+
else columnSchemas[key] = refined;
|
|
313
|
+
if (column) {
|
|
314
|
+
if (conditions.nullable(column)) columnSchemas[key] = v.nullable(columnSchemas[key]);
|
|
315
|
+
if (conditions.optional(column)) columnSchemas[key] = v.optional(columnSchemas[key]);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return v.object(columnSchemas);
|
|
354
319
|
}
|
|
355
320
|
const createSelectSchema = (entity, refine) => {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
optional: () => false,
|
|
363
|
-
nullable: (column) => !column.notNull,
|
|
364
|
-
});
|
|
321
|
+
if (isPgEnum(entity)) return v.enum(mapEnumValues(entity.enumValues));
|
|
322
|
+
return handleColumns(getColumns(entity), refine ?? {}, {
|
|
323
|
+
never: () => false,
|
|
324
|
+
optional: () => false,
|
|
325
|
+
nullable: (column) => !column.notNull
|
|
326
|
+
});
|
|
365
327
|
};
|
|
366
328
|
const createInsertSchema = (entity, refine) => {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
nullable: (column) => !column.notNull,
|
|
373
|
-
});
|
|
329
|
+
return handleColumns(getColumns(entity), refine ?? {}, {
|
|
330
|
+
never: (column) => column?.generated?.type === "always" || column?.generatedIdentity?.type === "always" || "identity" in (column ?? {}) && typeof column?.identity !== "undefined",
|
|
331
|
+
optional: (column) => !column.notNull || column.notNull && column.hasDefault,
|
|
332
|
+
nullable: (column) => !column.notNull
|
|
333
|
+
});
|
|
374
334
|
};
|
|
375
335
|
const createUpdateSchema = (entity, refine) => {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
nullable: (column) => !column.notNull,
|
|
382
|
-
});
|
|
336
|
+
return handleColumns(getColumns(entity), refine ?? {}, {
|
|
337
|
+
never: (column) => column?.generated?.type === "always" || column?.generatedIdentity?.type === "always" || "identity" in (column ?? {}) && typeof column?.identity !== "undefined",
|
|
338
|
+
optional: () => true,
|
|
339
|
+
nullable: (column) => !column.notNull
|
|
340
|
+
});
|
|
383
341
|
};
|
|
384
342
|
|
|
343
|
+
//#endregion
|
|
385
344
|
export { bufferSchema, createInsertSchema, createSelectSchema, createUpdateSchema, isColumnType, isPgEnum, isWithEnum, jsonSchema, literalSchema };
|
|
386
|
-
//# sourceMappingURL=index.mjs.map
|
|
345
|
+
//# sourceMappingURL=index.mjs.map
|