drizzle-valibot 1.0.0-beta.6-7a73126 → 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.mjs CHANGED
@@ -1,430 +1,345 @@
1
- import { extractExtendedColumnType, getTableName, getColumnTable, isTable, getTableColumns, getViewSelectedFields, is, Column, SQL, isView } from 'drizzle-orm';
2
- import * as v from 'valibot';
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
- INT8_MIN: -128,
6
- INT8_MAX: 127,
7
- INT8_UNSIGNED_MAX: 255,
8
- INT16_MIN: -32768,
9
- INT16_MAX: 32767,
10
- INT16_UNSIGNED_MAX: 65535,
11
- INT24_MIN: -8388608,
12
- INT24_MAX: 8388607,
13
- INT24_UNSIGNED_MAX: 16777215,
14
- INT32_MIN: -2147483648,
15
- INT32_MAX: 2147483647,
16
- INT32_UNSIGNED_MAX: 4294967295,
17
- INT48_MIN: -140737488355328,
18
- INT48_MAX: 140737488355327,
19
- INT48_UNSIGNED_MAX: 281474976710655,
20
- INT64_MIN: -9223372036854775808n,
21
- INT64_MAX: 9223372036854775807n,
22
- INT64_UNSIGNED_MAX: 18446744073709551615n,
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
- const literalSchema = v.union([v.string(), v.number(), v.boolean(), v.null()]);
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
- literalSchema,
28
- v.array(v.any()),
29
- v.record(v.string(), v.any()),
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
- return Object.fromEntries(values.map((value) => [value, value]));
41
+ return Object.fromEntries(values.map((value) => [value, value]));
34
42
  }
35
43
  function columnToSchema(column) {
36
- let schema;
37
- // Check for PG array columns (have dimensions property instead of changing dataType)
38
- const dimensions = column.dimensions;
39
- if (typeof dimensions === 'number' && dimensions > 0) {
40
- return pgArrayColumnToSchema(column, dimensions);
41
- }
42
- const { type, constraint } = extractExtendedColumnType(column);
43
- switch (type) {
44
- case 'array': {
45
- schema = arrayColumnToSchema(column, constraint);
46
- break;
47
- }
48
- case 'object': {
49
- schema = objectColumnToSchema(column, constraint);
50
- break;
51
- }
52
- case 'number': {
53
- schema = numberColumnToSchema(column, constraint);
54
- break;
55
- }
56
- case 'bigint': {
57
- schema = bigintColumnToSchema(column, constraint);
58
- break;
59
- }
60
- case 'boolean': {
61
- schema = v.boolean();
62
- break;
63
- }
64
- case 'string': {
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
- let min;
80
- let max;
81
- let integer = false;
82
- switch (constraint) {
83
- case 'int8': {
84
- min = CONSTANTS.INT8_MIN;
85
- max = CONSTANTS.INT8_MAX;
86
- integer = true;
87
- break;
88
- }
89
- case 'uint8': {
90
- min = 0;
91
- max = CONSTANTS.INT8_UNSIGNED_MAX;
92
- integer = true;
93
- break;
94
- }
95
- case 'int16': {
96
- min = CONSTANTS.INT16_MIN;
97
- max = CONSTANTS.INT16_MAX;
98
- integer = true;
99
- break;
100
- }
101
- case 'uint16': {
102
- min = 0;
103
- max = CONSTANTS.INT16_UNSIGNED_MAX;
104
- integer = true;
105
- break;
106
- }
107
- case 'int24': {
108
- min = CONSTANTS.INT24_MIN;
109
- max = CONSTANTS.INT24_MAX;
110
- integer = true;
111
- break;
112
- }
113
- case 'uint24': {
114
- min = 0;
115
- max = CONSTANTS.INT24_UNSIGNED_MAX;
116
- integer = true;
117
- break;
118
- }
119
- case 'int32': {
120
- min = CONSTANTS.INT32_MIN;
121
- max = CONSTANTS.INT32_MAX;
122
- integer = true;
123
- break;
124
- }
125
- case 'uint32': {
126
- min = 0;
127
- max = CONSTANTS.INT32_UNSIGNED_MAX;
128
- integer = true;
129
- break;
130
- }
131
- case 'int53': {
132
- min = Number.MIN_SAFE_INTEGER;
133
- max = Number.MAX_SAFE_INTEGER;
134
- integer = true;
135
- break;
136
- }
137
- case 'uint53': {
138
- min = 0;
139
- max = Number.MAX_SAFE_INTEGER;
140
- integer = true;
141
- break;
142
- }
143
- case 'float': {
144
- min = CONSTANTS.INT24_MIN;
145
- max = CONSTANTS.INT24_MAX;
146
- break;
147
- }
148
- case 'ufloat': {
149
- min = 0;
150
- max = CONSTANTS.INT24_UNSIGNED_MAX;
151
- break;
152
- }
153
- case 'double': {
154
- min = CONSTANTS.INT48_MIN;
155
- max = CONSTANTS.INT48_MAX;
156
- break;
157
- }
158
- case 'udouble': {
159
- min = 0;
160
- max = CONSTANTS.INT48_UNSIGNED_MAX;
161
- break;
162
- }
163
- case 'year': {
164
- min = 1901;
165
- max = 2155;
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
- let min;
196
- let max;
197
- switch (constraint) {
198
- case 'int64': {
199
- min = CONSTANTS.INT64_MIN;
200
- max = CONSTANTS.INT64_MAX;
201
- break;
202
- }
203
- case 'uint64': {
204
- min = 0n;
205
- max = CONSTANTS.INT64_UNSIGNED_MAX;
206
- break;
207
- }
208
- }
209
- const actions = [];
210
- if (min !== undefined)
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
- // PG style: the column IS the base type, with dimensions indicating array depth
218
- // Get the base schema from the column's own dataType
219
- const [baseType, baseConstraint] = column.dataType.split(' ');
220
- let baseSchema;
221
- switch (baseType) {
222
- case 'number':
223
- baseSchema = numberColumnToSchema(column, baseConstraint);
224
- break;
225
- case 'bigint':
226
- baseSchema = bigintColumnToSchema(column, baseConstraint);
227
- break;
228
- case 'boolean':
229
- baseSchema = v.boolean();
230
- break;
231
- case 'string':
232
- baseSchema = stringColumnToSchema(column, baseConstraint);
233
- break;
234
- case 'object':
235
- baseSchema = objectColumnToSchema(column, baseConstraint);
236
- break;
237
- case 'array':
238
- // Handle array types like point, line, etc.
239
- baseSchema = arrayColumnToSchema(column, baseConstraint);
240
- break;
241
- default:
242
- baseSchema = v.any();
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
- switch (constraint) {
254
- case 'geometry':
255
- case 'point': {
256
- return v.tuple([v.number(), v.number()]);
257
- }
258
- case 'line': {
259
- return v.tuple([v.number(), v.number(), v.number()]);
260
- }
261
- case 'vector':
262
- case 'halfvector': {
263
- const { length } = column;
264
- return length
265
- ? v.pipe(v.array(v.number()), v.length(length))
266
- : v.array(v.number());
267
- }
268
- case 'int64vector': {
269
- const length = column.length;
270
- return length
271
- ? v.pipe(v.array(v.pipe(v.bigint(), v.minValue(CONSTANTS.INT64_MIN), v.maxValue(CONSTANTS.INT64_MAX))), v.length(length))
272
- : v.array(v.pipe(v.bigint(), v.minValue(CONSTANTS.INT64_MIN), v.maxValue(CONSTANTS.INT64_MAX)));
273
- }
274
- case 'basecolumn': {
275
- // CockroachDB/GEL style: has a separate baseColumn
276
- const baseColumn = column.baseColumn;
277
- if (baseColumn) {
278
- const { length } = column;
279
- const schema = v.array(columnToSchema(baseColumn));
280
- if (length)
281
- return v.pipe(schema, v.length(length));
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
- switch (constraint) {
293
- case 'buffer': {
294
- return bufferSchema;
295
- }
296
- case 'date': {
297
- return v.date();
298
- }
299
- case 'geometry':
300
- case 'point': {
301
- return v.object({
302
- x: v.number(),
303
- y: v.number(),
304
- });
305
- }
306
- case 'json': {
307
- return jsonSchema;
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
- const { name: columnName, length, isLengthExact } = column;
323
- let regex;
324
- if (constraint === 'binary') {
325
- regex = /^[01]*$/;
326
- }
327
- if (constraint === 'uuid')
328
- return v.pipe(v.string(), v.uuid());
329
- if (constraint === 'enum') {
330
- const enumValues = column.enumValues;
331
- if (!enumValues) {
332
- throw new Error(`Column "${getTableName(getColumnTable(column))}"."${columnName}" is of 'enum' type, but lacks enum values`);
333
- }
334
- return v.enum(mapEnumValues(enumValues));
335
- }
336
- if (constraint === 'int64') {
337
- return bigintStringModeSchema;
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
- return columnTypes.includes(column.columnType);
284
+ return columnTypes.includes(column.columnType);
357
285
  }
358
286
  function isWithEnum(column) {
359
- return 'enumValues' in column && Array.isArray(column.enumValues) && column.enumValues.length > 0;
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
- return isTable(tableLike) ? getTableColumns(tableLike) : getViewSelectedFields(tableLike);
294
+ return isTable(tableLike) ? getTableColumns(tableLike) : getViewSelectedFields(tableLike);
365
295
  }
366
296
  function handleColumns(columns, refinements, conditions) {
367
- const columnSchemas = {};
368
- for (const [key, selected] of Object.entries(columns)) {
369
- if (!is(selected, Column) && !is(selected, SQL) && !is(selected, SQL.Aliased) && typeof selected === 'object') {
370
- const columns = isTable(selected) || isView(selected) ? getColumns(selected) : selected;
371
- columnSchemas[key] = handleColumns(columns, refinements[key] ?? {}, conditions);
372
- continue;
373
- }
374
- const refinement = refinements[key];
375
- if (refinement !== undefined && typeof refinement !== 'function') {
376
- columnSchemas[key] = refinement;
377
- continue;
378
- }
379
- const column = is(selected, Column) ? selected : undefined;
380
- const schema = column ? columnToSchema(column) : v.any();
381
- const refined = typeof refinement === 'function' ? refinement(schema) : schema;
382
- if (conditions.never(column)) {
383
- continue;
384
- }
385
- else {
386
- columnSchemas[key] = refined;
387
- }
388
- if (column) {
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
- if (isPgEnum(entity)) {
401
- return v.enum(mapEnumValues(entity.enumValues));
402
- }
403
- const columns = getColumns(entity);
404
- return handleColumns(columns, refine ?? {}, {
405
- never: () => false,
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
- const columns = getColumns(entity);
412
- return handleColumns(columns, refine ?? {}, {
413
- never: (column) => column?.generated?.type === 'always' || column?.generatedIdentity?.type === 'always'
414
- || ('identity' in (column ?? {}) && typeof column?.identity !== 'undefined'),
415
- optional: (column) => !column.notNull || (column.notNull && column.hasDefault),
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
- const columns = getColumns(entity);
421
- return handleColumns(columns, refine ?? {}, {
422
- never: (column) => column?.generated?.type === 'always' || column?.generatedIdentity?.type === 'always'
423
- || ('identity' in (column ?? {}) && typeof column?.identity !== 'undefined'),
424
- optional: () => true,
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