prisma-guard 1.11.0 → 1.13.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.
@@ -2843,21 +2843,80 @@ var import_zod9 = require("zod");
2843
2843
  // src/runtime/model-guard-data.ts
2844
2844
  var import_zod8 = require("zod");
2845
2845
  var ALLOWED_BODY_KEYS_CREATE = /* @__PURE__ */ new Set(["data"]);
2846
- var ALLOWED_BODY_KEYS_CREATE_PROJECTION = /* @__PURE__ */ new Set(["data", "select", "include"]);
2847
- var ALLOWED_BODY_KEYS_CREATE_MANY = /* @__PURE__ */ new Set(["data", "skipDuplicates"]);
2848
- var ALLOWED_BODY_KEYS_CREATE_MANY_PROJECTION = /* @__PURE__ */ new Set(["data", "select", "include", "skipDuplicates"]);
2846
+ var ALLOWED_BODY_KEYS_CREATE_PROJECTION = /* @__PURE__ */ new Set([
2847
+ "data",
2848
+ "select",
2849
+ "include"
2850
+ ]);
2851
+ var ALLOWED_BODY_KEYS_CREATE_MANY = /* @__PURE__ */ new Set([
2852
+ "data",
2853
+ "skipDuplicates"
2854
+ ]);
2855
+ var ALLOWED_BODY_KEYS_CREATE_MANY_PROJECTION = /* @__PURE__ */ new Set([
2856
+ "data",
2857
+ "select",
2858
+ "include",
2859
+ "skipDuplicates"
2860
+ ]);
2849
2861
  var ALLOWED_BODY_KEYS_UPDATE = /* @__PURE__ */ new Set(["data", "where"]);
2850
- var ALLOWED_BODY_KEYS_UPDATE_PROJECTION = /* @__PURE__ */ new Set(["data", "where", "select", "include"]);
2862
+ var ALLOWED_BODY_KEYS_UPDATE_PROJECTION = /* @__PURE__ */ new Set([
2863
+ "data",
2864
+ "where",
2865
+ "select",
2866
+ "include"
2867
+ ]);
2851
2868
  var ALLOWED_BODY_KEYS_DELETE = /* @__PURE__ */ new Set(["where"]);
2852
- var ALLOWED_BODY_KEYS_DELETE_PROJECTION = /* @__PURE__ */ new Set(["where", "select", "include"]);
2853
- var ALLOWED_BODY_KEYS_UPSERT = /* @__PURE__ */ new Set(["where", "create", "update", "select", "include"]);
2869
+ var ALLOWED_BODY_KEYS_DELETE_PROJECTION = /* @__PURE__ */ new Set([
2870
+ "where",
2871
+ "select",
2872
+ "include"
2873
+ ]);
2874
+ var ALLOWED_BODY_KEYS_UPSERT = /* @__PURE__ */ new Set([
2875
+ "where",
2876
+ "create",
2877
+ "update",
2878
+ "select",
2879
+ "include"
2880
+ ]);
2854
2881
  var VALID_SHAPE_KEYS_CREATE = /* @__PURE__ */ new Set(["data"]);
2855
- var VALID_SHAPE_KEYS_CREATE_PROJECTION = /* @__PURE__ */ new Set(["data", "select", "include"]);
2882
+ var VALID_SHAPE_KEYS_CREATE_PROJECTION = /* @__PURE__ */ new Set([
2883
+ "data",
2884
+ "select",
2885
+ "include"
2886
+ ]);
2856
2887
  var VALID_SHAPE_KEYS_UPDATE = /* @__PURE__ */ new Set(["data", "where"]);
2857
- var VALID_SHAPE_KEYS_UPDATE_PROJECTION = /* @__PURE__ */ new Set(["data", "where", "select", "include"]);
2888
+ var VALID_SHAPE_KEYS_UPDATE_PROJECTION = /* @__PURE__ */ new Set([
2889
+ "data",
2890
+ "where",
2891
+ "select",
2892
+ "include"
2893
+ ]);
2858
2894
  var VALID_SHAPE_KEYS_DELETE = /* @__PURE__ */ new Set(["where"]);
2859
- var VALID_SHAPE_KEYS_DELETE_PROJECTION = /* @__PURE__ */ new Set(["where", "select", "include"]);
2860
- var VALID_SHAPE_KEYS_UPSERT = /* @__PURE__ */ new Set(["where", "create", "update", "select", "include"]);
2895
+ var VALID_SHAPE_KEYS_DELETE_PROJECTION = /* @__PURE__ */ new Set([
2896
+ "where",
2897
+ "select",
2898
+ "include"
2899
+ ]);
2900
+ var VALID_SHAPE_KEYS_UPSERT = /* @__PURE__ */ new Set([
2901
+ "where",
2902
+ "create",
2903
+ "update",
2904
+ "select",
2905
+ "include"
2906
+ ]);
2907
+ var KNOWN_RELATION_WRITE_OPS = /* @__PURE__ */ new Set([
2908
+ "connect",
2909
+ "connectOrCreate",
2910
+ "create",
2911
+ "createMany",
2912
+ "disconnect",
2913
+ "delete",
2914
+ "set",
2915
+ "update",
2916
+ "updateMany",
2917
+ "upsert",
2918
+ "deleteMany"
2919
+ ]);
2861
2920
  function validateMutationBodyKeys(body, allowed, method) {
2862
2921
  for (const key of Object.keys(body)) {
2863
2922
  if (!allowed.has(key)) {
@@ -2902,6 +2961,456 @@ function validateCreateCompleteness(modelName, dataConfig, typeMap, scopeFks, zo
2902
2961
  );
2903
2962
  }
2904
2963
  }
2964
+ function buildWhereFieldsSchema(model, config, typeMap, schemaBuilder) {
2965
+ const modelFields = typeMap[model];
2966
+ if (!modelFields)
2967
+ throw new ShapeError(`Unknown model: ${model}`);
2968
+ const fieldSchemas = {};
2969
+ const fieldKeys = [];
2970
+ for (const [fieldName, value] of Object.entries(config)) {
2971
+ if (value !== true)
2972
+ throw new ShapeError(
2973
+ `Field "${fieldName}" in connect/where config must be true`
2974
+ );
2975
+ const meta = modelFields[fieldName];
2976
+ if (!meta)
2977
+ throw new ShapeError(`Unknown field "${fieldName}" on model "${model}"`);
2978
+ if (meta.isRelation)
2979
+ throw new ShapeError(
2980
+ `Relation field "${fieldName}" cannot be used in connect/where`
2981
+ );
2982
+ fieldSchemas[fieldName] = schemaBuilder.buildFieldSchema(model, fieldName).optional();
2983
+ fieldKeys.push(fieldName);
2984
+ }
2985
+ return import_zod8.z.object(fieldSchemas).strict().refine(
2986
+ (v) => fieldKeys.some((k) => v[k] !== void 0),
2987
+ { message: `At least one field required in connect/where` }
2988
+ );
2989
+ }
2990
+ function buildNestedDataSchema(model, config, typeMap, schemaBuilder) {
2991
+ const modelFields = typeMap[model];
2992
+ if (!modelFields)
2993
+ throw new ShapeError(`Unknown model: ${model}`);
2994
+ const fieldSchemas = {};
2995
+ for (const [fieldName, value] of Object.entries(config)) {
2996
+ if (value !== true)
2997
+ throw new ShapeError(
2998
+ `Field "${fieldName}" in nested data config must be true`
2999
+ );
3000
+ const meta = modelFields[fieldName];
3001
+ if (!meta)
3002
+ throw new ShapeError(`Unknown field "${fieldName}" on model "${model}"`);
3003
+ if (meta.isRelation)
3004
+ throw new ShapeError(
3005
+ `Nested relation writes inside nested data are not supported ("${model}.${fieldName}")`
3006
+ );
3007
+ if (meta.isUpdatedAt)
3008
+ throw new ShapeError(
3009
+ `updatedAt field "${fieldName}" cannot be used in nested data`
3010
+ );
3011
+ let fieldSchema = schemaBuilder.buildFieldSchema(model, fieldName);
3012
+ if (!meta.isRequired) {
3013
+ fieldSchema = fieldSchema.nullable().optional();
3014
+ } else if (meta.hasDefault) {
3015
+ fieldSchema = fieldSchema.optional();
3016
+ }
3017
+ fieldSchemas[fieldName] = fieldSchema;
3018
+ }
3019
+ return import_zod8.z.object(fieldSchemas).strict();
3020
+ }
3021
+ function buildRelationWriteSchema(model, fieldName, relatedModelName, isList, config, typeMap, schemaBuilder) {
3022
+ const relatedFields = typeMap[relatedModelName];
3023
+ if (!relatedFields)
3024
+ throw new ShapeError(
3025
+ `Unknown related model "${relatedModelName}" for field "${model}.${fieldName}"`
3026
+ );
3027
+ for (const key of Object.keys(config)) {
3028
+ if (!KNOWN_RELATION_WRITE_OPS.has(key)) {
3029
+ throw new ShapeError(
3030
+ `Unknown relation write operation "${key}" on "${model}.${fieldName}". Allowed: ${[...KNOWN_RELATION_WRITE_OPS].join(", ")}`
3031
+ );
3032
+ }
3033
+ }
3034
+ const opSchemas = {};
3035
+ if (config.connect !== void 0) {
3036
+ if (!isPlainObject(config.connect)) {
3037
+ throw new ShapeError(
3038
+ `connect config on "${model}.${fieldName}" must be an object of field names`
3039
+ );
3040
+ }
3041
+ const connectSchema = buildWhereFieldsSchema(
3042
+ relatedModelName,
3043
+ config.connect,
3044
+ typeMap,
3045
+ schemaBuilder
3046
+ );
3047
+ opSchemas["connect"] = isList ? import_zod8.z.union([
3048
+ connectSchema,
3049
+ import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(connectSchema))
3050
+ ]).optional() : connectSchema.optional();
3051
+ }
3052
+ if (config.connectOrCreate !== void 0) {
3053
+ if (!isPlainObject(config.connectOrCreate)) {
3054
+ throw new ShapeError(
3055
+ `connectOrCreate config on "${model}.${fieldName}" must be an object with "where" and "create"`
3056
+ );
3057
+ }
3058
+ const coc = config.connectOrCreate;
3059
+ if (!coc.where || !isPlainObject(coc.where)) {
3060
+ throw new ShapeError(
3061
+ `connectOrCreate on "${model}.${fieldName}" requires "where" object`
3062
+ );
3063
+ }
3064
+ if (!coc.create || !isPlainObject(coc.create)) {
3065
+ throw new ShapeError(
3066
+ `connectOrCreate on "${model}.${fieldName}" requires "create" object`
3067
+ );
3068
+ }
3069
+ const whereSchema = buildWhereFieldsSchema(
3070
+ relatedModelName,
3071
+ coc.where,
3072
+ typeMap,
3073
+ schemaBuilder
3074
+ );
3075
+ const createSchema = buildNestedDataSchema(
3076
+ relatedModelName,
3077
+ coc.create,
3078
+ typeMap,
3079
+ schemaBuilder
3080
+ );
3081
+ const cocSchema = import_zod8.z.object({ where: whereSchema, create: createSchema }).strict();
3082
+ opSchemas["connectOrCreate"] = isList ? import_zod8.z.union([cocSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(cocSchema))]).optional() : cocSchema.optional();
3083
+ }
3084
+ if (config.create !== void 0) {
3085
+ if (!isPlainObject(config.create)) {
3086
+ throw new ShapeError(
3087
+ `create config on "${model}.${fieldName}" must be an object of field names`
3088
+ );
3089
+ }
3090
+ const createSchema = buildNestedDataSchema(
3091
+ relatedModelName,
3092
+ config.create,
3093
+ typeMap,
3094
+ schemaBuilder
3095
+ );
3096
+ opSchemas["create"] = isList ? import_zod8.z.union([
3097
+ createSchema,
3098
+ import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(createSchema))
3099
+ ]).optional() : createSchema.optional();
3100
+ }
3101
+ if (config.createMany !== void 0) {
3102
+ if (!isList) {
3103
+ throw new ShapeError(
3104
+ `createMany is only valid on to-many relations ("${model}.${fieldName}")`
3105
+ );
3106
+ }
3107
+ if (!isPlainObject(config.createMany)) {
3108
+ throw new ShapeError(
3109
+ `createMany config on "${model}.${fieldName}" must be an object`
3110
+ );
3111
+ }
3112
+ const cmConfig = config.createMany;
3113
+ if (!cmConfig.data || !isPlainObject(cmConfig.data)) {
3114
+ throw new ShapeError(
3115
+ `createMany on "${model}.${fieldName}" requires "data" object`
3116
+ );
3117
+ }
3118
+ const dataSchema = buildNestedDataSchema(
3119
+ relatedModelName,
3120
+ cmConfig.data,
3121
+ typeMap,
3122
+ schemaBuilder
3123
+ );
3124
+ const cmSchemaFields = {
3125
+ data: import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(dataSchema))
3126
+ };
3127
+ if ("skipDuplicates" in cmConfig) {
3128
+ cmSchemaFields["skipDuplicates"] = import_zod8.z.boolean().optional();
3129
+ }
3130
+ opSchemas["createMany"] = import_zod8.z.object(cmSchemaFields).strict().optional();
3131
+ }
3132
+ if (config.disconnect !== void 0) {
3133
+ if (config.disconnect === true) {
3134
+ if (isList) {
3135
+ throw new ShapeError(
3136
+ `disconnect on to-many relation "${model}.${fieldName}" requires field config, not true`
3137
+ );
3138
+ }
3139
+ opSchemas["disconnect"] = import_zod8.z.literal(true).optional();
3140
+ } else if (isPlainObject(config.disconnect)) {
3141
+ const disconnectSchema = buildWhereFieldsSchema(
3142
+ relatedModelName,
3143
+ config.disconnect,
3144
+ typeMap,
3145
+ schemaBuilder
3146
+ );
3147
+ if (isList) {
3148
+ opSchemas["disconnect"] = import_zod8.z.union([
3149
+ disconnectSchema,
3150
+ import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(disconnectSchema))
3151
+ ]).optional();
3152
+ } else {
3153
+ opSchemas["disconnect"] = import_zod8.z.union([import_zod8.z.literal(true), disconnectSchema]).optional();
3154
+ }
3155
+ } else {
3156
+ throw new ShapeError(
3157
+ `disconnect config on "${model}.${fieldName}" must be true (to-one) or an object of field names`
3158
+ );
3159
+ }
3160
+ }
3161
+ if (config.delete !== void 0) {
3162
+ if (config.delete === true) {
3163
+ if (isList) {
3164
+ throw new ShapeError(
3165
+ `delete on to-many relation "${model}.${fieldName}" requires field config, not true`
3166
+ );
3167
+ }
3168
+ opSchemas["delete"] = import_zod8.z.literal(true).optional();
3169
+ } else if (isPlainObject(config.delete)) {
3170
+ const deleteSchema = buildWhereFieldsSchema(
3171
+ relatedModelName,
3172
+ config.delete,
3173
+ typeMap,
3174
+ schemaBuilder
3175
+ );
3176
+ if (isList) {
3177
+ opSchemas["delete"] = import_zod8.z.union([
3178
+ deleteSchema,
3179
+ import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(deleteSchema))
3180
+ ]).optional();
3181
+ } else {
3182
+ opSchemas["delete"] = import_zod8.z.union([import_zod8.z.literal(true), deleteSchema]).optional();
3183
+ }
3184
+ } else {
3185
+ throw new ShapeError(
3186
+ `delete config on "${model}.${fieldName}" must be true (to-one) or an object of field names`
3187
+ );
3188
+ }
3189
+ }
3190
+ if (config.delete !== void 0) {
3191
+ if (config.delete === true) {
3192
+ if (isList) {
3193
+ throw new ShapeError(
3194
+ `delete on to-many relation "${model}.${fieldName}" requires field config, not true`
3195
+ );
3196
+ }
3197
+ opSchemas["delete"] = import_zod8.z.literal(true).optional();
3198
+ } else if (isPlainObject(config.delete)) {
3199
+ const deleteSchema = buildWhereFieldsSchema(
3200
+ relatedModelName,
3201
+ config.delete,
3202
+ typeMap,
3203
+ schemaBuilder
3204
+ );
3205
+ opSchemas["delete"] = isList ? import_zod8.z.union([
3206
+ deleteSchema,
3207
+ import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(deleteSchema))
3208
+ ]).optional() : deleteSchema.optional();
3209
+ } else {
3210
+ throw new ShapeError(
3211
+ `delete config on "${model}.${fieldName}" must be true (to-one) or an object of field names`
3212
+ );
3213
+ }
3214
+ }
3215
+ if (config.set !== void 0) {
3216
+ if (!isList) {
3217
+ throw new ShapeError(
3218
+ `set is only valid on to-many relations ("${model}.${fieldName}")`
3219
+ );
3220
+ }
3221
+ if (!isPlainObject(config.set)) {
3222
+ throw new ShapeError(
3223
+ `set config on "${model}.${fieldName}" must be an object of field names`
3224
+ );
3225
+ }
3226
+ const setSchema = buildWhereFieldsSchema(
3227
+ relatedModelName,
3228
+ config.set,
3229
+ typeMap,
3230
+ schemaBuilder
3231
+ );
3232
+ opSchemas["set"] = import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(setSchema)).optional();
3233
+ }
3234
+ if (config.update !== void 0) {
3235
+ if (!isPlainObject(config.update)) {
3236
+ throw new ShapeError(
3237
+ `update config on "${model}.${fieldName}" must be an object`
3238
+ );
3239
+ }
3240
+ const updateConfig = config.update;
3241
+ if (isList) {
3242
+ if (!updateConfig.where || !isPlainObject(updateConfig.where)) {
3243
+ throw new ShapeError(
3244
+ `update on to-many "${model}.${fieldName}" requires "where" object`
3245
+ );
3246
+ }
3247
+ if (!updateConfig.data || !isPlainObject(updateConfig.data)) {
3248
+ throw new ShapeError(
3249
+ `update on to-many "${model}.${fieldName}" requires "data" object`
3250
+ );
3251
+ }
3252
+ const whereSchema = buildWhereFieldsSchema(
3253
+ relatedModelName,
3254
+ updateConfig.where,
3255
+ typeMap,
3256
+ schemaBuilder
3257
+ );
3258
+ const dataSchema = buildNestedDataSchema(
3259
+ relatedModelName,
3260
+ updateConfig.data,
3261
+ typeMap,
3262
+ schemaBuilder
3263
+ );
3264
+ const updateSchema = import_zod8.z.object({ where: whereSchema, data: dataSchema }).strict();
3265
+ opSchemas["update"] = import_zod8.z.union([
3266
+ updateSchema,
3267
+ import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(updateSchema))
3268
+ ]).optional();
3269
+ } else {
3270
+ const dataSchema = buildNestedDataSchema(
3271
+ relatedModelName,
3272
+ updateConfig,
3273
+ typeMap,
3274
+ schemaBuilder
3275
+ );
3276
+ opSchemas["update"] = dataSchema.optional();
3277
+ }
3278
+ }
3279
+ if (config.upsert !== void 0) {
3280
+ if (!isPlainObject(config.upsert)) {
3281
+ throw new ShapeError(
3282
+ `upsert config on "${model}.${fieldName}" must be an object`
3283
+ );
3284
+ }
3285
+ const upsertConfig = config.upsert;
3286
+ if (!upsertConfig.create || !isPlainObject(upsertConfig.create)) {
3287
+ throw new ShapeError(
3288
+ `upsert on "${model}.${fieldName}" requires "create" object`
3289
+ );
3290
+ }
3291
+ if (!upsertConfig.update || !isPlainObject(upsertConfig.update)) {
3292
+ throw new ShapeError(
3293
+ `upsert on "${model}.${fieldName}" requires "update" object`
3294
+ );
3295
+ }
3296
+ const createSchema = buildNestedDataSchema(
3297
+ relatedModelName,
3298
+ upsertConfig.create,
3299
+ typeMap,
3300
+ schemaBuilder
3301
+ );
3302
+ const updateSchema = buildNestedDataSchema(
3303
+ relatedModelName,
3304
+ upsertConfig.update,
3305
+ typeMap,
3306
+ schemaBuilder
3307
+ );
3308
+ if (isList) {
3309
+ if (!upsertConfig.where || !isPlainObject(upsertConfig.where)) {
3310
+ throw new ShapeError(
3311
+ `upsert on to-many "${model}.${fieldName}" requires "where" object`
3312
+ );
3313
+ }
3314
+ const whereSchema = buildWhereFieldsSchema(
3315
+ relatedModelName,
3316
+ upsertConfig.where,
3317
+ typeMap,
3318
+ schemaBuilder
3319
+ );
3320
+ const upsertSchema = import_zod8.z.object({
3321
+ where: whereSchema,
3322
+ create: createSchema,
3323
+ update: updateSchema
3324
+ }).strict();
3325
+ opSchemas["upsert"] = import_zod8.z.union([
3326
+ upsertSchema,
3327
+ import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(upsertSchema))
3328
+ ]).optional();
3329
+ } else {
3330
+ if (upsertConfig.where) {
3331
+ const whereSchema = buildWhereFieldsSchema(
3332
+ relatedModelName,
3333
+ upsertConfig.where,
3334
+ typeMap,
3335
+ schemaBuilder
3336
+ );
3337
+ const upsertSchema = import_zod8.z.object({
3338
+ where: whereSchema,
3339
+ create: createSchema,
3340
+ update: updateSchema
3341
+ }).strict();
3342
+ opSchemas["upsert"] = upsertSchema.optional();
3343
+ } else {
3344
+ const upsertSchema = import_zod8.z.object({ create: createSchema, update: updateSchema }).strict();
3345
+ opSchemas["upsert"] = upsertSchema.optional();
3346
+ }
3347
+ }
3348
+ }
3349
+ if (config.updateMany !== void 0) {
3350
+ if (!isList) {
3351
+ throw new ShapeError(
3352
+ `updateMany is only valid on to-many relations ("${model}.${fieldName}")`
3353
+ );
3354
+ }
3355
+ if (!isPlainObject(config.updateMany)) {
3356
+ throw new ShapeError(
3357
+ `updateMany config on "${model}.${fieldName}" must be an object`
3358
+ );
3359
+ }
3360
+ const umConfig = config.updateMany;
3361
+ if (!umConfig.where || !isPlainObject(umConfig.where)) {
3362
+ throw new ShapeError(
3363
+ `updateMany on "${model}.${fieldName}" requires "where" object`
3364
+ );
3365
+ }
3366
+ if (!umConfig.data || !isPlainObject(umConfig.data)) {
3367
+ throw new ShapeError(
3368
+ `updateMany on "${model}.${fieldName}" requires "data" object`
3369
+ );
3370
+ }
3371
+ const whereSchema = buildWhereFieldsSchema(
3372
+ relatedModelName,
3373
+ umConfig.where,
3374
+ typeMap,
3375
+ schemaBuilder
3376
+ );
3377
+ const dataSchema = buildNestedDataSchema(
3378
+ relatedModelName,
3379
+ umConfig.data,
3380
+ typeMap,
3381
+ schemaBuilder
3382
+ );
3383
+ const umSchema = import_zod8.z.object({ where: whereSchema, data: dataSchema }).strict();
3384
+ opSchemas["updateMany"] = import_zod8.z.union([umSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(umSchema))]).optional();
3385
+ }
3386
+ if (config.deleteMany !== void 0) {
3387
+ if (!isList) {
3388
+ throw new ShapeError(
3389
+ `deleteMany is only valid on to-many relations ("${model}.${fieldName}")`
3390
+ );
3391
+ }
3392
+ if (!isPlainObject(config.deleteMany)) {
3393
+ throw new ShapeError(
3394
+ `deleteMany config on "${model}.${fieldName}" must be an object`
3395
+ );
3396
+ }
3397
+ const dmConfig = config.deleteMany;
3398
+ if (!dmConfig.where || !isPlainObject(dmConfig.where)) {
3399
+ throw new ShapeError(
3400
+ `deleteMany on "${model}.${fieldName}" requires "where" object`
3401
+ );
3402
+ }
3403
+ const whereSchema = buildWhereFieldsSchema(
3404
+ relatedModelName,
3405
+ dmConfig.where,
3406
+ typeMap,
3407
+ schemaBuilder
3408
+ );
3409
+ const dmSchema = import_zod8.z.object({ where: whereSchema }).strict();
3410
+ opSchemas["deleteMany"] = import_zod8.z.union([dmSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(dmSchema))]).optional();
3411
+ }
3412
+ return import_zod8.z.object(opSchemas).strict();
3413
+ }
2905
3414
  function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDefaults) {
2906
3415
  const modelFields = typeMap[model];
2907
3416
  if (!modelFields)
@@ -2914,12 +3423,32 @@ function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDef
2914
3423
  const fieldMeta = modelFields[fieldName];
2915
3424
  if (!fieldMeta)
2916
3425
  throw new ShapeError(`Unknown field "${fieldName}" on model "${model}"`);
2917
- if (fieldMeta.isRelation)
2918
- throw new ShapeError(`Relation field "${fieldName}" cannot be used in data shape`);
3426
+ if (fieldMeta.isRelation) {
3427
+ if (!isPlainObject(value)) {
3428
+ throw new ShapeError(
3429
+ `Relation field "${fieldName}" on model "${model}" requires a relation write config object`
3430
+ );
3431
+ }
3432
+ schemaMap[fieldName] = buildRelationWriteSchema(
3433
+ model,
3434
+ fieldName,
3435
+ fieldMeta.type,
3436
+ fieldMeta.isList,
3437
+ value,
3438
+ typeMap,
3439
+ schemaBuilder
3440
+ ).optional();
3441
+ continue;
3442
+ }
2919
3443
  if (fieldMeta.isUpdatedAt)
2920
- throw new ShapeError(`updatedAt field "${fieldName}" cannot be used in data shape`);
3444
+ throw new ShapeError(
3445
+ `updatedAt field "${fieldName}" cannot be used in data shape`
3446
+ );
2921
3447
  if (typeof value === "function") {
2922
- let baseSchema = schemaBuilder.buildBaseFieldSchema(model, fieldName);
3448
+ let baseSchema = schemaBuilder.buildBaseFieldSchema(
3449
+ model,
3450
+ fieldName
3451
+ );
2923
3452
  let refined;
2924
3453
  try {
2925
3454
  refined = value(baseSchema);
@@ -2930,7 +3459,9 @@ function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDef
2930
3459
  );
2931
3460
  }
2932
3461
  if (!isZodSchema(refined)) {
2933
- throw new ShapeError(`Inline refine for "${model}.${fieldName}" must return a Zod schema`);
3462
+ throw new ShapeError(
3463
+ `Inline refine for "${model}.${fieldName}" must return a Zod schema`
3464
+ );
2934
3465
  }
2935
3466
  let fieldSchema = refined;
2936
3467
  const handlesUndefined = schemaProducesValueForUndefined(fieldSchema);
@@ -2951,7 +3482,10 @@ function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDef
2951
3482
  }
2952
3483
  schemaMap[fieldName] = fieldSchema;
2953
3484
  } else if (value === true) {
2954
- let fieldSchema = schemaBuilder.buildFieldSchema(model, fieldName);
3485
+ let fieldSchema = schemaBuilder.buildFieldSchema(
3486
+ model,
3487
+ fieldName
3488
+ );
2955
3489
  const isZodDefaultField = zodDefaultSet !== void 0 && zodDefaultSet.has(fieldName);
2956
3490
  if (mode === "create") {
2957
3491
  if (!fieldMeta.isRequired) {
@@ -2971,7 +3505,10 @@ function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDef
2971
3505
  schemaMap[fieldName] = fieldSchema;
2972
3506
  } else {
2973
3507
  const actualValue = isForcedValue(value) ? value.value : value;
2974
- let fieldSchema = schemaBuilder.buildFieldSchema(model, fieldName);
3508
+ let fieldSchema = schemaBuilder.buildFieldSchema(
3509
+ model,
3510
+ fieldName
3511
+ );
2975
3512
  if (!fieldMeta.isRequired) {
2976
3513
  fieldSchema = fieldSchema.nullable();
2977
3514
  }