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.
@@ -161,6 +161,47 @@ var import_zod3 = require("zod");
161
161
 
162
162
  // src/runtime/zod-type-map.ts
163
163
  var import_zod2 = require("zod");
164
+
165
+ // src/shared/utils.ts
166
+ function isPlainObject(v) {
167
+ if (typeof v !== "object" || v === null || Array.isArray(v))
168
+ return false;
169
+ const proto = Object.getPrototypeOf(v);
170
+ return proto === Object.prototype || proto === null;
171
+ }
172
+ function schemaProducesValueForUndefined(schema) {
173
+ const result = schema.safeParse(void 0);
174
+ return result.success && result.data !== void 0;
175
+ }
176
+ function isZodSchema(value) {
177
+ if (value == null || typeof value !== "object")
178
+ return false;
179
+ const v = value;
180
+ return typeof v.parse === "function" && typeof v.optional === "function";
181
+ }
182
+ function coerceToArray(value) {
183
+ if (Array.isArray(value))
184
+ return value;
185
+ if (value === null || value === void 0)
186
+ return value;
187
+ if (typeof value !== "object")
188
+ return value;
189
+ const keys = Object.keys(value);
190
+ if (keys.length === 0)
191
+ return [];
192
+ const allNumeric = keys.every((k) => /^\d+$/.test(k));
193
+ if (!allNumeric)
194
+ return value;
195
+ const sorted = keys.map(Number).sort((a, b) => a - b);
196
+ const obj = value;
197
+ const result = [];
198
+ for (const idx of sorted) {
199
+ result.push(obj[String(idx)]);
200
+ }
201
+ return result;
202
+ }
203
+
204
+ // src/runtime/zod-type-map.ts
164
205
  var SCALAR_OPERATORS = {
165
206
  String: /* @__PURE__ */ new Set(["equals", "not", "contains", "startsWith", "endsWith", "in", "notIn", "gt", "gte", "lt", "lte"]),
166
207
  Int: /* @__PURE__ */ new Set(["equals", "not", "gt", "gte", "lt", "lte", "in", "notIn"]),
@@ -212,17 +253,16 @@ function createScalarListOperatorSchema(fieldMeta, operator, enumMap, scalarBase
212
253
  if (operator === "isEmpty") {
213
254
  return import_zod2.z.boolean();
214
255
  }
215
- if (operator === "equals") {
216
- const itemMeta2 = { ...fieldMeta, isList: false };
217
- const itemBase2 = createBaseType(itemMeta2, enumMap, scalarBase);
218
- return fieldMeta.isRequired ? import_zod2.z.array(itemBase2) : import_zod2.z.union([import_zod2.z.array(itemBase2), import_zod2.z.null()]);
219
- }
220
256
  const itemMeta = { ...fieldMeta, isList: false };
221
257
  const itemBase = createBaseType(itemMeta, enumMap, scalarBase);
222
258
  if (operator === "has") {
223
259
  return !fieldMeta.isRequired ? import_zod2.z.union([itemBase, import_zod2.z.null()]) : itemBase;
224
260
  }
225
- return import_zod2.z.array(itemBase);
261
+ if (operator === "equals") {
262
+ const arrSchema = import_zod2.z.array(itemBase);
263
+ return fieldMeta.isRequired ? import_zod2.z.preprocess(coerceToArray, arrSchema) : import_zod2.z.union([import_zod2.z.preprocess(coerceToArray, arrSchema), import_zod2.z.null()]);
264
+ }
265
+ return import_zod2.z.preprocess(coerceToArray, import_zod2.z.array(itemBase));
226
266
  }
227
267
  function createOperatorSchema(fieldMeta, operator, enumMap, scalarBase) {
228
268
  if (fieldMeta.isList) {
@@ -240,10 +280,8 @@ function createOperatorSchema(fieldMeta, operator, enumMap, scalarBase) {
240
280
  if (operator === "equals" || operator === "not") {
241
281
  return !fieldMeta.isRequired ? import_zod2.z.union([enumSchema, import_zod2.z.null()]) : enumSchema;
242
282
  }
243
- if (!fieldMeta.isRequired) {
244
- return import_zod2.z.array(import_zod2.z.union([enumSchema, import_zod2.z.null()]));
245
- }
246
- return import_zod2.z.array(enumSchema);
283
+ const itemSchema = !fieldMeta.isRequired ? import_zod2.z.union([enumSchema, import_zod2.z.null()]) : enumSchema;
284
+ return import_zod2.z.preprocess(coerceToArray, import_zod2.z.array(itemSchema));
247
285
  }
248
286
  const supportedOps = SCALAR_OPERATORS[fieldMeta.type];
249
287
  if (!supportedOps) {
@@ -264,32 +302,12 @@ function createOperatorSchema(fieldMeta, operator, enumMap, scalarBase) {
264
302
  return !fieldMeta.isRequired ? import_zod2.z.union([scalar, import_zod2.z.null()]) : scalar;
265
303
  }
266
304
  if (operator === "in" || operator === "notIn") {
267
- if (!fieldMeta.isRequired) {
268
- return import_zod2.z.array(import_zod2.z.union([scalar, import_zod2.z.null()]));
269
- }
270
- return import_zod2.z.array(scalar);
305
+ const itemSchema = !fieldMeta.isRequired ? import_zod2.z.union([scalar, import_zod2.z.null()]) : scalar;
306
+ return import_zod2.z.preprocess(coerceToArray, import_zod2.z.array(itemSchema));
271
307
  }
272
308
  return scalar;
273
309
  }
274
310
 
275
- // src/shared/utils.ts
276
- function isPlainObject(v) {
277
- if (typeof v !== "object" || v === null || Array.isArray(v))
278
- return false;
279
- const proto = Object.getPrototypeOf(v);
280
- return proto === Object.prototype || proto === null;
281
- }
282
- function schemaProducesValueForUndefined(schema) {
283
- const result = schema.safeParse(void 0);
284
- return result.success && result.data !== void 0;
285
- }
286
- function isZodSchema(value) {
287
- if (value == null || typeof value !== "object")
288
- return false;
289
- const v = value;
290
- return typeof v.parse === "function" && typeof v.optional === "function";
291
- }
292
-
293
311
  // src/runtime/schema-builder.ts
294
312
  var DEFAULT_MAX_CACHE = 500;
295
313
  var DEFAULT_MAX_DEPTH = 5;
@@ -1179,9 +1197,12 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
1179
1197
  );
1180
1198
  }
1181
1199
  if (key === "NOT") {
1182
- fieldSchemas[key] = import_zod4.z.union([elementSchema, import_zod4.z.array(elementSchema).min(1)]).optional();
1200
+ fieldSchemas[key] = import_zod4.z.union([
1201
+ elementSchema,
1202
+ import_zod4.z.preprocess(coerceToArray, import_zod4.z.array(elementSchema).min(1))
1203
+ ]).optional();
1183
1204
  } else {
1184
- fieldSchemas[key] = import_zod4.z.array(elementSchema).min(1).optional();
1205
+ fieldSchemas[key] = import_zod4.z.preprocess(coerceToArray, import_zod4.z.array(elementSchema).min(1)).optional();
1185
1206
  }
1186
1207
  }
1187
1208
  if (hasWhereForced(result.forced)) {
@@ -1363,14 +1384,25 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
1363
1384
  }
1364
1385
  if (hasClientOps) {
1365
1386
  const opObj = import_zod4.z.object(opSchemas).strict();
1366
- fieldSchemas[fieldName] = opObj.refine(
1387
+ const refined = opObj.refine(
1367
1388
  (v) => clientOpKeys.some(
1368
1389
  (k) => v[k] !== void 0
1369
1390
  ),
1370
1391
  {
1371
1392
  message: `At least one operator required for where field "${fieldName}"`
1372
1393
  }
1373
- ).optional();
1394
+ );
1395
+ if ("equals" in opSchemas) {
1396
+ const equalsBase = createOperatorSchema(
1397
+ fieldMeta,
1398
+ "equals",
1399
+ enumMap,
1400
+ scalarBase
1401
+ );
1402
+ fieldSchemas[fieldName] = import_zod4.z.union([refined, equalsBase]).optional();
1403
+ } else {
1404
+ fieldSchemas[fieldName] = refined.optional();
1405
+ }
1374
1406
  }
1375
1407
  if (Object.keys(fieldForced).length > 0) {
1376
1408
  scalarConditions[fieldName] = fieldForced;
@@ -1472,7 +1504,7 @@ function createArgsBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
1472
1504
  (v) => fieldKeys.some((k) => v[k] !== void 0),
1473
1505
  { message: "orderBy must specify at least one field" }
1474
1506
  );
1475
- return import_zod5.z.union([singleSchema, import_zod5.z.array(singleSchema).min(1)]).optional();
1507
+ return import_zod5.z.union([singleSchema, import_zod5.z.preprocess(coerceToArray, import_zod5.z.array(singleSchema).min(1))]).optional();
1476
1508
  }
1477
1509
  function buildTakeSchema(config) {
1478
1510
  if (!Number.isFinite(config.max) || !Number.isInteger(config.max)) {
@@ -1543,7 +1575,7 @@ function createArgsBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
1543
1575
  throw new ShapeError(`List field "${fieldName}" cannot be used in distinct`);
1544
1576
  }
1545
1577
  const enumSchema = import_zod5.z.enum(distinctConfig);
1546
- return import_zod5.z.union([enumSchema, import_zod5.z.array(enumSchema).min(1)]).optional();
1578
+ return import_zod5.z.union([enumSchema, import_zod5.z.preprocess(coerceToArray, import_zod5.z.array(enumSchema).min(1))]).optional();
1547
1579
  }
1548
1580
  function buildBySchema(model, byConfig) {
1549
1581
  if (byConfig.length === 0) {
@@ -1565,7 +1597,7 @@ function createArgsBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
1565
1597
  throw new ShapeError(`List field "${fieldName}" cannot be used in by`);
1566
1598
  }
1567
1599
  const enumSchema = import_zod5.z.enum(byConfig);
1568
- return import_zod5.z.union([enumSchema, import_zod5.z.array(enumSchema).min(1)]);
1600
+ return import_zod5.z.union([enumSchema, import_zod5.z.preprocess(coerceToArray, import_zod5.z.array(enumSchema).min(1))]);
1569
1601
  }
1570
1602
  function buildHavingSchema(model, havingConfig) {
1571
1603
  const modelFields = typeMap[model];
@@ -2252,7 +2284,7 @@ function createQueryBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
2252
2284
  ),
2253
2285
  { message: "orderBy must specify at least one field" }
2254
2286
  );
2255
- schemaFields["orderBy"] = import_zod7.z.union([singleSchema, import_zod7.z.array(singleSchema).min(1)]).optional();
2287
+ schemaFields["orderBy"] = import_zod7.z.union([singleSchema, import_zod7.z.preprocess(coerceToArray, import_zod7.z.array(singleSchema).min(1))]).optional();
2256
2288
  } else {
2257
2289
  const groupByOrderFields = {};
2258
2290
  for (const [fieldName, config] of Object.entries(shape.orderBy)) {
@@ -2295,7 +2327,7 @@ function createQueryBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
2295
2327
  ),
2296
2328
  { message: "orderBy must specify at least one field" }
2297
2329
  );
2298
- schemaFields["orderBy"] = import_zod7.z.union([singleSchema, import_zod7.z.array(singleSchema).min(1)]).optional();
2330
+ schemaFields["orderBy"] = import_zod7.z.union([singleSchema, import_zod7.z.preprocess(coerceToArray, import_zod7.z.array(singleSchema).min(1))]).optional();
2299
2331
  }
2300
2332
  } else {
2301
2333
  schemaFields["orderBy"] = argsBuilder.buildOrderBySchema(
@@ -3946,7 +3978,9 @@ function createModelGuardExtension(config) {
3946
3978
 
3947
3979
  // src/runtime/guard.ts
3948
3980
  function createGuard(config) {
3949
- const scalarBase = createScalarBase(config.guardConfig.strictDecimal ?? false);
3981
+ const scalarBase = createScalarBase(
3982
+ config.guardConfig.strictDecimal ?? false
3983
+ );
3950
3984
  const schemaBuilder = createSchemaBuilder(
3951
3985
  config.typeMap,
3952
3986
  config.zodChains,
@@ -3960,11 +3994,15 @@ function createGuard(config) {
3960
3994
  config.uniqueMap ?? {},
3961
3995
  scalarBase
3962
3996
  );
3963
- const log = config.logger ?? { warn: (msg) => console.warn(msg) };
3997
+ const log = config.logger ?? {
3998
+ warn: (msg) => console.warn(msg)
3999
+ };
3964
4000
  const wrapZodErrors = config.wrapZodErrors ?? false;
3965
4001
  function rethrowZod(err) {
3966
4002
  if (err instanceof import_zod10.ZodError) {
3967
- throw new ShapeError(`Validation failed: ${formatZodError(err)}`, { cause: err });
4003
+ throw new ShapeError(`Validation failed: ${formatZodError(err)}`, {
4004
+ cause: err
4005
+ });
3968
4006
  }
3969
4007
  throw err;
3970
4008
  }
@@ -4001,6 +4039,7 @@ function createGuard(config) {
4001
4039
  };
4002
4040
  },
4003
4041
  extension: (contextFn) => {
4042
+ const effectiveContextFn = contextFn ?? (() => ({}));
4004
4043
  const scopeRoots = /* @__PURE__ */ new Set();
4005
4044
  for (const entries of Object.values(config.scopeMap)) {
4006
4045
  for (const entry of entries) {
@@ -4008,7 +4047,7 @@ function createGuard(config) {
4008
4047
  }
4009
4048
  }
4010
4049
  const scopeCtxFn = () => {
4011
- const ctx = validateContext(contextFn());
4050
+ const ctx = validateContext(effectiveContextFn());
4012
4051
  const scopeCtx = {};
4013
4052
  for (const key of Object.keys(ctx)) {
4014
4053
  if (!scopeRoots.has(key))
@@ -4038,7 +4077,7 @@ function createGuard(config) {
4038
4077
  uniqueMap: config.uniqueMap ?? {},
4039
4078
  scopeMap: config.scopeMap,
4040
4079
  guardConfig: config.guardConfig,
4041
- contextFn,
4080
+ contextFn: effectiveContextFn,
4042
4081
  wrapZodErrors
4043
4082
  });
4044
4083
  return {