@opensaas/stack-core 0.44.0-alpha.5 → 0.44.0-alpha.6
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/CHANGELOG.md +396 -0
- package/dist/.build-hash +1 -1
- package/dist/access/field-access.d.ts +22 -0
- package/dist/access/field-access.d.ts.map +1 -1
- package/dist/access/field-access.js +112 -69
- package/dist/access/field-access.js.map +1 -1
- package/dist/access/field-visibility.d.ts +1 -48
- package/dist/access/field-visibility.d.ts.map +1 -1
- package/dist/access/field-visibility.js +12 -29
- package/dist/access/field-visibility.js.map +1 -1
- package/dist/access/index.d.ts +3 -1
- package/dist/access/index.d.ts.map +1 -1
- package/dist/access/index.js +3 -1
- package/dist/access/index.js.map +1 -1
- package/dist/access/rule-item.d.ts +42 -0
- package/dist/access/rule-item.d.ts.map +1 -0
- package/dist/access/rule-item.js +215 -0
- package/dist/access/rule-item.js.map +1 -0
- package/dist/access/types.d.ts +17 -0
- package/dist/access/types.d.ts.map +1 -1
- package/dist/config/has-access-gate.d.ts +20 -0
- package/dist/config/has-access-gate.d.ts.map +1 -0
- package/dist/config/has-access-gate.js +26 -0
- package/dist/config/has-access-gate.js.map +1 -0
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +7 -2
- package/dist/config/index.js.map +1 -1
- package/dist/config/list-view-keys.d.ts +5 -0
- package/dist/config/list-view-keys.d.ts.map +1 -0
- package/dist/config/list-view-keys.js +18 -0
- package/dist/config/list-view-keys.js.map +1 -0
- package/dist/config/preset-views.d.ts +22 -0
- package/dist/config/preset-views.d.ts.map +1 -0
- package/dist/config/preset-views.js +46 -0
- package/dist/config/preset-views.js.map +1 -0
- package/dist/config/row-action-availability.d.ts +4 -0
- package/dist/config/row-action-availability.d.ts.map +1 -0
- package/dist/config/row-action-availability.js +20 -0
- package/dist/config/row-action-availability.js.map +1 -0
- package/dist/config/types.d.ts +268 -6
- package/dist/config/types.d.ts.map +1 -1
- package/dist/context/hook-pipeline.d.ts +5 -1
- package/dist/context/hook-pipeline.d.ts.map +1 -1
- package/dist/context/hook-pipeline.js +4 -4
- package/dist/context/hook-pipeline.js.map +1 -1
- package/dist/context/index.d.ts +5 -0
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +64 -13
- package/dist/context/index.js.map +1 -1
- package/dist/context/write-pipeline.d.ts.map +1 -1
- package/dist/context/write-pipeline.js +35 -15
- package/dist/context/write-pipeline.js.map +1 -1
- package/dist/contract/fixtures/contract-configs.d.ts +5 -0
- package/dist/contract/fixtures/contract-configs.d.ts.map +1 -1
- package/dist/contract/fixtures/contract-configs.js +21 -0
- package/dist/contract/fixtures/contract-configs.js.map +1 -1
- package/dist/contract/prisma.js +2 -2
- package/dist/contract/prisma.js.map +1 -1
- package/dist/extend.d.ts +1 -1
- package/dist/extend.d.ts.map +1 -1
- package/dist/fields/index.d.ts +12 -11
- package/dist/fields/index.d.ts.map +1 -1
- package/dist/fields/index.js +197 -62
- package/dist/fields/index.js.map +1 -1
- package/dist/filter/collect.d.ts +29 -0
- package/dist/filter/collect.d.ts.map +1 -1
- package/dist/filter/collect.js +100 -9
- package/dist/filter/collect.js.map +1 -1
- package/dist/filter/index.d.ts +7 -5
- package/dist/filter/index.d.ts.map +1 -1
- package/dist/filter/index.js +6 -14
- package/dist/filter/index.js.map +1 -1
- package/dist/filter/map.d.ts +108 -20
- package/dist/filter/map.d.ts.map +1 -1
- package/dist/filter/map.js +235 -49
- package/dist/filter/map.js.map +1 -1
- package/dist/filter/parse.d.ts +70 -26
- package/dist/filter/parse.d.ts.map +1 -1
- package/dist/filter/parse.js +212 -79
- package/dist/filter/parse.js.map +1 -1
- package/dist/filter/record-id.d.ts +19 -0
- package/dist/filter/record-id.d.ts.map +1 -0
- package/dist/filter/record-id.js +37 -0
- package/dist/filter/record-id.js.map +1 -0
- package/dist/filter/serialize.d.ts +45 -19
- package/dist/filter/serialize.d.ts.map +1 -1
- package/dist/filter/serialize.js +146 -50
- package/dist/filter/serialize.js.map +1 -1
- package/dist/filter/types.d.ts +132 -21
- package/dist/filter/types.d.ts.map +1 -1
- package/dist/filter/types.js +2 -11
- package/dist/filter/types.js.map +1 -1
- package/dist/hooks/index.d.ts +2 -2
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +4 -3
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +7 -1
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +5 -4
- package/dist/internal.js.map +1 -1
- package/dist/lib/database-errors.d.ts +2 -1
- package/dist/lib/database-errors.d.ts.map +1 -1
- package/dist/lib/database-errors.js +5 -4
- package/dist/lib/database-errors.js.map +1 -1
- package/dist/mcp/field-schema.d.ts.map +1 -1
- package/dist/mcp/field-schema.js +13 -2
- package/dist/mcp/field-schema.js.map +1 -1
- package/dist/mcp/handler.d.ts.map +1 -1
- package/dist/mcp/handler.js +68 -19
- package/dist/mcp/handler.js.map +1 -1
- package/dist/mcp/projection.d.ts +6 -5
- package/dist/mcp/projection.d.ts.map +1 -1
- package/dist/mcp/projection.js +11 -8
- package/dist/mcp/projection.js.map +1 -1
- package/dist/mcp/reachability.d.ts +9 -0
- package/dist/mcp/reachability.d.ts.map +1 -0
- package/dist/mcp/reachability.js +32 -0
- package/dist/mcp/reachability.js.map +1 -0
- package/dist/mcp/where-reachability.d.ts +8 -0
- package/dist/mcp/where-reachability.d.ts.map +1 -0
- package/dist/mcp/where-reachability.js +72 -0
- package/dist/mcp/where-reachability.js.map +1 -0
- package/dist/query/relationship-options.d.ts +6 -0
- package/dist/query/relationship-options.d.ts.map +1 -1
- package/dist/query/relationship-options.js +16 -3
- package/dist/query/relationship-options.js.map +1 -1
- package/dist/secured/include.d.ts +14 -1
- package/dist/secured/include.d.ts.map +1 -1
- package/dist/secured/include.js +11 -7
- package/dist/secured/include.js.map +1 -1
- package/dist/secured/lock.d.ts +8 -2
- package/dist/secured/lock.d.ts.map +1 -1
- package/dist/secured/lock.js +44 -15
- package/dist/secured/lock.js.map +1 -1
- package/dist/secured/lower.d.ts.map +1 -1
- package/dist/secured/lower.js +17 -4
- package/dist/secured/lower.js.map +1 -1
- package/dist/secured/read.d.ts +65 -7
- package/dist/secured/read.d.ts.map +1 -1
- package/dist/secured/read.js +115 -54
- package/dist/secured/read.js.map +1 -1
- package/dist/validation/config-refusal.d.ts +1 -1
- package/dist/validation/config-refusal.d.ts.map +1 -1
- package/dist/validation/database-config.d.ts +0 -15
- package/dist/validation/database-config.d.ts.map +1 -1
- package/dist/validation/database-config.js +32 -1
- package/dist/validation/database-config.js.map +1 -1
- package/dist/validation/relations.d.ts.map +1 -1
- package/dist/validation/relations.js +11 -0
- package/dist/validation/relations.js.map +1 -1
- package/dist/validation/schema.d.ts +3 -3
- package/dist/validation/schema.d.ts.map +1 -1
- package/dist/validation/schema.js +4 -4
- package/dist/validation/schema.js.map +1 -1
- package/package.json +9 -9
package/dist/fields/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { hashPassword, isHashedPassword, HashedPassword } from '../utils/password.js';
|
|
3
3
|
import { getLabelFieldName } from '../config/label.js';
|
|
4
|
+
import { parseListId } from '../contract/id-boundary.js';
|
|
4
5
|
function quoteStringLiteral(value) {
|
|
5
6
|
const escaped = value
|
|
6
7
|
.replace(/\\/g, '\\\\')
|
|
@@ -13,6 +14,36 @@ function quoteStringLiteral(value) {
|
|
|
13
14
|
}
|
|
14
15
|
/** Operators shared by numeric/date fields' `getFilterSpec`. */
|
|
15
16
|
const COMPARISON_OPERATORS = ['eq', 'gt', 'gte', 'lt', 'lte'];
|
|
17
|
+
const WHOLE_NUMBER = { pattern: '^-?\\d+$', description: 'takes a whole number' };
|
|
18
|
+
const WHOLE_NUMBER_MATCH = new RegExp(WHOLE_NUMBER.pattern);
|
|
19
|
+
const DECIMAL_VALUE = {
|
|
20
|
+
pattern: '^-?\\d+(?:\\.\\d+)?$',
|
|
21
|
+
description: 'takes a number',
|
|
22
|
+
};
|
|
23
|
+
const DECIMAL_VALUE_MATCH = new RegExp(DECIMAL_VALUE.pattern);
|
|
24
|
+
const DATE_VALUE = {
|
|
25
|
+
pattern: '^\\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\\d|3[01])$',
|
|
26
|
+
description: 'takes a date, like 2026-01-31',
|
|
27
|
+
};
|
|
28
|
+
const DATE_VALUE_MATCH = new RegExp(DATE_VALUE.pattern);
|
|
29
|
+
const PRESENCE_COUNT = {
|
|
30
|
+
pattern: '^0$',
|
|
31
|
+
description: 'takes 0: “is 0” finds none, “greater than 0” finds any',
|
|
32
|
+
operators: { gte: { pattern: '^1$', description: 'takes 1 with “at least”, which finds any' } },
|
|
33
|
+
};
|
|
34
|
+
function formatMatcher(format) {
|
|
35
|
+
const base = new RegExp(format.pattern);
|
|
36
|
+
const byOperator = new Map();
|
|
37
|
+
for (const [operator, pattern] of Object.entries(format.operators ?? {})) {
|
|
38
|
+
if (pattern)
|
|
39
|
+
byOperator.set(operator, new RegExp(pattern.pattern));
|
|
40
|
+
}
|
|
41
|
+
return (operator, value) => (byOperator.get(operator) ?? base).test(value);
|
|
42
|
+
}
|
|
43
|
+
const PRESENCE_COUNT_MATCH = formatMatcher(PRESENCE_COUNT);
|
|
44
|
+
function emptyWhenNullable(fieldName, nullable) {
|
|
45
|
+
return nullable ? { [fieldName]: { equals: null } } : undefined;
|
|
46
|
+
}
|
|
16
47
|
function prismaComparisonKey(operator) {
|
|
17
48
|
return operator === 'eq' ? 'equals' : operator;
|
|
18
49
|
}
|
|
@@ -79,9 +110,37 @@ function fitsDecimal(value, precision, scale) {
|
|
|
79
110
|
const parts = decimalParts(text);
|
|
80
111
|
if (parts === null)
|
|
81
112
|
return false;
|
|
82
|
-
const
|
|
83
|
-
|
|
113
|
+
const fraction = parts[3] ?? '';
|
|
114
|
+
let scaled = BigInt(`${parts[2] || '0'}${fraction.slice(0, scale).padEnd(scale, '0')}`);
|
|
115
|
+
if (fraction.length > scale && fraction.charCodeAt(scale) >= 53)
|
|
116
|
+
scaled += 1n;
|
|
117
|
+
return (scaled === 0n ? 1 : scaled.toString().length) <= precision;
|
|
84
118
|
}
|
|
119
|
+
const INT32_MIN = -2147483648;
|
|
120
|
+
const INT32_MAX = 2147483647;
|
|
121
|
+
const INT64_MIN = -(2n ** 63n);
|
|
122
|
+
const INT64_MAX = 2n ** 63n - 1n;
|
|
123
|
+
const hasNul = (root) => {
|
|
124
|
+
const seen = new Set();
|
|
125
|
+
const pending = [root];
|
|
126
|
+
while (pending.length > 0) {
|
|
127
|
+
const value = pending.pop();
|
|
128
|
+
if (typeof value === 'string') {
|
|
129
|
+
if (value.includes('\u0000'))
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
else if (typeof value === 'object' && value !== null && !seen.has(value)) {
|
|
133
|
+
seen.add(value);
|
|
134
|
+
if (Array.isArray(value))
|
|
135
|
+
pending.push(...value);
|
|
136
|
+
else {
|
|
137
|
+
for (const [key, child] of Object.entries(value))
|
|
138
|
+
pending.push(key, child);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return false;
|
|
143
|
+
};
|
|
85
144
|
function isCalendarDate(value) {
|
|
86
145
|
if (typeof value !== 'string')
|
|
87
146
|
return false;
|
|
@@ -89,6 +148,8 @@ function isCalendarDate(value) {
|
|
|
89
148
|
if (match === null)
|
|
90
149
|
return false;
|
|
91
150
|
const [year, month, day] = [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
151
|
+
if (year < 1)
|
|
152
|
+
return false;
|
|
92
153
|
const date = new Date(0);
|
|
93
154
|
date.setUTCFullYear(year, month - 1, day);
|
|
94
155
|
return (date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day);
|
|
@@ -153,6 +214,7 @@ function scalarColumn(fieldName, column) {
|
|
|
153
214
|
* Text field
|
|
154
215
|
*/
|
|
155
216
|
export function text(options) {
|
|
217
|
+
const nullable = options?.db?.isNullable ?? !options?.validation?.isRequired;
|
|
156
218
|
const zodSchema = (fieldName, operation) => {
|
|
157
219
|
const validation = options?.validation;
|
|
158
220
|
const isRequired = validation?.isRequired;
|
|
@@ -174,17 +236,21 @@ export function text(options) {
|
|
|
174
236
|
message: `${formatFieldName(fieldName)} must be at most ${length.max} characters`,
|
|
175
237
|
})
|
|
176
238
|
: withMin;
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
|
|
239
|
+
const noNul = withMax.refine((value) => !value.includes('\u0000'), {
|
|
240
|
+
message: `${formatFieldName(fieldName)} must not contain NUL characters`,
|
|
241
|
+
});
|
|
242
|
+
if (isRequired && operation === 'update')
|
|
243
|
+
return noNul.optional();
|
|
244
|
+
if (isRequired)
|
|
245
|
+
return noNul;
|
|
246
|
+
return nullable ? noNul.optional().nullable() : noNul.optional();
|
|
181
247
|
};
|
|
182
248
|
return {
|
|
183
249
|
type: 'text',
|
|
184
250
|
...options,
|
|
251
|
+
nullable,
|
|
185
252
|
getZodSchema: zodSchema,
|
|
186
253
|
getContractField: (fieldName, listKey, config) => {
|
|
187
|
-
const nullable = options?.db?.isNullable ?? !options?.validation?.isRequired;
|
|
188
254
|
// Keystone 6 gives every non-null text column an implicit empty-string
|
|
189
255
|
// default; `db.keystoneCompat` mirrors that so a migrating project
|
|
190
256
|
// reaches parity without hand-setting `defaultValue: ''`.
|
|
@@ -216,6 +282,11 @@ export function text(options) {
|
|
|
216
282
|
operators: ['eq'],
|
|
217
283
|
freeText: true,
|
|
218
284
|
toCondition: (operator, value) => operator === 'eq' ? { [fieldName]: { contains: value } } : null,
|
|
285
|
+
empty: nullable
|
|
286
|
+
? { OR: [{ [fieldName]: { equals: null } }, { [fieldName]: { equals: '' } }] }
|
|
287
|
+
: options?.validation?.isRequired
|
|
288
|
+
? undefined
|
|
289
|
+
: { [fieldName]: { equals: '' } },
|
|
219
290
|
suggestions: { valueSource: { kind: 'none' } },
|
|
220
291
|
}),
|
|
221
292
|
};
|
|
@@ -228,9 +299,18 @@ export function integer(options) {
|
|
|
228
299
|
type: 'integer',
|
|
229
300
|
...options,
|
|
230
301
|
getZodSchema: (fieldName, operation) => {
|
|
231
|
-
const
|
|
232
|
-
message: `${formatFieldName(fieldName)} must be a number
|
|
233
|
-
|
|
302
|
+
const whole = z
|
|
303
|
+
.number({ message: `${formatFieldName(fieldName)} must be a number` })
|
|
304
|
+
.int({ message: `${formatFieldName(fieldName)} must be a whole number` });
|
|
305
|
+
const baseSchema = options?.db?.nativeType === undefined
|
|
306
|
+
? whole
|
|
307
|
+
.min(INT32_MIN, {
|
|
308
|
+
message: `${formatFieldName(fieldName)} is below the 32-bit range`,
|
|
309
|
+
})
|
|
310
|
+
.max(INT32_MAX, {
|
|
311
|
+
message: `${formatFieldName(fieldName)} is above the 32-bit range`,
|
|
312
|
+
})
|
|
313
|
+
: whole;
|
|
234
314
|
const withMin = options?.validation?.min !== undefined
|
|
235
315
|
? baseSchema.min(options.validation.min, {
|
|
236
316
|
message: `${formatFieldName(fieldName)} must be at least ${options.validation.min}`,
|
|
@@ -244,7 +324,11 @@ export function integer(options) {
|
|
|
244
324
|
const isRequired = options?.validation?.isRequired;
|
|
245
325
|
if (isRequired && operation === 'update')
|
|
246
326
|
return withMax.optional();
|
|
247
|
-
|
|
327
|
+
if (isRequired)
|
|
328
|
+
return withMax;
|
|
329
|
+
return (options?.db?.isNullable ?? !isRequired)
|
|
330
|
+
? withMax.optional().nullable()
|
|
331
|
+
: withMax.optional();
|
|
248
332
|
},
|
|
249
333
|
getContractField: (fieldName, listKey) => {
|
|
250
334
|
checkedDefault(options?.defaultValue, listKey, fieldName, 'an integer within the 32-bit range', (value) => typeof value === 'number' &&
|
|
@@ -265,11 +349,12 @@ export function integer(options) {
|
|
|
265
349
|
operators: COMPARISON_OPERATORS,
|
|
266
350
|
toCondition: (operator, value) => {
|
|
267
351
|
const trimmed = value.trim();
|
|
268
|
-
if (
|
|
352
|
+
if (!WHOLE_NUMBER_MATCH.test(trimmed))
|
|
269
353
|
return null;
|
|
270
354
|
return { [fieldName]: { [prismaComparisonKey(operator)]: Number(trimmed) } };
|
|
271
355
|
},
|
|
272
|
-
|
|
356
|
+
empty: emptyWhenNullable(fieldName, options?.db?.isNullable ?? !options?.validation?.isRequired),
|
|
357
|
+
suggestions: { valueSource: { kind: 'none' }, valueFormat: WHOLE_NUMBER },
|
|
273
358
|
}),
|
|
274
359
|
};
|
|
275
360
|
}
|
|
@@ -338,7 +423,9 @@ export function decimal(options) {
|
|
|
338
423
|
], {
|
|
339
424
|
message: `${formatFieldName(fieldName)} must be a decimal value`,
|
|
340
425
|
});
|
|
341
|
-
let schema = baseSchema
|
|
426
|
+
let schema = baseSchema.refine((val) => fitsDecimal(val, precision, scale), {
|
|
427
|
+
message: `${formatFieldName(fieldName)} must be a finite number with at most ${precision - scale} integer digits`,
|
|
428
|
+
});
|
|
342
429
|
if (options?.validation?.min !== undefined) {
|
|
343
430
|
const minValue = parseFloat(options.validation.min);
|
|
344
431
|
schema = schema.refine((val) => {
|
|
@@ -360,7 +447,11 @@ export function decimal(options) {
|
|
|
360
447
|
const isRequired = options?.validation?.isRequired;
|
|
361
448
|
if (isRequired && operation === 'update')
|
|
362
449
|
return schema.optional();
|
|
363
|
-
|
|
450
|
+
if (isRequired)
|
|
451
|
+
return schema;
|
|
452
|
+
return (options?.db?.isNullable ?? !isRequired)
|
|
453
|
+
? schema.optional().nullable()
|
|
454
|
+
: schema.optional();
|
|
364
455
|
},
|
|
365
456
|
getContractField: (fieldName, listKey) => {
|
|
366
457
|
const size = checkedDecimalSize(options?.precision ?? 18, options?.scale ?? 4, listKey, fieldName);
|
|
@@ -380,11 +471,12 @@ export function decimal(options) {
|
|
|
380
471
|
operators: COMPARISON_OPERATORS,
|
|
381
472
|
toCondition: (operator, value) => {
|
|
382
473
|
const trimmed = value.trim();
|
|
383
|
-
if (
|
|
474
|
+
if (!DECIMAL_VALUE_MATCH.test(trimmed))
|
|
384
475
|
return null;
|
|
385
476
|
return { [fieldName]: { [prismaComparisonKey(operator)]: trimmed } };
|
|
386
477
|
},
|
|
387
|
-
|
|
478
|
+
empty: emptyWhenNullable(fieldName, options?.db?.isNullable ?? !options?.validation?.isRequired),
|
|
479
|
+
suggestions: { valueSource: { kind: 'none' }, valueFormat: DECIMAL_VALUE },
|
|
388
480
|
}),
|
|
389
481
|
};
|
|
390
482
|
}
|
|
@@ -434,7 +526,9 @@ export function bigInt(options) {
|
|
|
434
526
|
}
|
|
435
527
|
return BigInt(trimmed);
|
|
436
528
|
});
|
|
437
|
-
let schema = coerced
|
|
529
|
+
let schema = coerced.refine((val) => val >= INT64_MIN && val <= INT64_MAX, {
|
|
530
|
+
message: `${formatFieldName(fieldName)} is outside the 64-bit range`,
|
|
531
|
+
});
|
|
438
532
|
if (validation?.min !== undefined) {
|
|
439
533
|
const min = validation.min;
|
|
440
534
|
schema = schema.refine((val) => val >= min, {
|
|
@@ -449,7 +543,11 @@ export function bigInt(options) {
|
|
|
449
543
|
}
|
|
450
544
|
if (isRequired && operation === 'update')
|
|
451
545
|
return schema.optional();
|
|
452
|
-
|
|
546
|
+
if (isRequired)
|
|
547
|
+
return schema;
|
|
548
|
+
return (options?.db?.isNullable ?? !isRequired)
|
|
549
|
+
? schema.optional().nullable()
|
|
550
|
+
: schema.optional();
|
|
453
551
|
},
|
|
454
552
|
getContractField: (fieldName, listKey) => {
|
|
455
553
|
const defaultValue = options?.defaultValue;
|
|
@@ -470,11 +568,12 @@ export function bigInt(options) {
|
|
|
470
568
|
operators: COMPARISON_OPERATORS,
|
|
471
569
|
toCondition: (operator, value) => {
|
|
472
570
|
const trimmed = value.trim();
|
|
473
|
-
if (
|
|
571
|
+
if (!WHOLE_NUMBER_MATCH.test(trimmed))
|
|
474
572
|
return null;
|
|
475
573
|
return { [fieldName]: { [prismaComparisonKey(operator)]: BigInt(trimmed) } };
|
|
476
574
|
},
|
|
477
|
-
|
|
575
|
+
empty: emptyWhenNullable(fieldName, options?.db?.isNullable ?? !options?.validation?.isRequired),
|
|
576
|
+
suggestions: { valueSource: { kind: 'none' }, valueFormat: WHOLE_NUMBER },
|
|
478
577
|
}),
|
|
479
578
|
};
|
|
480
579
|
}
|
|
@@ -486,8 +585,11 @@ export function checkbox(options) {
|
|
|
486
585
|
type: 'checkbox',
|
|
487
586
|
...options,
|
|
488
587
|
defaultValue: options?.defaultValue ?? false,
|
|
489
|
-
getZodSchema: () => {
|
|
490
|
-
|
|
588
|
+
getZodSchema: (fieldName) => {
|
|
589
|
+
const schema = z
|
|
590
|
+
.boolean({ message: `${formatFieldName(fieldName)} must be true or false` })
|
|
591
|
+
.optional();
|
|
592
|
+
return options?.db?.isNullable === true ? schema.nullable() : schema;
|
|
491
593
|
},
|
|
492
594
|
getContractField: (fieldName, listKey) => {
|
|
493
595
|
checkedDefault(options?.defaultValue, listKey, fieldName, 'a boolean', (value) => typeof value === 'boolean');
|
|
@@ -511,6 +613,7 @@ export function checkbox(options) {
|
|
|
511
613
|
return { [fieldName]: { equals: false } };
|
|
512
614
|
return null;
|
|
513
615
|
},
|
|
616
|
+
empty: emptyWhenNullable(fieldName, options?.db?.isNullable === true),
|
|
514
617
|
suggestions: {
|
|
515
618
|
valueSource: {
|
|
516
619
|
kind: 'enum',
|
|
@@ -523,41 +626,57 @@ export function checkbox(options) {
|
|
|
523
626
|
}),
|
|
524
627
|
};
|
|
525
628
|
}
|
|
629
|
+
export function isZonelessNativeType(nativeType) {
|
|
630
|
+
return nativeType !== undefined && /^timestamp\s*(\(|$)/i.test(nativeType.trim());
|
|
631
|
+
}
|
|
526
632
|
/**
|
|
527
633
|
* Timestamp (DateTime) field
|
|
528
634
|
*/
|
|
529
635
|
export function timestamp(options) {
|
|
636
|
+
const defaultValue = options?.defaultValue;
|
|
637
|
+
const hasDefaultNow = typeof defaultValue === 'object' &&
|
|
638
|
+
defaultValue !== null &&
|
|
639
|
+
'kind' in defaultValue &&
|
|
640
|
+
defaultValue.kind === 'now';
|
|
641
|
+
const nullable = options?.db?.isNullable ?? !hasDefaultNow;
|
|
530
642
|
return {
|
|
531
643
|
type: 'timestamp',
|
|
532
644
|
...options,
|
|
533
|
-
getZodSchema: () => {
|
|
534
|
-
|
|
645
|
+
getZodSchema: (_fieldName, _operation, defaults) => {
|
|
646
|
+
const zoneless = isZonelessNativeType(options?.db?.nativeType ?? defaults?.timestampNativeType);
|
|
647
|
+
return z
|
|
648
|
+
.union([z.date(), z.iso.datetime(zoneless ? { local: true } : undefined)])
|
|
649
|
+
.optional()
|
|
650
|
+
.nullable();
|
|
535
651
|
},
|
|
536
652
|
getContractField: (fieldName) => {
|
|
537
|
-
const defaultValue = options?.defaultValue;
|
|
538
|
-
const hasDefaultNow = typeof defaultValue === 'object' &&
|
|
539
|
-
defaultValue !== null &&
|
|
540
|
-
'kind' in defaultValue &&
|
|
541
|
-
defaultValue.kind === 'now';
|
|
542
653
|
return scalarColumn(fieldName, {
|
|
543
654
|
type: pgType('dateTime'),
|
|
544
|
-
nullable
|
|
655
|
+
nullable,
|
|
545
656
|
nativeType: options?.db?.nativeType,
|
|
546
657
|
map: options?.db?.map,
|
|
547
658
|
isIndexed: options?.isIndexed,
|
|
548
659
|
default: hasDefaultNow ? { kind: 'now' } : undefined,
|
|
549
660
|
});
|
|
550
661
|
},
|
|
551
|
-
// An unparseable date degrades to free text.
|
|
662
|
+
// An unparseable date degrades to free text. `eq` on a date-only value matches that whole UTC day.
|
|
552
663
|
getFilterSpec: (fieldName) => ({
|
|
553
664
|
operators: COMPARISON_OPERATORS,
|
|
554
665
|
toCondition: (operator, value) => {
|
|
555
|
-
const
|
|
666
|
+
const typed = value.trim();
|
|
667
|
+
const date = new Date(typed);
|
|
556
668
|
if (Number.isNaN(date.getTime()))
|
|
557
669
|
return null;
|
|
670
|
+
if (operator === 'eq' &&
|
|
671
|
+
DATE_VALUE_MATCH.test(typed) &&
|
|
672
|
+
date.toISOString().startsWith(typed)) {
|
|
673
|
+
const next = new Date(date.getTime() + 86_400_000);
|
|
674
|
+
return { [fieldName]: { gte: date, lt: next } };
|
|
675
|
+
}
|
|
558
676
|
return { [fieldName]: { [prismaComparisonKey(operator)]: date } };
|
|
559
677
|
},
|
|
560
|
-
|
|
678
|
+
empty: emptyWhenNullable(fieldName, nullable),
|
|
679
|
+
suggestions: { valueSource: { kind: 'none' }, datePicker: 'instant' },
|
|
561
680
|
}),
|
|
562
681
|
};
|
|
563
682
|
}
|
|
@@ -638,6 +757,9 @@ export function calendarDay(options) {
|
|
|
638
757
|
})
|
|
639
758
|
.regex(/^\d{4}-\d{2}-\d{2}$/, {
|
|
640
759
|
message: `${formatFieldName(fieldName)} must be in YYYY-MM-DD format`,
|
|
760
|
+
})
|
|
761
|
+
.refine(isCalendarDate, {
|
|
762
|
+
message: `${formatFieldName(fieldName)} must be a real calendar date in YYYY-MM-DD format`,
|
|
641
763
|
});
|
|
642
764
|
if (isRequired && operation === 'create') {
|
|
643
765
|
return dateSchema;
|
|
@@ -646,7 +768,9 @@ export function calendarDay(options) {
|
|
|
646
768
|
return dateSchema.optional();
|
|
647
769
|
}
|
|
648
770
|
else {
|
|
649
|
-
return
|
|
771
|
+
return (options?.db?.isNullable ?? !isRequired)
|
|
772
|
+
? dateSchema.optional().nullable()
|
|
773
|
+
: dateSchema.optional();
|
|
650
774
|
}
|
|
651
775
|
},
|
|
652
776
|
getContractField: (fieldName, listKey) => {
|
|
@@ -663,20 +787,19 @@ export function calendarDay(options) {
|
|
|
663
787
|
// Calendar days compare on the `YYYY-MM-DD` value itself — the column's
|
|
664
788
|
// string codec passes it straight to the driver, so a Date built here
|
|
665
789
|
// would hit the same local-timezone drift the write path had (#1437). A
|
|
666
|
-
// malformed value, including
|
|
667
|
-
//
|
|
790
|
+
// malformed value, including a day the month does not have (e.g.
|
|
791
|
+
// '2025-02-31'), degrades to free text instead of
|
|
668
792
|
// reaching Postgres as an invalid date literal.
|
|
669
793
|
getFilterSpec: (fieldName) => ({
|
|
670
794
|
operators: COMPARISON_OPERATORS,
|
|
671
795
|
toCondition: (operator, value) => {
|
|
672
796
|
const trimmed = value.trim();
|
|
673
|
-
if (
|
|
674
|
-
return null;
|
|
675
|
-
if (Number.isNaN(new Date(`${trimmed}T00:00:00.000Z`).getTime()))
|
|
797
|
+
if (!DATE_VALUE_MATCH.test(trimmed) || !isCalendarDate(trimmed))
|
|
676
798
|
return null;
|
|
677
799
|
return { [fieldName]: { [prismaComparisonKey(operator)]: trimmed } };
|
|
678
800
|
},
|
|
679
|
-
|
|
801
|
+
empty: emptyWhenNullable(fieldName, options?.db?.isNullable ?? !options?.validation?.isRequired),
|
|
802
|
+
suggestions: { valueSource: { kind: 'none' }, valueFormat: DATE_VALUE, datePicker: 'day' },
|
|
680
803
|
}),
|
|
681
804
|
};
|
|
682
805
|
}
|
|
@@ -858,11 +981,14 @@ export function select(options) {
|
|
|
858
981
|
}
|
|
859
982
|
}
|
|
860
983
|
const unionType = options.options.map((opt) => quoteStringLiteral(opt.value)).join(' | ');
|
|
984
|
+
const nullable = options.db?.isNullable ??
|
|
985
|
+
(!options.validation?.isRequired && options.defaultValue === undefined);
|
|
861
986
|
return {
|
|
862
987
|
type: 'select',
|
|
863
988
|
outputType: unionType,
|
|
864
989
|
inputType: unionType,
|
|
865
990
|
...options,
|
|
991
|
+
nullable,
|
|
866
992
|
getZodSchema: (fieldName, operation) => {
|
|
867
993
|
const values = options.options.map((opt) => opt.value);
|
|
868
994
|
let schema = z.enum(values, {
|
|
@@ -873,13 +999,11 @@ export function select(options) {
|
|
|
873
999
|
schema = schema.optional();
|
|
874
1000
|
}
|
|
875
1001
|
else {
|
|
876
|
-
schema = schema.optional().nullable();
|
|
1002
|
+
schema = nullable ? schema.optional().nullable() : schema.optional();
|
|
877
1003
|
}
|
|
878
1004
|
return schema;
|
|
879
1005
|
},
|
|
880
1006
|
getContractField: (fieldName, listName) => {
|
|
881
|
-
const hasDefault = options.defaultValue !== undefined;
|
|
882
|
-
const nullable = options.db?.isNullable ?? (!options.validation?.isRequired && !hasDefault);
|
|
883
1007
|
if (!isNativeEnum) {
|
|
884
1008
|
return scalarColumn(fieldName, {
|
|
885
1009
|
type: pgType('text'),
|
|
@@ -917,6 +1041,7 @@ export function select(options) {
|
|
|
917
1041
|
return null;
|
|
918
1042
|
return { [fieldName]: { equals: match.value } };
|
|
919
1043
|
},
|
|
1044
|
+
empty: emptyWhenNullable(fieldName, nullable),
|
|
920
1045
|
suggestions: {
|
|
921
1046
|
valueSource: {
|
|
922
1047
|
kind: 'enum',
|
|
@@ -1121,7 +1246,8 @@ export function relationship(options) {
|
|
|
1121
1246
|
// • to-one filters by the related Item's label — `author:"Ada Lovelace"`
|
|
1122
1247
|
// becomes a `some` over the target list's Label field. Prisma 8 has no
|
|
1123
1248
|
// to-one predicate form: every relation predicate is an EXISTS, and its
|
|
1124
|
-
// accessor is the same for both cardinalities.
|
|
1249
|
+
// accessor is the same for both cardinalities. `author:#id` matches the
|
|
1250
|
+
// related record by its id, through the same relation predicate.
|
|
1125
1251
|
// • to-many filters by PRESENCE. Prisma 8 cannot compare a relation count
|
|
1126
1252
|
// in a `where`, so `orders:0` is `none`, `orders:>0`/`orders:>=1` are
|
|
1127
1253
|
// `some`, and any other comparison degrades to free text.
|
|
@@ -1134,21 +1260,18 @@ export function relationship(options) {
|
|
|
1134
1260
|
return undefined;
|
|
1135
1261
|
if (field.many === true) {
|
|
1136
1262
|
return {
|
|
1137
|
-
operators:
|
|
1263
|
+
operators: ['eq', 'gt', 'gte'],
|
|
1138
1264
|
toCondition: (operator, value) => {
|
|
1139
|
-
|
|
1140
|
-
if (!/^-?\d+$/.test(trimmed))
|
|
1265
|
+
if (!PRESENCE_COUNT_MATCH(operator, value.trim()))
|
|
1141
1266
|
return null;
|
|
1142
|
-
|
|
1143
|
-
if (operator === 'eq' && count === 0)
|
|
1267
|
+
if (operator === 'eq')
|
|
1144
1268
|
return { [_fieldName]: { none: {} } };
|
|
1145
|
-
if (operator === 'gt'
|
|
1146
|
-
return { [_fieldName]: { some: {} } };
|
|
1147
|
-
if (operator === 'gte' && count === 1)
|
|
1269
|
+
if (operator === 'gt' || operator === 'gte')
|
|
1148
1270
|
return { [_fieldName]: { some: {} } };
|
|
1149
1271
|
return null;
|
|
1150
1272
|
},
|
|
1151
|
-
|
|
1273
|
+
negation: 'redundant',
|
|
1274
|
+
suggestions: { valueSource: { kind: 'none' }, valueFormat: PRESENCE_COUNT },
|
|
1152
1275
|
};
|
|
1153
1276
|
}
|
|
1154
1277
|
const labelField = getLabelFieldName(relatedListConfig);
|
|
@@ -1161,6 +1284,7 @@ export function relationship(options) {
|
|
|
1161
1284
|
// integer, checkbox, select, etc.). None of these is filterable (#1359).
|
|
1162
1285
|
if (labelFieldConfig?.type !== 'text' || labelFieldConfig.virtual === true)
|
|
1163
1286
|
return undefined;
|
|
1287
|
+
const canBeEmpty = !shouldHaveForeignKey(_listKey, _fieldName, field, config) || field.db?.isNullable !== false;
|
|
1164
1288
|
return {
|
|
1165
1289
|
operators: ['eq'],
|
|
1166
1290
|
toCondition: (operator, value) => {
|
|
@@ -1168,6 +1292,12 @@ export function relationship(options) {
|
|
|
1168
1292
|
return null;
|
|
1169
1293
|
return { [_fieldName]: { some: { [labelField]: { contains: value } } } };
|
|
1170
1294
|
},
|
|
1295
|
+
toIdCondition: (id) => {
|
|
1296
|
+
const parsed = parseListId(config, targetList, id);
|
|
1297
|
+
return parsed.ok ? { [_fieldName]: { some: { id: { equals: parsed.value } } } } : null;
|
|
1298
|
+
},
|
|
1299
|
+
empty: canBeEmpty ? { [_fieldName]: { none: {} } } : undefined,
|
|
1300
|
+
negation: 'includesEmpty',
|
|
1171
1301
|
suggestions: { valueSource: { kind: 'relationship', listKey: targetList, many: false } },
|
|
1172
1302
|
};
|
|
1173
1303
|
};
|
|
@@ -1215,13 +1345,17 @@ export function relationship(options) {
|
|
|
1215
1345
|
* @returns JSON field configuration
|
|
1216
1346
|
*/
|
|
1217
1347
|
export function json(options) {
|
|
1348
|
+
const nullable = options?.db?.isNullable ?? !options?.validation?.isRequired;
|
|
1218
1349
|
return {
|
|
1219
1350
|
type: 'json',
|
|
1220
1351
|
...options,
|
|
1352
|
+
nullable,
|
|
1221
1353
|
getZodSchema: (fieldName, operation) => {
|
|
1222
1354
|
const validation = options?.validation;
|
|
1223
1355
|
const isRequired = validation?.isRequired;
|
|
1224
|
-
const baseSchema = z.unknown()
|
|
1356
|
+
const baseSchema = z.unknown().refine((value) => !hasNul(value), {
|
|
1357
|
+
message: `${formatFieldName(fieldName)} must not contain NUL characters`,
|
|
1358
|
+
});
|
|
1225
1359
|
if (isRequired && operation === 'create') {
|
|
1226
1360
|
// Required in create mode: a value must be provided and it must be
|
|
1227
1361
|
// non-null (issue #604 — a required json field means non-null). A bare
|
|
@@ -1234,12 +1368,13 @@ export function json(options) {
|
|
|
1234
1368
|
message: `${formatFieldName(fieldName)} is required`,
|
|
1235
1369
|
});
|
|
1236
1370
|
}
|
|
1237
|
-
else if (isRequired
|
|
1238
|
-
//
|
|
1239
|
-
//
|
|
1240
|
-
// (issue #604 — required json means non-null).
|
|
1241
|
-
// before `.optional()` short-circuits on undefined:
|
|
1242
|
-
// passes, a present null is rejected, and other present
|
|
1371
|
+
else if (isRequired || !nullable) {
|
|
1372
|
+
// Omitted keys still pass (issue #570 — partial updates may leave the
|
|
1373
|
+
// field untouched, and a NOT NULL column may carry a default), but a
|
|
1374
|
+
// present null is rejected (issue #604 — required json means non-null).
|
|
1375
|
+
// The `.refine()` runs before `.optional()` short-circuits on undefined:
|
|
1376
|
+
// absent/undefined passes, a present null is rejected, and other present
|
|
1377
|
+
// values pass.
|
|
1243
1378
|
return baseSchema
|
|
1244
1379
|
.refine((value) => value !== null, {
|
|
1245
1380
|
message: `${formatFieldName(fieldName)} is required`,
|
|
@@ -1252,7 +1387,7 @@ export function json(options) {
|
|
|
1252
1387
|
},
|
|
1253
1388
|
getContractField: (fieldName, listKey) => scalarColumn(fieldName, {
|
|
1254
1389
|
type: pgType('jsonb'),
|
|
1255
|
-
nullable
|
|
1390
|
+
nullable,
|
|
1256
1391
|
nativeType: options?.db?.nativeType,
|
|
1257
1392
|
map: options?.db?.map,
|
|
1258
1393
|
default: literalDefault(options?.defaultValue, listKey, fieldName),
|