@stoker-platform/utils 0.2.1
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/lib/src/access/collection.d.ts +9 -0
- package/lib/src/access/collection.js +59 -0
- package/lib/src/access/document.d.ts +3 -0
- package/lib/src/access/document.js +191 -0
- package/lib/src/access/getCollectionRestrictions.d.ts +7 -0
- package/lib/src/access/getCollectionRestrictions.js +71 -0
- package/lib/src/access/getRecordSubcollections.d.ts +2 -0
- package/lib/src/access/getRecordSubcollections.js +15 -0
- package/lib/src/access/getRelatedCollections.d.ts +2 -0
- package/lib/src/access/getRelatedCollections.js +24 -0
- package/lib/src/access/hasDependencyAccess.d.ts +2 -0
- package/lib/src/access/hasDependencyAccess.js +24 -0
- package/lib/src/access/isPaginationEnabled.d.ts +2 -0
- package/lib/src/access/isPaginationEnabled.js +35 -0
- package/lib/src/access/permissions.d.ts +2 -0
- package/lib/src/access/permissions.js +543 -0
- package/lib/src/access/read/getOne.d.ts +2 -0
- package/lib/src/access/read/getOne.js +19 -0
- package/lib/src/access/read/getSome.d.ts +2 -0
- package/lib/src/access/read/getSome.js +21 -0
- package/lib/src/access/roleHasOperationAccess.d.ts +2 -0
- package/lib/src/access/roleHasOperationAccess.js +7 -0
- package/lib/src/access/write/addRecord.d.ts +2 -0
- package/lib/src/access/write/addRecord.js +40 -0
- package/lib/src/access/write/deleteRecord.d.ts +2 -0
- package/lib/src/access/write/deleteRecord.js +26 -0
- package/lib/src/access/write/updateRecord.d.ts +2 -0
- package/lib/src/access/write/updateRecord.js +61 -0
- package/lib/src/getConfigValue.d.ts +12 -0
- package/lib/src/getConfigValue.js +83 -0
- package/lib/src/getCustomization.d.ts +4 -0
- package/lib/src/getCustomization.js +29 -0
- package/lib/src/getFieldCustomization.d.ts +7 -0
- package/lib/src/getFieldCustomization.js +3 -0
- package/lib/src/main.d.ts +60 -0
- package/lib/src/main.js +60 -0
- package/lib/src/operations/addInitialValues.d.ts +2 -0
- package/lib/src/operations/addInitialValues.js +27 -0
- package/lib/src/operations/addLowercaseFields.d.ts +2 -0
- package/lib/src/operations/addLowercaseFields.js +12 -0
- package/lib/src/operations/addRelationArrays.d.ts +2 -0
- package/lib/src/operations/addRelationArrays.js +60 -0
- package/lib/src/operations/addSystemFields.d.ts +2 -0
- package/lib/src/operations/addSystemFields.js +17 -0
- package/lib/src/operations/getDateRange.d.ts +5 -0
- package/lib/src/operations/getDateRange.js +56 -0
- package/lib/src/operations/getExtendedSchema.d.ts +2 -0
- package/lib/src/operations/getExtendedSchema.js +23 -0
- package/lib/src/operations/getFinalRecord.d.ts +1 -0
- package/lib/src/operations/getFinalRecord.js +11 -0
- package/lib/src/operations/getInputSchema.d.ts +15 -0
- package/lib/src/operations/getInputSchema.js +352 -0
- package/lib/src/operations/getLowercaseFields.d.ts +2 -0
- package/lib/src/operations/getLowercaseFields.js +15 -0
- package/lib/src/operations/getSingleFieldRelations.d.ts +2 -0
- package/lib/src/operations/getSingleFieldRelations.js +27 -0
- package/lib/src/operations/getZodSchema.d.ts +15 -0
- package/lib/src/operations/getZodSchema.js +303 -0
- package/lib/src/operations/isDeleteSentinel.d.ts +1 -0
- package/lib/src/operations/isDeleteSentinel.js +4 -0
- package/lib/src/operations/isSortingEnabled.d.ts +2 -0
- package/lib/src/operations/isSortingEnabled.js +7 -0
- package/lib/src/operations/isValidUniqueFieldValue.d.ts +1 -0
- package/lib/src/operations/isValidUniqueFieldValue.js +25 -0
- package/lib/src/operations/parseDate.d.ts +1 -0
- package/lib/src/operations/parseDate.js +4 -0
- package/lib/src/operations/prepareDenormalized.d.ts +8 -0
- package/lib/src/operations/prepareDenormalized.js +312 -0
- package/lib/src/operations/removeDeleteSentinels.d.ts +2 -0
- package/lib/src/operations/removeDeleteSentinels.js +15 -0
- package/lib/src/operations/removeDeletedFields.d.ts +2 -0
- package/lib/src/operations/removeDeletedFields.js +14 -0
- package/lib/src/operations/removeEmptyStrings.d.ts +2 -0
- package/lib/src/operations/removeEmptyStrings.js +14 -0
- package/lib/src/operations/removePrivateFields.d.ts +2 -0
- package/lib/src/operations/removePrivateFields.js +14 -0
- package/lib/src/operations/removeUndefined.d.ts +2 -0
- package/lib/src/operations/removeUndefined.js +14 -0
- package/lib/src/operations/retryOperation.d.ts +1 -0
- package/lib/src/operations/retryOperation.js +21 -0
- package/lib/src/operations/runHooks.d.ts +17 -0
- package/lib/src/operations/runHooks.js +18 -0
- package/lib/src/operations/sanitizeDownloadFilename.d.ts +1 -0
- package/lib/src/operations/sanitizeDownloadFilename.js +18 -0
- package/lib/src/operations/sanitizeEmailInput.d.ts +5 -0
- package/lib/src/operations/sanitizeEmailInput.js +73 -0
- package/lib/src/operations/updateFieldReference.d.ts +2 -0
- package/lib/src/operations/updateFieldReference.js +14 -0
- package/lib/src/operations/validateRecord.d.ts +2 -0
- package/lib/src/operations/validateRecord.js +18 -0
- package/lib/src/operations/validateStorageName.d.ts +1 -0
- package/lib/src/operations/validateStorageName.js +19 -0
- package/lib/src/schema/getAccessFields.d.ts +2 -0
- package/lib/src/schema/getAccessFields.js +62 -0
- package/lib/src/schema/getCollection.d.ts +2 -0
- package/lib/src/schema/getCollection.js +3 -0
- package/lib/src/schema/getDependencyFields.d.ts +6 -0
- package/lib/src/schema/getDependencyFields.js +22 -0
- package/lib/src/schema/getField.d.ts +2 -0
- package/lib/src/schema/getField.js +3 -0
- package/lib/src/schema/getFieldNames.d.ts +2 -0
- package/lib/src/schema/getFieldNames.js +3 -0
- package/lib/src/schema/getIndexFields.d.ts +9 -0
- package/lib/src/schema/getIndexFields.js +184 -0
- package/lib/src/schema/getInverseRelationType.d.ts +1 -0
- package/lib/src/schema/getInverseRelationType.js +15 -0
- package/lib/src/schema/getPathCollections.d.ts +2 -0
- package/lib/src/schema/getPathCollections.js +12 -0
- package/lib/src/schema/getRecordSystemFields.d.ts +2 -0
- package/lib/src/schema/getRecordSystemFields.js +11 -0
- package/lib/src/schema/getRelationLists.d.ts +5 -0
- package/lib/src/schema/getRelationLists.js +13 -0
- package/lib/src/schema/getSubcollections.d.ts +2 -0
- package/lib/src/schema/getSubcollections.js +9 -0
- package/lib/src/schema/getSystemFieldsSchema.d.ts +2 -0
- package/lib/src/schema/getSystemFieldsSchema.js +52 -0
- package/lib/src/schema/isDependencyField.d.ts +2 -0
- package/lib/src/schema/isDependencyField.js +18 -0
- package/lib/src/schema/isIncludedField.d.ts +2 -0
- package/lib/src/schema/isIncludedField.js +18 -0
- package/lib/src/schema/isRelationField.d.ts +2 -0
- package/lib/src/schema/isRelationField.js +3 -0
- package/lib/src/schema/system-fields.d.ts +2 -0
- package/lib/src/schema/system-fields.js +13 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +35 -0
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { isRelationField } from "../schema/isRelationField.js";
|
|
3
|
+
import { tryFunction } from "../getConfigValue.js";
|
|
4
|
+
import { getField } from "../schema/getField.js";
|
|
5
|
+
import { DateTime } from "luxon";
|
|
6
|
+
import { getFieldCustomization } from "../getFieldCustomization.js";
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
|
|
8
|
+
const isRelationSingle = (field, schema) => {
|
|
9
|
+
const includeFields = {};
|
|
10
|
+
if (field.includeFields) {
|
|
11
|
+
const relationCollection = schema.collections[field.collection];
|
|
12
|
+
if (!relationCollection)
|
|
13
|
+
return z.record(z.any());
|
|
14
|
+
field.includeFields.forEach((includeField) => {
|
|
15
|
+
const relationField = relationCollection.fields.find((field) => field.name === includeField);
|
|
16
|
+
if (!relationField) {
|
|
17
|
+
// eslint-disable-next-line security/detect-object-injection
|
|
18
|
+
includeFields[includeField] = z.any();
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
// eslint-disable-next-line security/detect-object-injection
|
|
22
|
+
includeFields[includeField] = getFieldSchema(relationField)?.nullable();
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return z.object({
|
|
27
|
+
Collection_Path: z.array(z.string()),
|
|
28
|
+
...includeFields,
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
const isRelationObject = (field, schema, customization, chat) => {
|
|
32
|
+
let fieldSchema = z.object({}).catchall(isRelationSingle(field, schema));
|
|
33
|
+
let skipFormRequiredValidation;
|
|
34
|
+
if (!chat && customization) {
|
|
35
|
+
const fieldCustomization = getFieldCustomization(field, customization);
|
|
36
|
+
skipFormRequiredValidation = tryFunction(fieldCustomization.admin?.skipFormRequiredValidation);
|
|
37
|
+
}
|
|
38
|
+
if (!field.required || skipFormRequiredValidation) {
|
|
39
|
+
fieldSchema = fieldSchema.optional();
|
|
40
|
+
}
|
|
41
|
+
return fieldSchema;
|
|
42
|
+
};
|
|
43
|
+
const isTimestamp = () => {
|
|
44
|
+
return z.any().refine((data) => {
|
|
45
|
+
return (data &&
|
|
46
|
+
((data.seconds !== undefined && data.nanoseconds !== undefined) ||
|
|
47
|
+
(data._seconds !== undefined && data._nanoseconds !== undefined)));
|
|
48
|
+
}, {
|
|
49
|
+
message: "Must be a valid date",
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
const getFieldSchema = (field, customization, chat) => {
|
|
53
|
+
let fieldSchema;
|
|
54
|
+
const fieldDescription = `${tryFunction(field.description)}\n\n` || "";
|
|
55
|
+
let hidden;
|
|
56
|
+
let skipFormRequiredValidation;
|
|
57
|
+
let image;
|
|
58
|
+
if (!chat && customization) {
|
|
59
|
+
const fieldCustomization = getFieldCustomization(field, customization);
|
|
60
|
+
hidden =
|
|
61
|
+
fieldCustomization.admin?.condition?.form !== undefined &&
|
|
62
|
+
!tryFunction(fieldCustomization.admin.condition.form);
|
|
63
|
+
skipFormRequiredValidation = tryFunction(fieldCustomization.admin?.skipFormRequiredValidation);
|
|
64
|
+
image = tryFunction(fieldCustomization.admin?.image);
|
|
65
|
+
}
|
|
66
|
+
switch (field.type) {
|
|
67
|
+
case "String":
|
|
68
|
+
fieldSchema = z.string();
|
|
69
|
+
if (field.length) {
|
|
70
|
+
fieldSchema = fieldSchema.length(field.length, `Must be ${field.length} character(s) long`);
|
|
71
|
+
}
|
|
72
|
+
if (field.minlength) {
|
|
73
|
+
fieldSchema = fieldSchema.min(field.minlength, `Must contain at least ${field.minlength} character(s)`);
|
|
74
|
+
}
|
|
75
|
+
if (field.maxlength) {
|
|
76
|
+
fieldSchema = fieldSchema.max(field.maxlength, `Must contain at most ${field.maxlength} character(s)`);
|
|
77
|
+
}
|
|
78
|
+
if (field.email) {
|
|
79
|
+
fieldSchema = fieldSchema.email("Must be a valid email address");
|
|
80
|
+
}
|
|
81
|
+
else if (field.uuid) {
|
|
82
|
+
fieldSchema = z.union([fieldSchema.uuid("Must be a valid UUID"), z.literal("")]);
|
|
83
|
+
}
|
|
84
|
+
else if (field.url) {
|
|
85
|
+
fieldSchema = z.union([fieldSchema.url("Must be a valid URL"), z.literal("")]);
|
|
86
|
+
}
|
|
87
|
+
else if (field.emoji) {
|
|
88
|
+
fieldSchema = z.union([fieldSchema.emoji("Must be a valid emoji"), z.literal("")]);
|
|
89
|
+
}
|
|
90
|
+
else if (field.ip) {
|
|
91
|
+
fieldSchema = z.union([fieldSchema.ip("Must be a valid IP address"), z.literal("")]);
|
|
92
|
+
}
|
|
93
|
+
else if (field.pattern) {
|
|
94
|
+
if (!chat && image) {
|
|
95
|
+
// eslint-disable-next-line security/detect-non-literal-regexp
|
|
96
|
+
fieldSchema = z.union([fieldSchema.regex(new RegExp(field.pattern)), fieldSchema.regex(/^blob:/)], {
|
|
97
|
+
message: "Must be a valid value",
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
// eslint-disable-next-line security/detect-non-literal-regexp
|
|
102
|
+
fieldSchema = fieldSchema.regex(new RegExp(field.pattern), "Must be a valid value");
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (field.values) {
|
|
106
|
+
fieldSchema = z.union([
|
|
107
|
+
z.enum(field.values, {
|
|
108
|
+
message: `Must be one of the following values: ${field.values.join(", ")}`,
|
|
109
|
+
}),
|
|
110
|
+
z.undefined(),
|
|
111
|
+
]);
|
|
112
|
+
}
|
|
113
|
+
if (fieldDescription) {
|
|
114
|
+
fieldSchema = fieldSchema.describe(fieldDescription.trim());
|
|
115
|
+
}
|
|
116
|
+
break;
|
|
117
|
+
case "Boolean":
|
|
118
|
+
fieldSchema = z.boolean();
|
|
119
|
+
if (fieldDescription) {
|
|
120
|
+
fieldSchema = fieldSchema.describe(fieldDescription.trim());
|
|
121
|
+
}
|
|
122
|
+
break;
|
|
123
|
+
case "Number":
|
|
124
|
+
if (field.autoIncrement) {
|
|
125
|
+
fieldSchema = z.union([z.undefined(), z.number().int()], {
|
|
126
|
+
message: "Must be a valid number",
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
fieldSchema = z.number();
|
|
131
|
+
if (field.min) {
|
|
132
|
+
fieldSchema = fieldSchema.min(field.min, `Must be greater than or equal to ${field.min}`);
|
|
133
|
+
}
|
|
134
|
+
if (field.max) {
|
|
135
|
+
fieldSchema = fieldSchema.max(field.max, `Must be less than or equal to ${field.max}`);
|
|
136
|
+
}
|
|
137
|
+
if (!field.decimal) {
|
|
138
|
+
fieldSchema = fieldSchema.int("Must not have decimal places");
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
if (!chat) {
|
|
142
|
+
fieldSchema = fieldSchema.refine((data) => {
|
|
143
|
+
if (!field.decimal)
|
|
144
|
+
return false;
|
|
145
|
+
const decimalPlaces = data.toString().split(".")[1]?.length || 0;
|
|
146
|
+
return decimalPlaces <= field.decimal;
|
|
147
|
+
}, {
|
|
148
|
+
message: `Must have ${field.decimal} or fewer decimal places`,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
fieldSchema = fieldSchema.describe(`${fieldDescription}Must have ${field.decimal} decimal places`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (field.values) {
|
|
156
|
+
if (!chat) {
|
|
157
|
+
fieldSchema = z.union([
|
|
158
|
+
fieldSchema.refine((data) => field.values?.includes(data), {
|
|
159
|
+
message: `Must be one of the following values: ${field.values.join(", ")}`,
|
|
160
|
+
}),
|
|
161
|
+
z.undefined(),
|
|
162
|
+
]);
|
|
163
|
+
}
|
|
164
|
+
fieldSchema = fieldSchema.describe(`${fieldDescription}Must be one of the following values: ${field.values.join(", ")}`);
|
|
165
|
+
}
|
|
166
|
+
else if (fieldDescription) {
|
|
167
|
+
fieldSchema = fieldSchema.describe(fieldDescription.trim());
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
case "Timestamp":
|
|
172
|
+
if (chat) {
|
|
173
|
+
fieldSchema = z.object({
|
|
174
|
+
seconds: z.number(),
|
|
175
|
+
nanoseconds: z.number(),
|
|
176
|
+
});
|
|
177
|
+
if (field.min && field.max) {
|
|
178
|
+
fieldSchema = fieldSchema.describe(`${fieldDescription}Must be between ${DateTime.fromMillis(field.min).toLocaleString(DateTime.DATE_MED)} and ${DateTime.fromMillis(field.max).toLocaleString(DateTime.DATE_MED)}`);
|
|
179
|
+
}
|
|
180
|
+
else if (field.min) {
|
|
181
|
+
fieldSchema = fieldSchema.describe(`${fieldDescription}Must be greater than or equal to ${DateTime.fromMillis(field.min).toLocaleString(DateTime.DATE_MED)}`);
|
|
182
|
+
}
|
|
183
|
+
else if (field.max) {
|
|
184
|
+
fieldSchema = fieldSchema.describe(`${fieldDescription}Must be less than or equal to ${DateTime.fromMillis(field.max).toLocaleString(DateTime.DATE_MED)}`);
|
|
185
|
+
}
|
|
186
|
+
else if (fieldDescription) {
|
|
187
|
+
fieldSchema = fieldSchema.describe(fieldDescription.trim());
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
fieldSchema = isTimestamp();
|
|
192
|
+
if (field.min) {
|
|
193
|
+
fieldSchema = fieldSchema.refine((data) => data.toMillis() >= field.min, {
|
|
194
|
+
message: `Must be greater than or equal to ${DateTime.fromMillis(field.min).toLocaleString(DateTime.DATE_MED)}`,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
if (field.max) {
|
|
198
|
+
fieldSchema = fieldSchema.refine((data) => data.toMillis() <= field.max, {
|
|
199
|
+
message: `Must be less than or equal to ${DateTime.fromMillis(field.max).toLocaleString(DateTime.DATE_MED)}`,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
break;
|
|
204
|
+
case "Array":
|
|
205
|
+
fieldSchema = z.array(z.any());
|
|
206
|
+
if (field.length) {
|
|
207
|
+
fieldSchema = fieldSchema.length(field.length, `Must contain ${field.length} item(s)`);
|
|
208
|
+
}
|
|
209
|
+
if (field.minlength) {
|
|
210
|
+
fieldSchema = fieldSchema.min(field.minlength, `Must contain at least ${field.minlength} item(s)`);
|
|
211
|
+
}
|
|
212
|
+
else if (field.required && !hidden) {
|
|
213
|
+
fieldSchema = fieldSchema.nonempty("Must contain at least one item");
|
|
214
|
+
}
|
|
215
|
+
if (field.maxlength) {
|
|
216
|
+
fieldSchema = fieldSchema.max(field.maxlength, `Must contain at most ${field.maxlength} item(s)`);
|
|
217
|
+
}
|
|
218
|
+
if (field.values) {
|
|
219
|
+
if (!chat) {
|
|
220
|
+
fieldSchema = fieldSchema.refine((data) => {
|
|
221
|
+
let valid = true;
|
|
222
|
+
data.forEach((value) => {
|
|
223
|
+
if (field.values && !field.values.includes(value)) {
|
|
224
|
+
valid = false;
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
return valid;
|
|
228
|
+
}, { message: `Must only contain the following values: ${field.values.join(", ")}` });
|
|
229
|
+
}
|
|
230
|
+
fieldSchema = fieldSchema.describe(`${fieldDescription}Must only contain the following values: ${field.values.join(", ")}`);
|
|
231
|
+
}
|
|
232
|
+
else if (fieldDescription) {
|
|
233
|
+
fieldSchema = fieldSchema.describe(fieldDescription.trim());
|
|
234
|
+
}
|
|
235
|
+
if (field.nullable) {
|
|
236
|
+
fieldSchema = fieldSchema.nullable();
|
|
237
|
+
}
|
|
238
|
+
break;
|
|
239
|
+
case "Map":
|
|
240
|
+
if (field.required && !hidden) {
|
|
241
|
+
if (!chat) {
|
|
242
|
+
fieldSchema = z
|
|
243
|
+
.record(z.any())
|
|
244
|
+
.refine((data) => data && typeof data === "object" && Object.keys(data).length > 0, {
|
|
245
|
+
message: "Must not be an empty value",
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
fieldSchema = z
|
|
250
|
+
.object({})
|
|
251
|
+
.catchall(z.any())
|
|
252
|
+
.describe(`${fieldDescription}Must be an object. Must contain at least one object property.`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
fieldSchema = z.object({}).catchall(z.any()).describe(`${fieldDescription}Must be an object`);
|
|
257
|
+
}
|
|
258
|
+
break;
|
|
259
|
+
case "Computed":
|
|
260
|
+
fieldSchema = z.any();
|
|
261
|
+
}
|
|
262
|
+
if (!field.required || hidden || skipFormRequiredValidation) {
|
|
263
|
+
fieldSchema = fieldSchema.optional();
|
|
264
|
+
}
|
|
265
|
+
return fieldSchema;
|
|
266
|
+
};
|
|
267
|
+
export const getInputSchema = (collection, schema, customization, chat, updateMany) => {
|
|
268
|
+
const { auth, softDelete } = collection;
|
|
269
|
+
const fields = {};
|
|
270
|
+
collection.fields.forEach((field) => {
|
|
271
|
+
if (field.type === "Embedding" || (chat && field.type === "Computed")) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (chat && (field.name === softDelete?.archivedField || field.name === softDelete?.timestampField)) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (isRelationField(field)) {
|
|
278
|
+
fields[field.name] = isRelationObject(field, schema, customization, chat);
|
|
279
|
+
if (!chat) {
|
|
280
|
+
if (field.length) {
|
|
281
|
+
fields[field.name] = fields[field.name].refine((data) => field.length && data && Object.keys(data).length === field.length, {
|
|
282
|
+
message: `Must contain ${field.length} item(s)`,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
if (field.min) {
|
|
286
|
+
fields[field.name] = fields[field.name].refine((data) => field.min && data && Object.keys(data).length >= field.min, {
|
|
287
|
+
message: `Must contain at least ${field.min} item(s)`,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
if (field.max) {
|
|
291
|
+
fields[field.name] = fields[field.name].refine((data) => field.max && (!data || Object.keys(data).length <= field.max), {
|
|
292
|
+
message: `Must contain at most ${field.max} item(s)`,
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
if (field.length) {
|
|
298
|
+
fields[field.name] = fields[field.name].describe(`Must contain ${field.length} item(s)`);
|
|
299
|
+
}
|
|
300
|
+
if (field.min) {
|
|
301
|
+
fields[field.name] = fields[field.name].describe(`Must contain at least ${field.min} item(s)`);
|
|
302
|
+
}
|
|
303
|
+
if (field.max) {
|
|
304
|
+
fields[field.name] = fields[field.name].describe(`Must contain at most ${field.max} item(s)`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
fields[field.name] = getFieldSchema(field, customization, chat);
|
|
310
|
+
}
|
|
311
|
+
if (updateMany) {
|
|
312
|
+
fields[field.name] = fields[field.name].optional();
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
if (!chat && auth) {
|
|
316
|
+
fields.operation = z.union([z.literal("create"), z.literal("update"), z.literal("delete")]).optional();
|
|
317
|
+
fields.password = z.string().optional();
|
|
318
|
+
fields.passwordConfirm = z.string().optional();
|
|
319
|
+
Object.values(schema.collections).forEach((permissionsCollection) => {
|
|
320
|
+
fields[`auth-${permissionsCollection.labels.collection}`] = z.boolean().optional();
|
|
321
|
+
fields[`operations-${permissionsCollection.labels.collection}`] = z
|
|
322
|
+
.array(z.union([z.literal("Read"), z.literal("Create"), z.literal("Update"), z.literal("Delete")]))
|
|
323
|
+
.optional();
|
|
324
|
+
fields[`restrict-${permissionsCollection.labels.collection}`] = z.boolean().optional();
|
|
325
|
+
permissionsCollection.access.attributeRestrictions?.forEach((restriction) => {
|
|
326
|
+
fields[`attribute-${permissionsCollection.labels.collection}-${restriction.type}`] = z
|
|
327
|
+
.boolean()
|
|
328
|
+
.optional();
|
|
329
|
+
});
|
|
330
|
+
permissionsCollection.access.entityRestrictions?.restrictions?.forEach((restriction) => {
|
|
331
|
+
if (restriction.type === "Individual") {
|
|
332
|
+
fields[`accessible-${permissionsCollection.labels.collection}-${permissionsCollection.labels.collection}`] = z.object({}).catchall(z.any()).optional();
|
|
333
|
+
}
|
|
334
|
+
if (restriction.type === "Parent" || restriction.type === "Parent_Property") {
|
|
335
|
+
const collectionField = getField(permissionsCollection.fields, restriction.collectionField);
|
|
336
|
+
fields[`accessible-${permissionsCollection.labels.collection}-${collectionField.collection}`] = z
|
|
337
|
+
.object({})
|
|
338
|
+
.catchall(z.any())
|
|
339
|
+
.optional();
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
const record = z.object(fields);
|
|
345
|
+
if (collection.allowSchemalessFields) {
|
|
346
|
+
return record.passthrough();
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
return record.strict();
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
/* eslint-enable @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const getLowercaseFields = (collection, fields) => {
|
|
2
|
+
const { access, recordTitleField } = collection;
|
|
3
|
+
const { serverReadOnly } = access;
|
|
4
|
+
const lowercaseFields = new Set();
|
|
5
|
+
fields.forEach((field) => {
|
|
6
|
+
if ((recordTitleField === field.name ||
|
|
7
|
+
(field.sorting &&
|
|
8
|
+
(typeof field.sorting === "boolean" ||
|
|
9
|
+
!field.sorting.roles?.every((role) => serverReadOnly?.includes(role))))) &&
|
|
10
|
+
field.type === "String") {
|
|
11
|
+
lowercaseFields.add(field);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
return lowercaseFields;
|
|
15
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { getField } from "../schema/getField.js";
|
|
2
|
+
import { isRelationField } from "../schema/isRelationField.js";
|
|
3
|
+
export const getSingleFieldRelations = (collection, fields) => {
|
|
4
|
+
const { access, queries } = collection;
|
|
5
|
+
const { serverReadOnly } = access;
|
|
6
|
+
const singleRelationFields = new Set();
|
|
7
|
+
if (queries) {
|
|
8
|
+
for (const query of queries) {
|
|
9
|
+
const queryField = getField(fields, query.field);
|
|
10
|
+
if (fields.find((field) => field.name === query.field)) {
|
|
11
|
+
if (isRelationField(queryField) && ["OneToOne", "OneToMany"].includes(queryField.type)) {
|
|
12
|
+
singleRelationFields.add(queryField);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
fields.forEach((field) => {
|
|
18
|
+
if (isRelationField(field) &&
|
|
19
|
+
["OneToOne", "OneToMany"].includes(field.type) &&
|
|
20
|
+
field.sorting &&
|
|
21
|
+
(typeof field.sorting === "boolean" ||
|
|
22
|
+
!field.sorting.roles?.every((role) => serverReadOnly?.includes(role)))) {
|
|
23
|
+
singleRelationFields.add(field);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return singleRelationFields;
|
|
27
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CollectionSchema, CollectionsSchema } from "@stoker-platform/types";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export declare const getZodSchema: (operation: "create" | "update", collection: CollectionSchema, schema: CollectionsSchema) => z.ZodObject<{
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}, z.ZodTypeAny, "passthrough">> | z.ZodObject<{
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}, "strict", z.ZodTypeAny, {
|
|
12
|
+
[x: string]: any;
|
|
13
|
+
}, {
|
|
14
|
+
[x: string]: any;
|
|
15
|
+
}>;
|