prisma-guard 1.6.1 → 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.
@@ -2224,21 +2224,67 @@ function createQueryBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
2224
2224
  }
2225
2225
  }
2226
2226
  if (shape.orderBy) {
2227
- if (method === "groupBy" && shape.orderBy === true && shape.by) {
2227
+ if (method === "groupBy" && shape.by) {
2228
2228
  const sortEnum = import_zod7.z.enum(["asc", "desc"]);
2229
- const groupByOrderFields = {};
2230
- for (const field of shape.by) {
2231
- groupByOrderFields[field] = sortEnum.optional();
2232
- }
2233
- groupByOrderFields["_count"] = sortEnum.optional();
2234
- const fieldKeys = Object.keys(groupByOrderFields);
2235
- const singleSchema = import_zod7.z.object(groupByOrderFields).strict().refine(
2236
- (v) => fieldKeys.some(
2237
- (k) => v[k] !== void 0
2238
- ),
2239
- { message: "orderBy must specify at least one field" }
2240
- );
2241
- 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
+ }
2242
2288
  } else {
2243
2289
  schemaFields["orderBy"] = argsBuilder.buildOrderBySchema(
2244
2290
  model,