drizzle-valibot 1.0.0-beta.6-9514357 → 1.0.0-beta.6-a679d20
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 +330 -408
- 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 +302 -387
- 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,430 +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
|
-
schema = stringColumnToSchema(column, constraint);
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
case 'custom': {
|
|
69
|
-
schema = v.any();
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
default: {
|
|
73
|
-
schema = v.any();
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
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;
|
|
77
73
|
}
|
|
78
74
|
function numberColumnToSchema(column, constraint) {
|
|
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
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
integer = true;
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
case 'unsigned': {
|
|
170
|
-
min = 0;
|
|
171
|
-
max = Number.MAX_SAFE_INTEGER;
|
|
172
|
-
break;
|
|
173
|
-
}
|
|
174
|
-
default: {
|
|
175
|
-
min = Number.MIN_SAFE_INTEGER;
|
|
176
|
-
max = Number.MAX_SAFE_INTEGER;
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
const actions = [v.minValue(min), v.maxValue(max)];
|
|
181
|
-
if (integer) {
|
|
182
|
-
actions.push(v.integer());
|
|
183
|
-
}
|
|
184
|
-
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);
|
|
185
162
|
}
|
|
186
163
|
/** @internal */
|
|
187
|
-
const bigintStringModeSchema = v.pipe(v.string(), v.regex(/^-?\d+$/),
|
|
188
|
-
// eslint-disable-next-line unicorn/prefer-native-coercion-functions
|
|
189
|
-
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()));
|
|
190
165
|
/** @internal */
|
|
191
|
-
const unsignedBigintStringModeSchema = v.pipe(v.string(), v.regex(/^\d+$/),
|
|
192
|
-
// eslint-disable-next-line unicorn/prefer-native-coercion-functions
|
|
193
|
-
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()));
|
|
194
167
|
function bigintColumnToSchema(column, constraint) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
actions.push(v.minValue(min));
|
|
212
|
-
if (max !== undefined)
|
|
213
|
-
actions.push(v.maxValue(max));
|
|
214
|
-
return actions.length > 0 ? v.pipe(v.bigint(), ...actions) : v.bigint();
|
|
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();
|
|
215
184
|
}
|
|
216
185
|
function pgArrayColumnToSchema(column, dimensions) {
|
|
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
|
-
|
|
243
|
-
}
|
|
244
|
-
// Wrap in arrays based on dimensions
|
|
245
|
-
// Note: For PG arrays, column.length is the base type's length (e.g., varchar(10)), not array size
|
|
246
|
-
let schema = v.array(baseSchema);
|
|
247
|
-
for (let i = 1; i < dimensions; i++) {
|
|
248
|
-
schema = v.array(schema);
|
|
249
|
-
}
|
|
250
|
-
return schema;
|
|
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;
|
|
251
212
|
}
|
|
252
213
|
function arrayColumnToSchema(column, constraint) {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
return schema;
|
|
283
|
-
}
|
|
284
|
-
return v.array(v.any());
|
|
285
|
-
}
|
|
286
|
-
default: {
|
|
287
|
-
return v.array(v.any());
|
|
288
|
-
}
|
|
289
|
-
}
|
|
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
|
+
}
|
|
290
243
|
}
|
|
291
244
|
function objectColumnToSchema(column, constraint) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
}
|
|
309
|
-
case 'line': {
|
|
310
|
-
return v.object({
|
|
311
|
-
a: v.number(),
|
|
312
|
-
b: v.number(),
|
|
313
|
-
c: v.number(),
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
default: {
|
|
317
|
-
return v.looseObject({});
|
|
318
|
-
}
|
|
319
|
-
}
|
|
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
|
+
}
|
|
320
261
|
}
|
|
321
262
|
function stringColumnToSchema(column, constraint) {
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
339
|
-
if (constraint === 'uint64') {
|
|
340
|
-
return unsignedBigintStringModeSchema;
|
|
341
|
-
}
|
|
342
|
-
const actions = [];
|
|
343
|
-
if (regex) {
|
|
344
|
-
actions.push(v.regex(regex));
|
|
345
|
-
}
|
|
346
|
-
if (length && isLengthExact) {
|
|
347
|
-
actions.push(v.length(length));
|
|
348
|
-
}
|
|
349
|
-
else if (length) {
|
|
350
|
-
actions.push(v.maxLength(length));
|
|
351
|
-
}
|
|
352
|
-
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();
|
|
353
279
|
}
|
|
354
280
|
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/utils.ts
|
|
355
283
|
function isColumnType(column, columnTypes) {
|
|
356
|
-
|
|
284
|
+
return columnTypes.includes(column.columnType);
|
|
357
285
|
}
|
|
358
286
|
function isWithEnum(column) {
|
|
359
|
-
|
|
287
|
+
return "enumValues" in column && Array.isArray(column.enumValues) && column.enumValues.length > 0;
|
|
360
288
|
}
|
|
361
289
|
const isPgEnum = isWithEnum;
|
|
362
290
|
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region src/schema.ts
|
|
363
293
|
function getColumns(tableLike) {
|
|
364
|
-
|
|
294
|
+
return isTable(tableLike) ? getTableColumns(tableLike) : getViewSelectedFields(tableLike);
|
|
365
295
|
}
|
|
366
296
|
function handleColumns(columns, refinements, conditions) {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
if (conditions.nullable(column)) {
|
|
390
|
-
columnSchemas[key] = v.nullable(columnSchemas[key]);
|
|
391
|
-
}
|
|
392
|
-
if (conditions.optional(column)) {
|
|
393
|
-
columnSchemas[key] = v.optional(columnSchemas[key]);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
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);
|
|
398
319
|
}
|
|
399
320
|
const createSelectSchema = (entity, refine) => {
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
optional: () => false,
|
|
407
|
-
nullable: (column) => !column.notNull,
|
|
408
|
-
});
|
|
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
|
+
});
|
|
409
327
|
};
|
|
410
328
|
const createInsertSchema = (entity, refine) => {
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
nullable: (column) => !column.notNull,
|
|
417
|
-
});
|
|
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
|
+
});
|
|
418
334
|
};
|
|
419
335
|
const createUpdateSchema = (entity, refine) => {
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
nullable: (column) => !column.notNull,
|
|
426
|
-
});
|
|
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
|
+
});
|
|
427
341
|
};
|
|
428
342
|
|
|
343
|
+
//#endregion
|
|
429
344
|
export { bufferSchema, createInsertSchema, createSelectSchema, createUpdateSchema, isColumnType, isPgEnum, isWithEnum, jsonSchema, literalSchema };
|
|
430
|
-
//# sourceMappingURL=index.mjs.map
|
|
345
|
+
//# sourceMappingURL=index.mjs.map
|