prisma-guard 1.9.1 → 1.11.0
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/dist/runtime/index.d.cts
CHANGED
|
@@ -158,7 +158,7 @@ declare function createGuard<TModels extends TypeMap = TypeMap, TRoots extends s
|
|
|
158
158
|
input: (model: Extract<keyof TModels, string>, opts: InputOpts) => InputSchema;
|
|
159
159
|
model: (model: Extract<keyof TModels, string>, opts: ModelOpts) => zod.ZodObject<any, zod_v4_core.$strip>;
|
|
160
160
|
query: <TCtx = unknown>(model: Extract<keyof TModels, string>, method: QueryMethod, config_: ShapeOrFn<TCtx> | Record<string, ShapeOrFn<TCtx>>) => QuerySchema<TCtx>;
|
|
161
|
-
extension: <TCtx extends Record<string, unknown> = Record<string, unknown>>(contextFn
|
|
161
|
+
extension: <TCtx extends Record<string, unknown> = Record<string, unknown>>(contextFn?: () => TCtx) => {
|
|
162
162
|
name: string;
|
|
163
163
|
model: TModelExt;
|
|
164
164
|
query: {
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -158,7 +158,7 @@ declare function createGuard<TModels extends TypeMap = TypeMap, TRoots extends s
|
|
|
158
158
|
input: (model: Extract<keyof TModels, string>, opts: InputOpts) => InputSchema;
|
|
159
159
|
model: (model: Extract<keyof TModels, string>, opts: ModelOpts) => zod.ZodObject<any, zod_v4_core.$strip>;
|
|
160
160
|
query: <TCtx = unknown>(model: Extract<keyof TModels, string>, method: QueryMethod, config_: ShapeOrFn<TCtx> | Record<string, ShapeOrFn<TCtx>>) => QuerySchema<TCtx>;
|
|
161
|
-
extension: <TCtx extends Record<string, unknown> = Record<string, unknown>>(contextFn
|
|
161
|
+
extension: <TCtx extends Record<string, unknown> = Record<string, unknown>>(contextFn?: () => TCtx) => {
|
|
162
162
|
name: string;
|
|
163
163
|
model: TModelExt;
|
|
164
164
|
query: {
|
package/dist/runtime/index.js
CHANGED
|
@@ -131,6 +131,47 @@ import { z as z3 } from "zod";
|
|
|
131
131
|
|
|
132
132
|
// src/runtime/zod-type-map.ts
|
|
133
133
|
import { z as z2 } from "zod";
|
|
134
|
+
|
|
135
|
+
// src/shared/utils.ts
|
|
136
|
+
function isPlainObject(v) {
|
|
137
|
+
if (typeof v !== "object" || v === null || Array.isArray(v))
|
|
138
|
+
return false;
|
|
139
|
+
const proto = Object.getPrototypeOf(v);
|
|
140
|
+
return proto === Object.prototype || proto === null;
|
|
141
|
+
}
|
|
142
|
+
function schemaProducesValueForUndefined(schema) {
|
|
143
|
+
const result = schema.safeParse(void 0);
|
|
144
|
+
return result.success && result.data !== void 0;
|
|
145
|
+
}
|
|
146
|
+
function isZodSchema(value) {
|
|
147
|
+
if (value == null || typeof value !== "object")
|
|
148
|
+
return false;
|
|
149
|
+
const v = value;
|
|
150
|
+
return typeof v.parse === "function" && typeof v.optional === "function";
|
|
151
|
+
}
|
|
152
|
+
function coerceToArray(value) {
|
|
153
|
+
if (Array.isArray(value))
|
|
154
|
+
return value;
|
|
155
|
+
if (value === null || value === void 0)
|
|
156
|
+
return value;
|
|
157
|
+
if (typeof value !== "object")
|
|
158
|
+
return value;
|
|
159
|
+
const keys = Object.keys(value);
|
|
160
|
+
if (keys.length === 0)
|
|
161
|
+
return [];
|
|
162
|
+
const allNumeric = keys.every((k) => /^\d+$/.test(k));
|
|
163
|
+
if (!allNumeric)
|
|
164
|
+
return value;
|
|
165
|
+
const sorted = keys.map(Number).sort((a, b) => a - b);
|
|
166
|
+
const obj = value;
|
|
167
|
+
const result = [];
|
|
168
|
+
for (const idx of sorted) {
|
|
169
|
+
result.push(obj[String(idx)]);
|
|
170
|
+
}
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// src/runtime/zod-type-map.ts
|
|
134
175
|
var SCALAR_OPERATORS = {
|
|
135
176
|
String: /* @__PURE__ */ new Set(["equals", "not", "contains", "startsWith", "endsWith", "in", "notIn", "gt", "gte", "lt", "lte"]),
|
|
136
177
|
Int: /* @__PURE__ */ new Set(["equals", "not", "gt", "gte", "lt", "lte", "in", "notIn"]),
|
|
@@ -182,17 +223,16 @@ function createScalarListOperatorSchema(fieldMeta, operator, enumMap, scalarBase
|
|
|
182
223
|
if (operator === "isEmpty") {
|
|
183
224
|
return z2.boolean();
|
|
184
225
|
}
|
|
185
|
-
if (operator === "equals") {
|
|
186
|
-
const itemMeta2 = { ...fieldMeta, isList: false };
|
|
187
|
-
const itemBase2 = createBaseType(itemMeta2, enumMap, scalarBase);
|
|
188
|
-
return fieldMeta.isRequired ? z2.array(itemBase2) : z2.union([z2.array(itemBase2), z2.null()]);
|
|
189
|
-
}
|
|
190
226
|
const itemMeta = { ...fieldMeta, isList: false };
|
|
191
227
|
const itemBase = createBaseType(itemMeta, enumMap, scalarBase);
|
|
192
228
|
if (operator === "has") {
|
|
193
229
|
return !fieldMeta.isRequired ? z2.union([itemBase, z2.null()]) : itemBase;
|
|
194
230
|
}
|
|
195
|
-
|
|
231
|
+
if (operator === "equals") {
|
|
232
|
+
const arrSchema = z2.array(itemBase);
|
|
233
|
+
return fieldMeta.isRequired ? z2.preprocess(coerceToArray, arrSchema) : z2.union([z2.preprocess(coerceToArray, arrSchema), z2.null()]);
|
|
234
|
+
}
|
|
235
|
+
return z2.preprocess(coerceToArray, z2.array(itemBase));
|
|
196
236
|
}
|
|
197
237
|
function createOperatorSchema(fieldMeta, operator, enumMap, scalarBase) {
|
|
198
238
|
if (fieldMeta.isList) {
|
|
@@ -210,10 +250,8 @@ function createOperatorSchema(fieldMeta, operator, enumMap, scalarBase) {
|
|
|
210
250
|
if (operator === "equals" || operator === "not") {
|
|
211
251
|
return !fieldMeta.isRequired ? z2.union([enumSchema, z2.null()]) : enumSchema;
|
|
212
252
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
return z2.array(enumSchema);
|
|
253
|
+
const itemSchema = !fieldMeta.isRequired ? z2.union([enumSchema, z2.null()]) : enumSchema;
|
|
254
|
+
return z2.preprocess(coerceToArray, z2.array(itemSchema));
|
|
217
255
|
}
|
|
218
256
|
const supportedOps = SCALAR_OPERATORS[fieldMeta.type];
|
|
219
257
|
if (!supportedOps) {
|
|
@@ -234,32 +272,12 @@ function createOperatorSchema(fieldMeta, operator, enumMap, scalarBase) {
|
|
|
234
272
|
return !fieldMeta.isRequired ? z2.union([scalar, z2.null()]) : scalar;
|
|
235
273
|
}
|
|
236
274
|
if (operator === "in" || operator === "notIn") {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
return z2.array(scalar);
|
|
275
|
+
const itemSchema = !fieldMeta.isRequired ? z2.union([scalar, z2.null()]) : scalar;
|
|
276
|
+
return z2.preprocess(coerceToArray, z2.array(itemSchema));
|
|
241
277
|
}
|
|
242
278
|
return scalar;
|
|
243
279
|
}
|
|
244
280
|
|
|
245
|
-
// src/shared/utils.ts
|
|
246
|
-
function isPlainObject(v) {
|
|
247
|
-
if (typeof v !== "object" || v === null || Array.isArray(v))
|
|
248
|
-
return false;
|
|
249
|
-
const proto = Object.getPrototypeOf(v);
|
|
250
|
-
return proto === Object.prototype || proto === null;
|
|
251
|
-
}
|
|
252
|
-
function schemaProducesValueForUndefined(schema) {
|
|
253
|
-
const result = schema.safeParse(void 0);
|
|
254
|
-
return result.success && result.data !== void 0;
|
|
255
|
-
}
|
|
256
|
-
function isZodSchema(value) {
|
|
257
|
-
if (value == null || typeof value !== "object")
|
|
258
|
-
return false;
|
|
259
|
-
const v = value;
|
|
260
|
-
return typeof v.parse === "function" && typeof v.optional === "function";
|
|
261
|
-
}
|
|
262
|
-
|
|
263
281
|
// src/runtime/schema-builder.ts
|
|
264
282
|
var DEFAULT_MAX_CACHE = 500;
|
|
265
283
|
var DEFAULT_MAX_DEPTH = 5;
|
|
@@ -1149,9 +1167,12 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
|
|
|
1149
1167
|
);
|
|
1150
1168
|
}
|
|
1151
1169
|
if (key === "NOT") {
|
|
1152
|
-
fieldSchemas[key] = z4.union([
|
|
1170
|
+
fieldSchemas[key] = z4.union([
|
|
1171
|
+
elementSchema,
|
|
1172
|
+
z4.preprocess(coerceToArray, z4.array(elementSchema).min(1))
|
|
1173
|
+
]).optional();
|
|
1153
1174
|
} else {
|
|
1154
|
-
fieldSchemas[key] = z4.array(elementSchema).min(1).optional();
|
|
1175
|
+
fieldSchemas[key] = z4.preprocess(coerceToArray, z4.array(elementSchema).min(1)).optional();
|
|
1155
1176
|
}
|
|
1156
1177
|
}
|
|
1157
1178
|
if (hasWhereForced(result.forced)) {
|
|
@@ -1333,14 +1354,25 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
|
|
|
1333
1354
|
}
|
|
1334
1355
|
if (hasClientOps) {
|
|
1335
1356
|
const opObj = z4.object(opSchemas).strict();
|
|
1336
|
-
|
|
1357
|
+
const refined = opObj.refine(
|
|
1337
1358
|
(v) => clientOpKeys.some(
|
|
1338
1359
|
(k) => v[k] !== void 0
|
|
1339
1360
|
),
|
|
1340
1361
|
{
|
|
1341
1362
|
message: `At least one operator required for where field "${fieldName}"`
|
|
1342
1363
|
}
|
|
1343
|
-
)
|
|
1364
|
+
);
|
|
1365
|
+
if ("equals" in opSchemas) {
|
|
1366
|
+
const equalsBase = createOperatorSchema(
|
|
1367
|
+
fieldMeta,
|
|
1368
|
+
"equals",
|
|
1369
|
+
enumMap,
|
|
1370
|
+
scalarBase
|
|
1371
|
+
);
|
|
1372
|
+
fieldSchemas[fieldName] = z4.union([refined, equalsBase]).optional();
|
|
1373
|
+
} else {
|
|
1374
|
+
fieldSchemas[fieldName] = refined.optional();
|
|
1375
|
+
}
|
|
1344
1376
|
}
|
|
1345
1377
|
if (Object.keys(fieldForced).length > 0) {
|
|
1346
1378
|
scalarConditions[fieldName] = fieldForced;
|
|
@@ -1442,7 +1474,7 @@ function createArgsBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
|
|
|
1442
1474
|
(v) => fieldKeys.some((k) => v[k] !== void 0),
|
|
1443
1475
|
{ message: "orderBy must specify at least one field" }
|
|
1444
1476
|
);
|
|
1445
|
-
return z5.union([singleSchema, z5.array(singleSchema).min(1)]).optional();
|
|
1477
|
+
return z5.union([singleSchema, z5.preprocess(coerceToArray, z5.array(singleSchema).min(1))]).optional();
|
|
1446
1478
|
}
|
|
1447
1479
|
function buildTakeSchema(config) {
|
|
1448
1480
|
if (!Number.isFinite(config.max) || !Number.isInteger(config.max)) {
|
|
@@ -1513,7 +1545,7 @@ function createArgsBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
|
|
|
1513
1545
|
throw new ShapeError(`List field "${fieldName}" cannot be used in distinct`);
|
|
1514
1546
|
}
|
|
1515
1547
|
const enumSchema = z5.enum(distinctConfig);
|
|
1516
|
-
return z5.union([enumSchema, z5.array(enumSchema).min(1)]).optional();
|
|
1548
|
+
return z5.union([enumSchema, z5.preprocess(coerceToArray, z5.array(enumSchema).min(1))]).optional();
|
|
1517
1549
|
}
|
|
1518
1550
|
function buildBySchema(model, byConfig) {
|
|
1519
1551
|
if (byConfig.length === 0) {
|
|
@@ -1535,7 +1567,7 @@ function createArgsBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
|
|
|
1535
1567
|
throw new ShapeError(`List field "${fieldName}" cannot be used in by`);
|
|
1536
1568
|
}
|
|
1537
1569
|
const enumSchema = z5.enum(byConfig);
|
|
1538
|
-
return z5.union([enumSchema, z5.array(enumSchema).min(1)]);
|
|
1570
|
+
return z5.union([enumSchema, z5.preprocess(coerceToArray, z5.array(enumSchema).min(1))]);
|
|
1539
1571
|
}
|
|
1540
1572
|
function buildHavingSchema(model, havingConfig) {
|
|
1541
1573
|
const modelFields = typeMap[model];
|
|
@@ -2222,7 +2254,7 @@ function createQueryBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
|
|
|
2222
2254
|
),
|
|
2223
2255
|
{ message: "orderBy must specify at least one field" }
|
|
2224
2256
|
);
|
|
2225
|
-
schemaFields["orderBy"] = z7.union([singleSchema, z7.array(singleSchema).min(1)]).optional();
|
|
2257
|
+
schemaFields["orderBy"] = z7.union([singleSchema, z7.preprocess(coerceToArray, z7.array(singleSchema).min(1))]).optional();
|
|
2226
2258
|
} else {
|
|
2227
2259
|
const groupByOrderFields = {};
|
|
2228
2260
|
for (const [fieldName, config] of Object.entries(shape.orderBy)) {
|
|
@@ -2265,7 +2297,7 @@ function createQueryBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
|
|
|
2265
2297
|
),
|
|
2266
2298
|
{ message: "orderBy must specify at least one field" }
|
|
2267
2299
|
);
|
|
2268
|
-
schemaFields["orderBy"] = z7.union([singleSchema, z7.array(singleSchema).min(1)]).optional();
|
|
2300
|
+
schemaFields["orderBy"] = z7.union([singleSchema, z7.preprocess(coerceToArray, z7.array(singleSchema).min(1))]).optional();
|
|
2269
2301
|
}
|
|
2270
2302
|
} else {
|
|
2271
2303
|
schemaFields["orderBy"] = argsBuilder.buildOrderBySchema(
|
|
@@ -3916,7 +3948,9 @@ function createModelGuardExtension(config) {
|
|
|
3916
3948
|
|
|
3917
3949
|
// src/runtime/guard.ts
|
|
3918
3950
|
function createGuard(config) {
|
|
3919
|
-
const scalarBase = createScalarBase(
|
|
3951
|
+
const scalarBase = createScalarBase(
|
|
3952
|
+
config.guardConfig.strictDecimal ?? false
|
|
3953
|
+
);
|
|
3920
3954
|
const schemaBuilder = createSchemaBuilder(
|
|
3921
3955
|
config.typeMap,
|
|
3922
3956
|
config.zodChains,
|
|
@@ -3930,11 +3964,15 @@ function createGuard(config) {
|
|
|
3930
3964
|
config.uniqueMap ?? {},
|
|
3931
3965
|
scalarBase
|
|
3932
3966
|
);
|
|
3933
|
-
const log = config.logger ?? {
|
|
3967
|
+
const log = config.logger ?? {
|
|
3968
|
+
warn: (msg) => console.warn(msg)
|
|
3969
|
+
};
|
|
3934
3970
|
const wrapZodErrors = config.wrapZodErrors ?? false;
|
|
3935
3971
|
function rethrowZod(err) {
|
|
3936
3972
|
if (err instanceof ZodError) {
|
|
3937
|
-
throw new ShapeError(`Validation failed: ${formatZodError(err)}`, {
|
|
3973
|
+
throw new ShapeError(`Validation failed: ${formatZodError(err)}`, {
|
|
3974
|
+
cause: err
|
|
3975
|
+
});
|
|
3938
3976
|
}
|
|
3939
3977
|
throw err;
|
|
3940
3978
|
}
|
|
@@ -3971,6 +4009,7 @@ function createGuard(config) {
|
|
|
3971
4009
|
};
|
|
3972
4010
|
},
|
|
3973
4011
|
extension: (contextFn) => {
|
|
4012
|
+
const effectiveContextFn = contextFn ?? (() => ({}));
|
|
3974
4013
|
const scopeRoots = /* @__PURE__ */ new Set();
|
|
3975
4014
|
for (const entries of Object.values(config.scopeMap)) {
|
|
3976
4015
|
for (const entry of entries) {
|
|
@@ -3978,7 +4017,7 @@ function createGuard(config) {
|
|
|
3978
4017
|
}
|
|
3979
4018
|
}
|
|
3980
4019
|
const scopeCtxFn = () => {
|
|
3981
|
-
const ctx = validateContext(
|
|
4020
|
+
const ctx = validateContext(effectiveContextFn());
|
|
3982
4021
|
const scopeCtx = {};
|
|
3983
4022
|
for (const key of Object.keys(ctx)) {
|
|
3984
4023
|
if (!scopeRoots.has(key))
|
|
@@ -4008,7 +4047,7 @@ function createGuard(config) {
|
|
|
4008
4047
|
uniqueMap: config.uniqueMap ?? {},
|
|
4009
4048
|
scopeMap: config.scopeMap,
|
|
4010
4049
|
guardConfig: config.guardConfig,
|
|
4011
|
-
contextFn,
|
|
4050
|
+
contextFn: effectiveContextFn,
|
|
4012
4051
|
wrapZodErrors
|
|
4013
4052
|
});
|
|
4014
4053
|
return {
|