prisma-guard 1.11.0 → 1.12.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.
@@ -2858,6 +2858,19 @@ var VALID_SHAPE_KEYS_UPDATE_PROJECTION = /* @__PURE__ */ new Set(["data", "where
2858
2858
  var VALID_SHAPE_KEYS_DELETE = /* @__PURE__ */ new Set(["where"]);
2859
2859
  var VALID_SHAPE_KEYS_DELETE_PROJECTION = /* @__PURE__ */ new Set(["where", "select", "include"]);
2860
2860
  var VALID_SHAPE_KEYS_UPSERT = /* @__PURE__ */ new Set(["where", "create", "update", "select", "include"]);
2861
+ var KNOWN_RELATION_WRITE_OPS = /* @__PURE__ */ new Set([
2862
+ "connect",
2863
+ "connectOrCreate",
2864
+ "create",
2865
+ "createMany",
2866
+ "disconnect",
2867
+ "delete",
2868
+ "set",
2869
+ "update",
2870
+ "updateMany",
2871
+ "upsert",
2872
+ "deleteMany"
2873
+ ]);
2861
2874
  function validateMutationBodyKeys(body, allowed, method) {
2862
2875
  for (const key of Object.keys(body)) {
2863
2876
  if (!allowed.has(key)) {
@@ -2882,6 +2895,26 @@ function validateCreateCompleteness(modelName, dataConfig, typeMap, scopeFks, zo
2882
2895
  return;
2883
2896
  const zodDefaultFields = zodDefaults[modelName];
2884
2897
  const zodDefaultSet = zodDefaultFields ? new Set(zodDefaultFields) : void 0;
2898
+ const relationFksCoveredByShape = /* @__PURE__ */ new Set();
2899
+ for (const [fieldName, value] of Object.entries(dataConfig)) {
2900
+ const meta = modelFields[fieldName];
2901
+ if (!meta)
2902
+ continue;
2903
+ if (meta.isRelation && isPlainObject(value)) {
2904
+ const relConfig = value;
2905
+ if (relConfig.connect || relConfig.connectOrCreate || relConfig.create) {
2906
+ const fkFields = Object.keys(modelFields).filter((f) => {
2907
+ const fm = modelFields[f];
2908
+ return !fm.isRelation && fieldName !== f;
2909
+ });
2910
+ for (const fk of Object.keys(modelFields)) {
2911
+ const fm = modelFields[fk];
2912
+ if (fm.isRelation)
2913
+ continue;
2914
+ }
2915
+ }
2916
+ }
2917
+ }
2885
2918
  for (const [fieldName, meta] of Object.entries(modelFields)) {
2886
2919
  if (meta.isRelation)
2887
2920
  continue;
@@ -2897,11 +2930,237 @@ function validateCreateCompleteness(modelName, dataConfig, typeMap, scopeFks, zo
2897
2930
  continue;
2898
2931
  if (zodDefaultSet && zodDefaultSet.has(fieldName))
2899
2932
  continue;
2933
+ const coveredByRelation = Object.entries(dataConfig).some(([shapeName, shapeVal]) => {
2934
+ const shapeMeta = modelFields[shapeName];
2935
+ if (!shapeMeta || !shapeMeta.isRelation)
2936
+ return false;
2937
+ if (!isPlainObject(shapeVal))
2938
+ return false;
2939
+ const relConfig = shapeVal;
2940
+ return !!(relConfig.connect || relConfig.connectOrCreate || relConfig.create);
2941
+ });
2942
+ if (coveredByRelation)
2943
+ continue;
2900
2944
  throw new ShapeError(
2901
2945
  `Required field "${fieldName}" on model "${modelName}" is missing from create data shape, has no default, and is not a scope FK`
2902
2946
  );
2903
2947
  }
2904
2948
  }
2949
+ function buildWhereFieldsSchema(model, config, typeMap, schemaBuilder) {
2950
+ const modelFields = typeMap[model];
2951
+ if (!modelFields)
2952
+ throw new ShapeError(`Unknown model: ${model}`);
2953
+ const fieldSchemas = {};
2954
+ for (const [fieldName, value] of Object.entries(config)) {
2955
+ if (value !== true)
2956
+ throw new ShapeError(`Field "${fieldName}" in connect/where config must be true`);
2957
+ const meta = modelFields[fieldName];
2958
+ if (!meta)
2959
+ throw new ShapeError(`Unknown field "${fieldName}" on model "${model}"`);
2960
+ if (meta.isRelation)
2961
+ throw new ShapeError(`Relation field "${fieldName}" cannot be used in connect/where`);
2962
+ fieldSchemas[fieldName] = schemaBuilder.buildFieldSchema(model, fieldName).optional();
2963
+ }
2964
+ return import_zod8.z.object(fieldSchemas).strict();
2965
+ }
2966
+ function buildNestedDataSchema(model, config, typeMap, schemaBuilder) {
2967
+ const modelFields = typeMap[model];
2968
+ if (!modelFields)
2969
+ throw new ShapeError(`Unknown model: ${model}`);
2970
+ const fieldSchemas = {};
2971
+ for (const [fieldName, value] of Object.entries(config)) {
2972
+ if (value !== true)
2973
+ throw new ShapeError(`Field "${fieldName}" in nested data config must be true`);
2974
+ const meta = modelFields[fieldName];
2975
+ if (!meta)
2976
+ throw new ShapeError(`Unknown field "${fieldName}" on model "${model}"`);
2977
+ if (meta.isRelation)
2978
+ throw new ShapeError(`Nested relation writes inside nested data are not supported ("${model}.${fieldName}")`);
2979
+ if (meta.isUpdatedAt)
2980
+ throw new ShapeError(`updatedAt field "${fieldName}" cannot be used in nested data`);
2981
+ let fieldSchema = schemaBuilder.buildFieldSchema(model, fieldName);
2982
+ if (!meta.isRequired) {
2983
+ fieldSchema = fieldSchema.nullable().optional();
2984
+ } else if (meta.hasDefault) {
2985
+ fieldSchema = fieldSchema.optional();
2986
+ }
2987
+ fieldSchemas[fieldName] = fieldSchema;
2988
+ }
2989
+ return import_zod8.z.object(fieldSchemas).strict();
2990
+ }
2991
+ function buildRelationWriteSchema(model, fieldName, relatedModelName, isList, config, typeMap, schemaBuilder) {
2992
+ const relatedFields = typeMap[relatedModelName];
2993
+ if (!relatedFields)
2994
+ throw new ShapeError(`Unknown related model "${relatedModelName}" for field "${model}.${fieldName}"`);
2995
+ for (const key of Object.keys(config)) {
2996
+ if (!KNOWN_RELATION_WRITE_OPS.has(key)) {
2997
+ throw new ShapeError(`Unknown relation write operation "${key}" on "${model}.${fieldName}". Allowed: ${[...KNOWN_RELATION_WRITE_OPS].join(", ")}`);
2998
+ }
2999
+ }
3000
+ const opSchemas = {};
3001
+ if (config.connect !== void 0) {
3002
+ if (!isPlainObject(config.connect)) {
3003
+ throw new ShapeError(`connect config on "${model}.${fieldName}" must be an object of field names`);
3004
+ }
3005
+ const connectSchema = buildWhereFieldsSchema(relatedModelName, config.connect, typeMap, schemaBuilder);
3006
+ opSchemas["connect"] = isList ? import_zod8.z.union([connectSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(connectSchema))]).optional() : connectSchema.optional();
3007
+ }
3008
+ if (config.connectOrCreate !== void 0) {
3009
+ if (!isPlainObject(config.connectOrCreate)) {
3010
+ throw new ShapeError(`connectOrCreate config on "${model}.${fieldName}" must be an object with "where" and "create"`);
3011
+ }
3012
+ const coc = config.connectOrCreate;
3013
+ if (!coc.where || !isPlainObject(coc.where)) {
3014
+ throw new ShapeError(`connectOrCreate on "${model}.${fieldName}" requires "where" object`);
3015
+ }
3016
+ if (!coc.create || !isPlainObject(coc.create)) {
3017
+ throw new ShapeError(`connectOrCreate on "${model}.${fieldName}" requires "create" object`);
3018
+ }
3019
+ const whereSchema = buildWhereFieldsSchema(relatedModelName, coc.where, typeMap, schemaBuilder);
3020
+ const createSchema = buildNestedDataSchema(relatedModelName, coc.create, typeMap, schemaBuilder);
3021
+ const cocSchema = import_zod8.z.object({ where: whereSchema, create: createSchema }).strict();
3022
+ opSchemas["connectOrCreate"] = isList ? import_zod8.z.union([cocSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(cocSchema))]).optional() : cocSchema.optional();
3023
+ }
3024
+ if (config.create !== void 0) {
3025
+ if (!isPlainObject(config.create)) {
3026
+ throw new ShapeError(`create config on "${model}.${fieldName}" must be an object of field names`);
3027
+ }
3028
+ const createSchema = buildNestedDataSchema(relatedModelName, config.create, typeMap, schemaBuilder);
3029
+ opSchemas["create"] = isList ? import_zod8.z.union([createSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(createSchema))]).optional() : createSchema.optional();
3030
+ }
3031
+ if (config.createMany !== void 0) {
3032
+ if (!isList) {
3033
+ throw new ShapeError(`createMany is only valid on to-many relations ("${model}.${fieldName}")`);
3034
+ }
3035
+ if (!isPlainObject(config.createMany)) {
3036
+ throw new ShapeError(`createMany config on "${model}.${fieldName}" must be an object`);
3037
+ }
3038
+ const cmConfig = config.createMany;
3039
+ if (!cmConfig.data || !isPlainObject(cmConfig.data)) {
3040
+ throw new ShapeError(`createMany on "${model}.${fieldName}" requires "data" object`);
3041
+ }
3042
+ const dataSchema = buildNestedDataSchema(relatedModelName, cmConfig.data, typeMap, schemaBuilder);
3043
+ const cmSchemaFields = {
3044
+ data: import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(dataSchema))
3045
+ };
3046
+ if ("skipDuplicates" in cmConfig) {
3047
+ cmSchemaFields["skipDuplicates"] = import_zod8.z.boolean().optional();
3048
+ }
3049
+ opSchemas["createMany"] = import_zod8.z.object(cmSchemaFields).strict().optional();
3050
+ }
3051
+ if (config.disconnect !== void 0) {
3052
+ if (config.disconnect === true) {
3053
+ if (isList) {
3054
+ throw new ShapeError(`disconnect on to-many relation "${model}.${fieldName}" requires field config, not true`);
3055
+ }
3056
+ opSchemas["disconnect"] = import_zod8.z.literal(true).optional();
3057
+ } else if (isPlainObject(config.disconnect)) {
3058
+ const disconnectSchema = buildWhereFieldsSchema(relatedModelName, config.disconnect, typeMap, schemaBuilder);
3059
+ opSchemas["disconnect"] = isList ? import_zod8.z.union([disconnectSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(disconnectSchema))]).optional() : disconnectSchema.optional();
3060
+ } else {
3061
+ throw new ShapeError(`disconnect config on "${model}.${fieldName}" must be true (to-one) or an object of field names`);
3062
+ }
3063
+ }
3064
+ if (config.delete !== void 0) {
3065
+ if (config.delete === true) {
3066
+ if (isList) {
3067
+ throw new ShapeError(`delete on to-many relation "${model}.${fieldName}" requires field config, not true`);
3068
+ }
3069
+ opSchemas["delete"] = import_zod8.z.literal(true).optional();
3070
+ } else if (isPlainObject(config.delete)) {
3071
+ const deleteSchema = buildWhereFieldsSchema(relatedModelName, config.delete, typeMap, schemaBuilder);
3072
+ opSchemas["delete"] = isList ? import_zod8.z.union([deleteSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(deleteSchema))]).optional() : deleteSchema.optional();
3073
+ } else {
3074
+ throw new ShapeError(`delete config on "${model}.${fieldName}" must be true (to-one) or an object of field names`);
3075
+ }
3076
+ }
3077
+ if (config.set !== void 0) {
3078
+ if (!isList) {
3079
+ throw new ShapeError(`set is only valid on to-many relations ("${model}.${fieldName}")`);
3080
+ }
3081
+ if (!isPlainObject(config.set)) {
3082
+ throw new ShapeError(`set config on "${model}.${fieldName}" must be an object of field names`);
3083
+ }
3084
+ const setSchema = buildWhereFieldsSchema(relatedModelName, config.set, typeMap, schemaBuilder);
3085
+ opSchemas["set"] = import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(setSchema)).optional();
3086
+ }
3087
+ if (config.update !== void 0) {
3088
+ if (!isPlainObject(config.update)) {
3089
+ throw new ShapeError(`update config on "${model}.${fieldName}" must be an object`);
3090
+ }
3091
+ const updateConfig = config.update;
3092
+ if (isList) {
3093
+ if (!updateConfig.where || !isPlainObject(updateConfig.where)) {
3094
+ throw new ShapeError(`update on to-many "${model}.${fieldName}" requires "where" object`);
3095
+ }
3096
+ if (!updateConfig.data || !isPlainObject(updateConfig.data)) {
3097
+ throw new ShapeError(`update on to-many "${model}.${fieldName}" requires "data" object`);
3098
+ }
3099
+ const whereSchema = buildWhereFieldsSchema(relatedModelName, updateConfig.where, typeMap, schemaBuilder);
3100
+ const dataSchema = buildNestedDataSchema(relatedModelName, updateConfig.data, typeMap, schemaBuilder);
3101
+ const updateSchema = import_zod8.z.object({ where: whereSchema, data: dataSchema }).strict();
3102
+ opSchemas["update"] = import_zod8.z.union([updateSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(updateSchema))]).optional();
3103
+ } else {
3104
+ const dataSchema = buildNestedDataSchema(relatedModelName, updateConfig, typeMap, schemaBuilder);
3105
+ opSchemas["update"] = dataSchema.optional();
3106
+ }
3107
+ }
3108
+ if (config.upsert !== void 0) {
3109
+ if (!isPlainObject(config.upsert)) {
3110
+ throw new ShapeError(`upsert config on "${model}.${fieldName}" must be an object`);
3111
+ }
3112
+ const upsertConfig = config.upsert;
3113
+ if (!upsertConfig.where || !isPlainObject(upsertConfig.where)) {
3114
+ throw new ShapeError(`upsert on "${model}.${fieldName}" requires "where" object`);
3115
+ }
3116
+ if (!upsertConfig.create || !isPlainObject(upsertConfig.create)) {
3117
+ throw new ShapeError(`upsert on "${model}.${fieldName}" requires "create" object`);
3118
+ }
3119
+ if (!upsertConfig.update || !isPlainObject(upsertConfig.update)) {
3120
+ throw new ShapeError(`upsert on "${model}.${fieldName}" requires "update" object`);
3121
+ }
3122
+ const whereSchema = buildWhereFieldsSchema(relatedModelName, upsertConfig.where, typeMap, schemaBuilder);
3123
+ const createSchema = buildNestedDataSchema(relatedModelName, upsertConfig.create, typeMap, schemaBuilder);
3124
+ const updateSchema = buildNestedDataSchema(relatedModelName, upsertConfig.update, typeMap, schemaBuilder);
3125
+ const upsertSchema = import_zod8.z.object({ where: whereSchema, create: createSchema, update: updateSchema }).strict();
3126
+ opSchemas["upsert"] = isList ? import_zod8.z.union([upsertSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(upsertSchema))]).optional() : upsertSchema.optional();
3127
+ }
3128
+ if (config.updateMany !== void 0) {
3129
+ if (!isList) {
3130
+ throw new ShapeError(`updateMany is only valid on to-many relations ("${model}.${fieldName}")`);
3131
+ }
3132
+ if (!isPlainObject(config.updateMany)) {
3133
+ throw new ShapeError(`updateMany config on "${model}.${fieldName}" must be an object`);
3134
+ }
3135
+ const umConfig = config.updateMany;
3136
+ if (!umConfig.where || !isPlainObject(umConfig.where)) {
3137
+ throw new ShapeError(`updateMany on "${model}.${fieldName}" requires "where" object`);
3138
+ }
3139
+ if (!umConfig.data || !isPlainObject(umConfig.data)) {
3140
+ throw new ShapeError(`updateMany on "${model}.${fieldName}" requires "data" object`);
3141
+ }
3142
+ const whereSchema = buildWhereFieldsSchema(relatedModelName, umConfig.where, typeMap, schemaBuilder);
3143
+ const dataSchema = buildNestedDataSchema(relatedModelName, umConfig.data, typeMap, schemaBuilder);
3144
+ const umSchema = import_zod8.z.object({ where: whereSchema, data: dataSchema }).strict();
3145
+ opSchemas["updateMany"] = import_zod8.z.union([umSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(umSchema))]).optional();
3146
+ }
3147
+ if (config.deleteMany !== void 0) {
3148
+ if (!isList) {
3149
+ throw new ShapeError(`deleteMany is only valid on to-many relations ("${model}.${fieldName}")`);
3150
+ }
3151
+ if (!isPlainObject(config.deleteMany)) {
3152
+ throw new ShapeError(`deleteMany config on "${model}.${fieldName}" must be an object`);
3153
+ }
3154
+ const dmConfig = config.deleteMany;
3155
+ if (!dmConfig.where || !isPlainObject(dmConfig.where)) {
3156
+ throw new ShapeError(`deleteMany on "${model}.${fieldName}" requires "where" object`);
3157
+ }
3158
+ const whereSchema = buildWhereFieldsSchema(relatedModelName, dmConfig.where, typeMap, schemaBuilder);
3159
+ const dmSchema = import_zod8.z.object({ where: whereSchema }).strict();
3160
+ opSchemas["deleteMany"] = import_zod8.z.union([dmSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(dmSchema))]).optional();
3161
+ }
3162
+ return import_zod8.z.object(opSchemas).strict();
3163
+ }
2905
3164
  function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDefaults) {
2906
3165
  const modelFields = typeMap[model];
2907
3166
  if (!modelFields)
@@ -2914,8 +3173,21 @@ function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDef
2914
3173
  const fieldMeta = modelFields[fieldName];
2915
3174
  if (!fieldMeta)
2916
3175
  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`);
3176
+ if (fieldMeta.isRelation) {
3177
+ if (!isPlainObject(value)) {
3178
+ throw new ShapeError(`Relation field "${fieldName}" on model "${model}" requires a relation write config object`);
3179
+ }
3180
+ schemaMap[fieldName] = buildRelationWriteSchema(
3181
+ model,
3182
+ fieldName,
3183
+ fieldMeta.type,
3184
+ fieldMeta.isList,
3185
+ value,
3186
+ typeMap,
3187
+ schemaBuilder
3188
+ ).optional();
3189
+ continue;
3190
+ }
2919
3191
  if (fieldMeta.isUpdatedAt)
2920
3192
  throw new ShapeError(`updatedAt field "${fieldName}" cannot be used in data shape`);
2921
3193
  if (typeof value === "function") {