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