drizzle-valibot 1.0.0-beta.6-cd45bda → 1.0.0-beta.6-a42e15a

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