prisma-guard 1.12.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.
- package/dist/runtime/index.cjs +374 -109
- package/dist/runtime/index.cjs.map +1 -1
- package/dist/runtime/index.js +374 -109
- package/dist/runtime/index.js.map +1 -1
- package/package.json +1 -1
package/dist/runtime/index.cjs
CHANGED
|
@@ -2843,21 +2843,67 @@ 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([
|
|
2847
|
-
|
|
2848
|
-
|
|
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([
|
|
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([
|
|
2853
|
-
|
|
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([
|
|
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([
|
|
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([
|
|
2860
|
-
|
|
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
|
+
]);
|
|
2861
2907
|
var KNOWN_RELATION_WRITE_OPS = /* @__PURE__ */ new Set([
|
|
2862
2908
|
"connect",
|
|
2863
2909
|
"connectOrCreate",
|
|
@@ -2895,26 +2941,6 @@ function validateCreateCompleteness(modelName, dataConfig, typeMap, scopeFks, zo
|
|
|
2895
2941
|
return;
|
|
2896
2942
|
const zodDefaultFields = zodDefaults[modelName];
|
|
2897
2943
|
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
|
-
}
|
|
2918
2944
|
for (const [fieldName, meta] of Object.entries(modelFields)) {
|
|
2919
2945
|
if (meta.isRelation)
|
|
2920
2946
|
continue;
|
|
@@ -2930,17 +2956,6 @@ function validateCreateCompleteness(modelName, dataConfig, typeMap, scopeFks, zo
|
|
|
2930
2956
|
continue;
|
|
2931
2957
|
if (zodDefaultSet && zodDefaultSet.has(fieldName))
|
|
2932
2958
|
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;
|
|
2944
2959
|
throw new ShapeError(
|
|
2945
2960
|
`Required field "${fieldName}" on model "${modelName}" is missing from create data shape, has no default, and is not a scope FK`
|
|
2946
2961
|
);
|
|
@@ -2951,17 +2966,26 @@ function buildWhereFieldsSchema(model, config, typeMap, schemaBuilder) {
|
|
|
2951
2966
|
if (!modelFields)
|
|
2952
2967
|
throw new ShapeError(`Unknown model: ${model}`);
|
|
2953
2968
|
const fieldSchemas = {};
|
|
2969
|
+
const fieldKeys = [];
|
|
2954
2970
|
for (const [fieldName, value] of Object.entries(config)) {
|
|
2955
2971
|
if (value !== true)
|
|
2956
|
-
throw new ShapeError(
|
|
2972
|
+
throw new ShapeError(
|
|
2973
|
+
`Field "${fieldName}" in connect/where config must be true`
|
|
2974
|
+
);
|
|
2957
2975
|
const meta = modelFields[fieldName];
|
|
2958
2976
|
if (!meta)
|
|
2959
2977
|
throw new ShapeError(`Unknown field "${fieldName}" on model "${model}"`);
|
|
2960
2978
|
if (meta.isRelation)
|
|
2961
|
-
throw new ShapeError(
|
|
2979
|
+
throw new ShapeError(
|
|
2980
|
+
`Relation field "${fieldName}" cannot be used in connect/where`
|
|
2981
|
+
);
|
|
2962
2982
|
fieldSchemas[fieldName] = schemaBuilder.buildFieldSchema(model, fieldName).optional();
|
|
2983
|
+
fieldKeys.push(fieldName);
|
|
2963
2984
|
}
|
|
2964
|
-
return import_zod8.z.object(fieldSchemas).strict()
|
|
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
|
+
);
|
|
2965
2989
|
}
|
|
2966
2990
|
function buildNestedDataSchema(model, config, typeMap, schemaBuilder) {
|
|
2967
2991
|
const modelFields = typeMap[model];
|
|
@@ -2970,14 +2994,20 @@ function buildNestedDataSchema(model, config, typeMap, schemaBuilder) {
|
|
|
2970
2994
|
const fieldSchemas = {};
|
|
2971
2995
|
for (const [fieldName, value] of Object.entries(config)) {
|
|
2972
2996
|
if (value !== true)
|
|
2973
|
-
throw new ShapeError(
|
|
2997
|
+
throw new ShapeError(
|
|
2998
|
+
`Field "${fieldName}" in nested data config must be true`
|
|
2999
|
+
);
|
|
2974
3000
|
const meta = modelFields[fieldName];
|
|
2975
3001
|
if (!meta)
|
|
2976
3002
|
throw new ShapeError(`Unknown field "${fieldName}" on model "${model}"`);
|
|
2977
3003
|
if (meta.isRelation)
|
|
2978
|
-
throw new ShapeError(
|
|
3004
|
+
throw new ShapeError(
|
|
3005
|
+
`Nested relation writes inside nested data are not supported ("${model}.${fieldName}")`
|
|
3006
|
+
);
|
|
2979
3007
|
if (meta.isUpdatedAt)
|
|
2980
|
-
throw new ShapeError(
|
|
3008
|
+
throw new ShapeError(
|
|
3009
|
+
`updatedAt field "${fieldName}" cannot be used in nested data`
|
|
3010
|
+
);
|
|
2981
3011
|
let fieldSchema = schemaBuilder.buildFieldSchema(model, fieldName);
|
|
2982
3012
|
if (!meta.isRequired) {
|
|
2983
3013
|
fieldSchema = fieldSchema.nullable().optional();
|
|
@@ -2991,55 +3021,106 @@ function buildNestedDataSchema(model, config, typeMap, schemaBuilder) {
|
|
|
2991
3021
|
function buildRelationWriteSchema(model, fieldName, relatedModelName, isList, config, typeMap, schemaBuilder) {
|
|
2992
3022
|
const relatedFields = typeMap[relatedModelName];
|
|
2993
3023
|
if (!relatedFields)
|
|
2994
|
-
throw new ShapeError(
|
|
3024
|
+
throw new ShapeError(
|
|
3025
|
+
`Unknown related model "${relatedModelName}" for field "${model}.${fieldName}"`
|
|
3026
|
+
);
|
|
2995
3027
|
for (const key of Object.keys(config)) {
|
|
2996
3028
|
if (!KNOWN_RELATION_WRITE_OPS.has(key)) {
|
|
2997
|
-
throw new ShapeError(
|
|
3029
|
+
throw new ShapeError(
|
|
3030
|
+
`Unknown relation write operation "${key}" on "${model}.${fieldName}". Allowed: ${[...KNOWN_RELATION_WRITE_OPS].join(", ")}`
|
|
3031
|
+
);
|
|
2998
3032
|
}
|
|
2999
3033
|
}
|
|
3000
3034
|
const opSchemas = {};
|
|
3001
3035
|
if (config.connect !== void 0) {
|
|
3002
3036
|
if (!isPlainObject(config.connect)) {
|
|
3003
|
-
throw new ShapeError(
|
|
3037
|
+
throw new ShapeError(
|
|
3038
|
+
`connect config on "${model}.${fieldName}" must be an object of field names`
|
|
3039
|
+
);
|
|
3004
3040
|
}
|
|
3005
|
-
const connectSchema = buildWhereFieldsSchema(
|
|
3006
|
-
|
|
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();
|
|
3007
3051
|
}
|
|
3008
3052
|
if (config.connectOrCreate !== void 0) {
|
|
3009
3053
|
if (!isPlainObject(config.connectOrCreate)) {
|
|
3010
|
-
throw new ShapeError(
|
|
3054
|
+
throw new ShapeError(
|
|
3055
|
+
`connectOrCreate config on "${model}.${fieldName}" must be an object with "where" and "create"`
|
|
3056
|
+
);
|
|
3011
3057
|
}
|
|
3012
3058
|
const coc = config.connectOrCreate;
|
|
3013
3059
|
if (!coc.where || !isPlainObject(coc.where)) {
|
|
3014
|
-
throw new ShapeError(
|
|
3060
|
+
throw new ShapeError(
|
|
3061
|
+
`connectOrCreate on "${model}.${fieldName}" requires "where" object`
|
|
3062
|
+
);
|
|
3015
3063
|
}
|
|
3016
3064
|
if (!coc.create || !isPlainObject(coc.create)) {
|
|
3017
|
-
throw new ShapeError(
|
|
3065
|
+
throw new ShapeError(
|
|
3066
|
+
`connectOrCreate on "${model}.${fieldName}" requires "create" object`
|
|
3067
|
+
);
|
|
3018
3068
|
}
|
|
3019
|
-
const whereSchema = buildWhereFieldsSchema(
|
|
3020
|
-
|
|
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
|
+
);
|
|
3021
3081
|
const cocSchema = import_zod8.z.object({ where: whereSchema, create: createSchema }).strict();
|
|
3022
3082
|
opSchemas["connectOrCreate"] = isList ? import_zod8.z.union([cocSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(cocSchema))]).optional() : cocSchema.optional();
|
|
3023
3083
|
}
|
|
3024
3084
|
if (config.create !== void 0) {
|
|
3025
3085
|
if (!isPlainObject(config.create)) {
|
|
3026
|
-
throw new ShapeError(
|
|
3086
|
+
throw new ShapeError(
|
|
3087
|
+
`create config on "${model}.${fieldName}" must be an object of field names`
|
|
3088
|
+
);
|
|
3027
3089
|
}
|
|
3028
|
-
const createSchema = buildNestedDataSchema(
|
|
3029
|
-
|
|
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();
|
|
3030
3100
|
}
|
|
3031
3101
|
if (config.createMany !== void 0) {
|
|
3032
3102
|
if (!isList) {
|
|
3033
|
-
throw new ShapeError(
|
|
3103
|
+
throw new ShapeError(
|
|
3104
|
+
`createMany is only valid on to-many relations ("${model}.${fieldName}")`
|
|
3105
|
+
);
|
|
3034
3106
|
}
|
|
3035
3107
|
if (!isPlainObject(config.createMany)) {
|
|
3036
|
-
throw new ShapeError(
|
|
3108
|
+
throw new ShapeError(
|
|
3109
|
+
`createMany config on "${model}.${fieldName}" must be an object`
|
|
3110
|
+
);
|
|
3037
3111
|
}
|
|
3038
3112
|
const cmConfig = config.createMany;
|
|
3039
3113
|
if (!cmConfig.data || !isPlainObject(cmConfig.data)) {
|
|
3040
|
-
throw new ShapeError(
|
|
3114
|
+
throw new ShapeError(
|
|
3115
|
+
`createMany on "${model}.${fieldName}" requires "data" object`
|
|
3116
|
+
);
|
|
3041
3117
|
}
|
|
3042
|
-
const dataSchema = buildNestedDataSchema(
|
|
3118
|
+
const dataSchema = buildNestedDataSchema(
|
|
3119
|
+
relatedModelName,
|
|
3120
|
+
cmConfig.data,
|
|
3121
|
+
typeMap,
|
|
3122
|
+
schemaBuilder
|
|
3123
|
+
);
|
|
3043
3124
|
const cmSchemaFields = {
|
|
3044
3125
|
data: import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(dataSchema))
|
|
3045
3126
|
};
|
|
@@ -3051,111 +3132,280 @@ function buildRelationWriteSchema(model, fieldName, relatedModelName, isList, co
|
|
|
3051
3132
|
if (config.disconnect !== void 0) {
|
|
3052
3133
|
if (config.disconnect === true) {
|
|
3053
3134
|
if (isList) {
|
|
3054
|
-
throw new ShapeError(
|
|
3135
|
+
throw new ShapeError(
|
|
3136
|
+
`disconnect on to-many relation "${model}.${fieldName}" requires field config, not true`
|
|
3137
|
+
);
|
|
3055
3138
|
}
|
|
3056
3139
|
opSchemas["disconnect"] = import_zod8.z.literal(true).optional();
|
|
3057
3140
|
} else if (isPlainObject(config.disconnect)) {
|
|
3058
|
-
const disconnectSchema = buildWhereFieldsSchema(
|
|
3059
|
-
|
|
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
|
+
}
|
|
3060
3155
|
} else {
|
|
3061
|
-
throw new ShapeError(
|
|
3156
|
+
throw new ShapeError(
|
|
3157
|
+
`disconnect config on "${model}.${fieldName}" must be true (to-one) or an object of field names`
|
|
3158
|
+
);
|
|
3062
3159
|
}
|
|
3063
3160
|
}
|
|
3064
3161
|
if (config.delete !== void 0) {
|
|
3065
3162
|
if (config.delete === true) {
|
|
3066
3163
|
if (isList) {
|
|
3067
|
-
throw new ShapeError(
|
|
3164
|
+
throw new ShapeError(
|
|
3165
|
+
`delete on to-many relation "${model}.${fieldName}" requires field config, not true`
|
|
3166
|
+
);
|
|
3068
3167
|
}
|
|
3069
3168
|
opSchemas["delete"] = import_zod8.z.literal(true).optional();
|
|
3070
3169
|
} else if (isPlainObject(config.delete)) {
|
|
3071
|
-
const deleteSchema = buildWhereFieldsSchema(
|
|
3072
|
-
|
|
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
|
+
}
|
|
3073
3184
|
} else {
|
|
3074
|
-
throw new ShapeError(
|
|
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
|
+
);
|
|
3075
3213
|
}
|
|
3076
3214
|
}
|
|
3077
3215
|
if (config.set !== void 0) {
|
|
3078
3216
|
if (!isList) {
|
|
3079
|
-
throw new ShapeError(
|
|
3217
|
+
throw new ShapeError(
|
|
3218
|
+
`set is only valid on to-many relations ("${model}.${fieldName}")`
|
|
3219
|
+
);
|
|
3080
3220
|
}
|
|
3081
3221
|
if (!isPlainObject(config.set)) {
|
|
3082
|
-
throw new ShapeError(
|
|
3222
|
+
throw new ShapeError(
|
|
3223
|
+
`set config on "${model}.${fieldName}" must be an object of field names`
|
|
3224
|
+
);
|
|
3083
3225
|
}
|
|
3084
|
-
const setSchema = buildWhereFieldsSchema(
|
|
3226
|
+
const setSchema = buildWhereFieldsSchema(
|
|
3227
|
+
relatedModelName,
|
|
3228
|
+
config.set,
|
|
3229
|
+
typeMap,
|
|
3230
|
+
schemaBuilder
|
|
3231
|
+
);
|
|
3085
3232
|
opSchemas["set"] = import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(setSchema)).optional();
|
|
3086
3233
|
}
|
|
3087
3234
|
if (config.update !== void 0) {
|
|
3088
3235
|
if (!isPlainObject(config.update)) {
|
|
3089
|
-
throw new ShapeError(
|
|
3236
|
+
throw new ShapeError(
|
|
3237
|
+
`update config on "${model}.${fieldName}" must be an object`
|
|
3238
|
+
);
|
|
3090
3239
|
}
|
|
3091
3240
|
const updateConfig = config.update;
|
|
3092
3241
|
if (isList) {
|
|
3093
3242
|
if (!updateConfig.where || !isPlainObject(updateConfig.where)) {
|
|
3094
|
-
throw new ShapeError(
|
|
3243
|
+
throw new ShapeError(
|
|
3244
|
+
`update on to-many "${model}.${fieldName}" requires "where" object`
|
|
3245
|
+
);
|
|
3095
3246
|
}
|
|
3096
3247
|
if (!updateConfig.data || !isPlainObject(updateConfig.data)) {
|
|
3097
|
-
throw new ShapeError(
|
|
3248
|
+
throw new ShapeError(
|
|
3249
|
+
`update on to-many "${model}.${fieldName}" requires "data" object`
|
|
3250
|
+
);
|
|
3098
3251
|
}
|
|
3099
|
-
const whereSchema = buildWhereFieldsSchema(
|
|
3100
|
-
|
|
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
|
+
);
|
|
3101
3264
|
const updateSchema = import_zod8.z.object({ where: whereSchema, data: dataSchema }).strict();
|
|
3102
|
-
opSchemas["update"] = import_zod8.z.union([
|
|
3265
|
+
opSchemas["update"] = import_zod8.z.union([
|
|
3266
|
+
updateSchema,
|
|
3267
|
+
import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(updateSchema))
|
|
3268
|
+
]).optional();
|
|
3103
3269
|
} else {
|
|
3104
|
-
const dataSchema = buildNestedDataSchema(
|
|
3270
|
+
const dataSchema = buildNestedDataSchema(
|
|
3271
|
+
relatedModelName,
|
|
3272
|
+
updateConfig,
|
|
3273
|
+
typeMap,
|
|
3274
|
+
schemaBuilder
|
|
3275
|
+
);
|
|
3105
3276
|
opSchemas["update"] = dataSchema.optional();
|
|
3106
3277
|
}
|
|
3107
3278
|
}
|
|
3108
3279
|
if (config.upsert !== void 0) {
|
|
3109
3280
|
if (!isPlainObject(config.upsert)) {
|
|
3110
|
-
throw new ShapeError(
|
|
3281
|
+
throw new ShapeError(
|
|
3282
|
+
`upsert config on "${model}.${fieldName}" must be an object`
|
|
3283
|
+
);
|
|
3111
3284
|
}
|
|
3112
3285
|
const upsertConfig = config.upsert;
|
|
3113
|
-
if (!upsertConfig.where || !isPlainObject(upsertConfig.where)) {
|
|
3114
|
-
throw new ShapeError(`upsert on "${model}.${fieldName}" requires "where" object`);
|
|
3115
|
-
}
|
|
3116
3286
|
if (!upsertConfig.create || !isPlainObject(upsertConfig.create)) {
|
|
3117
|
-
throw new ShapeError(
|
|
3287
|
+
throw new ShapeError(
|
|
3288
|
+
`upsert on "${model}.${fieldName}" requires "create" object`
|
|
3289
|
+
);
|
|
3118
3290
|
}
|
|
3119
3291
|
if (!upsertConfig.update || !isPlainObject(upsertConfig.update)) {
|
|
3120
|
-
throw new ShapeError(
|
|
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
|
+
}
|
|
3121
3347
|
}
|
|
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
3348
|
}
|
|
3128
3349
|
if (config.updateMany !== void 0) {
|
|
3129
3350
|
if (!isList) {
|
|
3130
|
-
throw new ShapeError(
|
|
3351
|
+
throw new ShapeError(
|
|
3352
|
+
`updateMany is only valid on to-many relations ("${model}.${fieldName}")`
|
|
3353
|
+
);
|
|
3131
3354
|
}
|
|
3132
3355
|
if (!isPlainObject(config.updateMany)) {
|
|
3133
|
-
throw new ShapeError(
|
|
3356
|
+
throw new ShapeError(
|
|
3357
|
+
`updateMany config on "${model}.${fieldName}" must be an object`
|
|
3358
|
+
);
|
|
3134
3359
|
}
|
|
3135
3360
|
const umConfig = config.updateMany;
|
|
3136
3361
|
if (!umConfig.where || !isPlainObject(umConfig.where)) {
|
|
3137
|
-
throw new ShapeError(
|
|
3362
|
+
throw new ShapeError(
|
|
3363
|
+
`updateMany on "${model}.${fieldName}" requires "where" object`
|
|
3364
|
+
);
|
|
3138
3365
|
}
|
|
3139
3366
|
if (!umConfig.data || !isPlainObject(umConfig.data)) {
|
|
3140
|
-
throw new ShapeError(
|
|
3367
|
+
throw new ShapeError(
|
|
3368
|
+
`updateMany on "${model}.${fieldName}" requires "data" object`
|
|
3369
|
+
);
|
|
3141
3370
|
}
|
|
3142
|
-
const whereSchema = buildWhereFieldsSchema(
|
|
3143
|
-
|
|
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
|
+
);
|
|
3144
3383
|
const umSchema = import_zod8.z.object({ where: whereSchema, data: dataSchema }).strict();
|
|
3145
3384
|
opSchemas["updateMany"] = import_zod8.z.union([umSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(umSchema))]).optional();
|
|
3146
3385
|
}
|
|
3147
3386
|
if (config.deleteMany !== void 0) {
|
|
3148
3387
|
if (!isList) {
|
|
3149
|
-
throw new ShapeError(
|
|
3388
|
+
throw new ShapeError(
|
|
3389
|
+
`deleteMany is only valid on to-many relations ("${model}.${fieldName}")`
|
|
3390
|
+
);
|
|
3150
3391
|
}
|
|
3151
3392
|
if (!isPlainObject(config.deleteMany)) {
|
|
3152
|
-
throw new ShapeError(
|
|
3393
|
+
throw new ShapeError(
|
|
3394
|
+
`deleteMany config on "${model}.${fieldName}" must be an object`
|
|
3395
|
+
);
|
|
3153
3396
|
}
|
|
3154
3397
|
const dmConfig = config.deleteMany;
|
|
3155
3398
|
if (!dmConfig.where || !isPlainObject(dmConfig.where)) {
|
|
3156
|
-
throw new ShapeError(
|
|
3399
|
+
throw new ShapeError(
|
|
3400
|
+
`deleteMany on "${model}.${fieldName}" requires "where" object`
|
|
3401
|
+
);
|
|
3157
3402
|
}
|
|
3158
|
-
const whereSchema = buildWhereFieldsSchema(
|
|
3403
|
+
const whereSchema = buildWhereFieldsSchema(
|
|
3404
|
+
relatedModelName,
|
|
3405
|
+
dmConfig.where,
|
|
3406
|
+
typeMap,
|
|
3407
|
+
schemaBuilder
|
|
3408
|
+
);
|
|
3159
3409
|
const dmSchema = import_zod8.z.object({ where: whereSchema }).strict();
|
|
3160
3410
|
opSchemas["deleteMany"] = import_zod8.z.union([dmSchema, import_zod8.z.preprocess(coerceToArray, import_zod8.z.array(dmSchema))]).optional();
|
|
3161
3411
|
}
|
|
@@ -3175,7 +3425,9 @@ function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDef
|
|
|
3175
3425
|
throw new ShapeError(`Unknown field "${fieldName}" on model "${model}"`);
|
|
3176
3426
|
if (fieldMeta.isRelation) {
|
|
3177
3427
|
if (!isPlainObject(value)) {
|
|
3178
|
-
throw new ShapeError(
|
|
3428
|
+
throw new ShapeError(
|
|
3429
|
+
`Relation field "${fieldName}" on model "${model}" requires a relation write config object`
|
|
3430
|
+
);
|
|
3179
3431
|
}
|
|
3180
3432
|
schemaMap[fieldName] = buildRelationWriteSchema(
|
|
3181
3433
|
model,
|
|
@@ -3189,9 +3441,14 @@ function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDef
|
|
|
3189
3441
|
continue;
|
|
3190
3442
|
}
|
|
3191
3443
|
if (fieldMeta.isUpdatedAt)
|
|
3192
|
-
throw new ShapeError(
|
|
3444
|
+
throw new ShapeError(
|
|
3445
|
+
`updatedAt field "${fieldName}" cannot be used in data shape`
|
|
3446
|
+
);
|
|
3193
3447
|
if (typeof value === "function") {
|
|
3194
|
-
let baseSchema = schemaBuilder.buildBaseFieldSchema(
|
|
3448
|
+
let baseSchema = schemaBuilder.buildBaseFieldSchema(
|
|
3449
|
+
model,
|
|
3450
|
+
fieldName
|
|
3451
|
+
);
|
|
3195
3452
|
let refined;
|
|
3196
3453
|
try {
|
|
3197
3454
|
refined = value(baseSchema);
|
|
@@ -3202,7 +3459,9 @@ function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDef
|
|
|
3202
3459
|
);
|
|
3203
3460
|
}
|
|
3204
3461
|
if (!isZodSchema(refined)) {
|
|
3205
|
-
throw new ShapeError(
|
|
3462
|
+
throw new ShapeError(
|
|
3463
|
+
`Inline refine for "${model}.${fieldName}" must return a Zod schema`
|
|
3464
|
+
);
|
|
3206
3465
|
}
|
|
3207
3466
|
let fieldSchema = refined;
|
|
3208
3467
|
const handlesUndefined = schemaProducesValueForUndefined(fieldSchema);
|
|
@@ -3223,7 +3482,10 @@ function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDef
|
|
|
3223
3482
|
}
|
|
3224
3483
|
schemaMap[fieldName] = fieldSchema;
|
|
3225
3484
|
} else if (value === true) {
|
|
3226
|
-
let fieldSchema = schemaBuilder.buildFieldSchema(
|
|
3485
|
+
let fieldSchema = schemaBuilder.buildFieldSchema(
|
|
3486
|
+
model,
|
|
3487
|
+
fieldName
|
|
3488
|
+
);
|
|
3227
3489
|
const isZodDefaultField = zodDefaultSet !== void 0 && zodDefaultSet.has(fieldName);
|
|
3228
3490
|
if (mode === "create") {
|
|
3229
3491
|
if (!fieldMeta.isRequired) {
|
|
@@ -3243,7 +3505,10 @@ function buildDataSchema(model, dataConfig, mode, typeMap, schemaBuilder, zodDef
|
|
|
3243
3505
|
schemaMap[fieldName] = fieldSchema;
|
|
3244
3506
|
} else {
|
|
3245
3507
|
const actualValue = isForcedValue(value) ? value.value : value;
|
|
3246
|
-
let fieldSchema = schemaBuilder.buildFieldSchema(
|
|
3508
|
+
let fieldSchema = schemaBuilder.buildFieldSchema(
|
|
3509
|
+
model,
|
|
3510
|
+
fieldName
|
|
3511
|
+
);
|
|
3247
3512
|
if (!fieldMeta.isRequired) {
|
|
3248
3513
|
fieldSchema = fieldSchema.nullable();
|
|
3249
3514
|
}
|