prisma-guard 1.6.0 → 1.7.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.
@@ -735,28 +735,25 @@ function mergeUniqueWhereForced(where, forced) {
735
735
  }
736
736
  function applyBuiltShape(built, body, isUniqueMethod) {
737
737
  let parseable = body;
738
+ const hasWhereInSchema = "where" in built.zodSchema.shape;
738
739
  if (isPlainObject(body)) {
739
740
  const bodyObj = body;
740
- if (isPlainObject(bodyObj.where)) {
741
- const where = { ...bodyObj.where };
742
- let modified = false;
743
- if (built.forcedOnlyWhereKeys.size > 0) {
744
- for (const key of built.forcedOnlyWhereKeys) {
745
- if (key in where) {
746
- delete where[key];
747
- modified = true;
748
- }
749
- }
750
- }
751
- if (Object.keys(where).length === 0 && hasWhereForced(built.forcedWhere)) {
741
+ if ("where" in bodyObj) {
742
+ if (!hasWhereInSchema) {
752
743
  const { where: _, ...rest } = bodyObj;
753
744
  parseable = rest;
754
- } else if (modified) {
755
- parseable = { ...bodyObj, where };
745
+ } else if (built.forcedOnlyWhereKeys.size > 0 && isPlainObject(bodyObj.where)) {
746
+ const where = { ...bodyObj.where };
747
+ for (const key of built.forcedOnlyWhereKeys) {
748
+ delete where[key];
749
+ }
750
+ if (Object.keys(where).length === 0 && hasWhereForced(built.forcedWhere)) {
751
+ const { where: _, ...rest } = bodyObj;
752
+ parseable = rest;
753
+ } else {
754
+ parseable = { ...bodyObj, where };
755
+ }
756
756
  }
757
- } else if ("where" in bodyObj && hasWhereForced(built.forcedWhere) && !built.zodSchema.shape.where) {
758
- const { where: _, ...rest } = bodyObj;
759
- parseable = rest;
760
757
  }
761
758
  }
762
759
  const validated = built.zodSchema.parse(parseable);
@@ -2227,21 +2224,67 @@ function createQueryBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
2227
2224
  }
2228
2225
  }
2229
2226
  if (shape.orderBy) {
2230
- if (method === "groupBy" && shape.orderBy === true && shape.by) {
2227
+ if (method === "groupBy" && shape.by) {
2231
2228
  const sortEnum = import_zod7.z.enum(["asc", "desc"]);
2232
- const groupByOrderFields = {};
2233
- for (const field of shape.by) {
2234
- groupByOrderFields[field] = sortEnum.optional();
2235
- }
2236
- groupByOrderFields["_count"] = sortEnum.optional();
2237
- const fieldKeys = Object.keys(groupByOrderFields);
2238
- const singleSchema = import_zod7.z.object(groupByOrderFields).strict().refine(
2239
- (v) => fieldKeys.some(
2240
- (k) => v[k] !== void 0
2241
- ),
2242
- { message: "orderBy must specify at least one field" }
2243
- );
2244
- schemaFields["orderBy"] = import_zod7.z.union([singleSchema, import_zod7.z.array(singleSchema).min(1)]).optional();
2229
+ const bySet = new Set(shape.by);
2230
+ if (shape.orderBy === true) {
2231
+ const groupByOrderFields = {};
2232
+ for (const field of shape.by) {
2233
+ groupByOrderFields[field] = sortEnum.optional();
2234
+ }
2235
+ groupByOrderFields["_count"] = sortEnum.optional();
2236
+ const fieldKeys = Object.keys(groupByOrderFields);
2237
+ const singleSchema = import_zod7.z.object(groupByOrderFields).strict().refine(
2238
+ (v) => fieldKeys.some(
2239
+ (k) => v[k] !== void 0
2240
+ ),
2241
+ { message: "orderBy must specify at least one field" }
2242
+ );
2243
+ schemaFields["orderBy"] = import_zod7.z.union([singleSchema, import_zod7.z.array(singleSchema).min(1)]).optional();
2244
+ } else {
2245
+ const groupByOrderFields = {};
2246
+ for (const [fieldName, config] of Object.entries(shape.orderBy)) {
2247
+ if (fieldName === "_count") {
2248
+ if (config === true) {
2249
+ groupByOrderFields["_count"] = sortEnum.optional();
2250
+ } else if (typeof config === "object" && config !== null) {
2251
+ const countFields = {};
2252
+ for (const countField of Object.keys(config)) {
2253
+ if (!bySet.has(countField)) {
2254
+ throw new ShapeError(
2255
+ `orderBy _count field "${countField}" must be included in "by" for groupBy`
2256
+ );
2257
+ }
2258
+ countFields[countField] = sortEnum.optional();
2259
+ }
2260
+ const countKeys = Object.keys(countFields);
2261
+ groupByOrderFields["_count"] = import_zod7.z.object(countFields).strict().refine(
2262
+ (v) => countKeys.some(
2263
+ (k) => v[k] !== void 0
2264
+ ),
2265
+ {
2266
+ message: "orderBy._count must specify at least one field"
2267
+ }
2268
+ ).optional();
2269
+ }
2270
+ continue;
2271
+ }
2272
+ if (!bySet.has(fieldName)) {
2273
+ throw new ShapeError(
2274
+ `orderBy field "${fieldName}" must be included in "by" for groupBy`
2275
+ );
2276
+ }
2277
+ groupByOrderFields[fieldName] = sortEnum.optional();
2278
+ }
2279
+ const fieldKeys = Object.keys(groupByOrderFields);
2280
+ const singleSchema = import_zod7.z.object(groupByOrderFields).strict().refine(
2281
+ (v) => fieldKeys.some(
2282
+ (k) => v[k] !== void 0
2283
+ ),
2284
+ { message: "orderBy must specify at least one field" }
2285
+ );
2286
+ schemaFields["orderBy"] = import_zod7.z.union([singleSchema, import_zod7.z.array(singleSchema).min(1)]).optional();
2287
+ }
2245
2288
  } else {
2246
2289
  schemaFields["orderBy"] = argsBuilder.buildOrderBySchema(
2247
2290
  model,