prisma-guard 1.16.0 → 1.17.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.
@@ -210,7 +210,18 @@ var SCALAR_OPERATORS = {
210
210
  BigInt: /* @__PURE__ */ new Set(["equals", "not", "gt", "gte", "lt", "lte", "in", "notIn"]),
211
211
  Boolean: /* @__PURE__ */ new Set(["equals", "not"]),
212
212
  DateTime: /* @__PURE__ */ new Set(["equals", "not", "gt", "gte", "lt", "lte", "in", "notIn"]),
213
- Bytes: /* @__PURE__ */ new Set([])
213
+ Bytes: /* @__PURE__ */ new Set([]),
214
+ Json: /* @__PURE__ */ new Set([
215
+ "equals",
216
+ "not",
217
+ "path",
218
+ "string_contains",
219
+ "string_starts_with",
220
+ "string_ends_with",
221
+ "array_contains",
222
+ "array_starts_with",
223
+ "array_ends_with"
224
+ ])
214
225
  };
215
226
  var SCALAR_LIST_OPERATORS = /* @__PURE__ */ new Set([
216
227
  "has",
@@ -229,6 +240,16 @@ var COMPARABLE_TYPES = /* @__PURE__ */ new Set([
229
240
  "String",
230
241
  "DateTime"
231
242
  ]);
243
+ var JSON_STRING_OPERATORS = /* @__PURE__ */ new Set([
244
+ "string_contains",
245
+ "string_starts_with",
246
+ "string_ends_with"
247
+ ]);
248
+ var JSON_ARRAY_OPERATORS = /* @__PURE__ */ new Set([
249
+ "array_contains",
250
+ "array_starts_with",
251
+ "array_ends_with"
252
+ ]);
232
253
  function getSupportedOperators(fieldMeta) {
233
254
  if (fieldMeta.isList)
234
255
  return [...SCALAR_LIST_OPERATORS];
@@ -281,6 +302,24 @@ function createScalarListOperatorSchema(fieldMeta, operator, enumMap, scalarBase
281
302
  }
282
303
  return z2.preprocess(coerceToArray, z2.array(itemBase));
283
304
  }
305
+ function createJsonOperatorSchema(fieldMeta, operator) {
306
+ const jsonValue = z2.unknown();
307
+ if (operator === "equals" || operator === "not") {
308
+ return !fieldMeta.isRequired ? z2.union([jsonValue, z2.null()]) : jsonValue;
309
+ }
310
+ if (operator === "path") {
311
+ return z2.preprocess(coerceToArray, z2.array(z2.string()).min(1));
312
+ }
313
+ if (JSON_STRING_OPERATORS.has(operator)) {
314
+ return z2.string();
315
+ }
316
+ if (JSON_ARRAY_OPERATORS.has(operator)) {
317
+ return jsonValue;
318
+ }
319
+ throw new ShapeError(
320
+ `Operator "${operator}" not supported for Json fields`
321
+ );
322
+ }
284
323
  function createOperatorSchema(fieldMeta, operator, enumMap, scalarBase) {
285
324
  if (fieldMeta.isList) {
286
325
  return createScalarListOperatorSchema(
@@ -307,6 +346,15 @@ function createOperatorSchema(fieldMeta, operator, enumMap, scalarBase) {
307
346
  const itemSchema = !fieldMeta.isRequired ? z2.union([enumSchema, z2.null()]) : enumSchema;
308
347
  return z2.preprocess(coerceToArray, z2.array(itemSchema));
309
348
  }
349
+ if (fieldMeta.type === "Json") {
350
+ const supportedOps2 = SCALAR_OPERATORS["Json"];
351
+ if (!supportedOps2 || !supportedOps2.has(operator)) {
352
+ throw new ShapeError(
353
+ `Operator "${operator}" not supported for type "Json"`
354
+ );
355
+ }
356
+ return createJsonOperatorSchema(fieldMeta, operator);
357
+ }
310
358
  const supportedOps = SCALAR_OPERATORS[fieldMeta.type];
311
359
  if (!supportedOps) {
312
360
  throw new ShapeError(`Unknown scalar type for operator: ${fieldMeta.type}`);
@@ -1064,13 +1112,18 @@ function validateUniqueEquality(model, where, method, uniqueMap, typeMap) {
1064
1112
  }
1065
1113
 
1066
1114
  // src/runtime/query-builder-where.ts
1067
- var UNSUPPORTED_WHERE_TYPES = /* @__PURE__ */ new Set(["Json", "Bytes"]);
1115
+ var UNSUPPORTED_WHERE_TYPES = /* @__PURE__ */ new Set(["Bytes"]);
1068
1116
  var STRING_MODE_OPS = /* @__PURE__ */ new Set([
1069
1117
  "contains",
1070
1118
  "startsWith",
1071
1119
  "endsWith",
1072
1120
  "equals"
1073
1121
  ]);
1122
+ var JSON_STRING_MODE_OPS = /* @__PURE__ */ new Set([
1123
+ "string_contains",
1124
+ "string_starts_with",
1125
+ "string_ends_with"
1126
+ ]);
1074
1127
  var MAX_WHERE_DEPTH = 10;
1075
1128
  function safeStringify(v) {
1076
1129
  if (typeof v === "bigint")
@@ -1166,6 +1219,13 @@ function mergeRelationForcedMaps(target, source) {
1166
1219
  }
1167
1220
  }
1168
1221
  }
1222
+ function isModeCompatibleOp(fieldType, op) {
1223
+ if (fieldType === "String")
1224
+ return STRING_MODE_OPS.has(op);
1225
+ if (fieldType === "Json")
1226
+ return JSON_STRING_MODE_OPS.has(op);
1227
+ return false;
1228
+ }
1169
1229
  function createWhereBuilder(typeMap, enumMap, scalarBase) {
1170
1230
  function buildWhereSchema(model, whereConfig, depth) {
1171
1231
  const currentDepth = depth ?? 0;
@@ -1368,7 +1428,7 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
1368
1428
  const opSchemas = {};
1369
1429
  const fieldForced = {};
1370
1430
  let hasClientOps = false;
1371
- let hasStringModeOp = false;
1431
+ let hasModeCompatibleOp = false;
1372
1432
  const clientOpKeys = [];
1373
1433
  let modeConfigValue = void 0;
1374
1434
  let hasModeConfig = false;
@@ -1387,8 +1447,8 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
1387
1447
  ).optional();
1388
1448
  hasClientOps = true;
1389
1449
  clientOpKeys.push(op);
1390
- if (fieldMeta.type === "String" && !fieldMeta.isList && STRING_MODE_OPS.has(op)) {
1391
- hasStringModeOp = true;
1450
+ if (!fieldMeta.isList && isModeCompatibleOp(fieldMeta.type, op)) {
1451
+ hasModeCompatibleOp = true;
1392
1452
  }
1393
1453
  } else {
1394
1454
  const actualOpValue = isForcedValue(opValue) ? opValue.value : opValue;
@@ -1407,15 +1467,15 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
1407
1467
  );
1408
1468
  }
1409
1469
  fieldForced[op] = parsed;
1410
- if (fieldMeta.type === "String" && !fieldMeta.isList && STRING_MODE_OPS.has(op)) {
1411
- hasStringModeOp = true;
1470
+ if (!fieldMeta.isList && isModeCompatibleOp(fieldMeta.type, op)) {
1471
+ hasModeCompatibleOp = true;
1412
1472
  }
1413
1473
  }
1414
1474
  }
1415
1475
  if (!hasClientOps && Object.keys(fieldForced).length === 0) {
1416
1476
  if (hasModeConfig) {
1417
1477
  throw new ShapeError(
1418
- `Where field "${fieldName}" on model "${model}" has "mode" but no operators. Add at least one operator (contains, startsWith, endsWith, equals).`
1478
+ `Where field "${fieldName}" on model "${model}" has "mode" but no operators. Add at least one compatible operator.`
1419
1479
  );
1420
1480
  }
1421
1481
  throw new ShapeError(
@@ -1423,9 +1483,9 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
1423
1483
  );
1424
1484
  }
1425
1485
  if (hasModeConfig) {
1426
- if (!hasStringModeOp) {
1486
+ if (!hasModeCompatibleOp) {
1427
1487
  throw new ShapeError(
1428
- `"mode" on where field "${fieldName}" on model "${model}" requires a compatible String operator (contains, startsWith, endsWith, equals)`
1488
+ `"mode" on where field "${fieldName}" on model "${model}" requires a compatible operator (String: contains, startsWith, endsWith, equals; Json: string_contains, string_starts_with, string_ends_with)`
1429
1489
  );
1430
1490
  }
1431
1491
  if (modeConfigValue === true) {
@@ -1443,7 +1503,7 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
1443
1503
  }
1444
1504
  fieldForced["mode"] = parsed;
1445
1505
  }
1446
- } else if (hasStringModeOp) {
1506
+ } else if (hasModeCompatibleOp) {
1447
1507
  opSchemas["mode"] = z4.enum(["default", "insensitive"]).optional();
1448
1508
  }
1449
1509
  if (hasClientOps) {
@@ -1456,7 +1516,7 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
1456
1516
  message: `At least one operator required for where field "${fieldName}"`
1457
1517
  }
1458
1518
  );
1459
- if ("equals" in opSchemas) {
1519
+ if ("equals" in opSchemas && fieldMeta.type !== "Json") {
1460
1520
  const equalsBase = createOperatorSchema(
1461
1521
  fieldMeta,
1462
1522
  "equals",