@stndrds/schema 1.0.0-alpha.188 → 1.0.0-alpha.189
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/chunk-FQ2ZYOVS.js +688 -0
- package/dist/chunk-LTHSU4NF.mjs +639 -0
- package/dist/{helpers-BjuFhbQQ.d.mts → helpers-HrKmSkLi.d.mts} +7 -1
- package/dist/{helpers-CSuoa3kj.d.ts → helpers-Y9rNQ6sz.d.ts} +7 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +160 -475
- package/dist/index.mjs +3 -426
- package/dist/validation/all.d.mts +1 -1
- package/dist/validation/all.d.ts +1 -1
- package/dist/validation/all.js +7 -7
- package/dist/validation/all.mjs +1 -1
- package/dist/validation/object/index.d.mts +1 -1
- package/dist/validation/object/index.d.ts +1 -1
- package/dist/validation/object/index.js +13 -13
- package/dist/validation/object/index.mjs +1 -1
- package/package.json +2 -2
- package/dist/chunk-64R5X3DF.js +0 -221
- package/dist/chunk-6JEQE4IP.mjs +0 -208
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkMAKSIK3P_js = require('./chunk-MAKSIK3P.js');
|
|
4
|
+
var chunkJ5HRK3WD_js = require('./chunk-J5HRK3WD.js');
|
|
5
|
+
var chunkTKN73433_js = require('./chunk-TKN73433.js');
|
|
6
|
+
var chunkD7WFIGXW_js = require('./chunk-D7WFIGXW.js');
|
|
7
|
+
var chunk3JINFUN3_js = require('./chunk-3JINFUN3.js');
|
|
8
|
+
var chunkQWZHR2OO_js = require('./chunk-QWZHR2OO.js');
|
|
9
|
+
var chunkEZHMVZQ4_js = require('./chunk-EZHMVZQ4.js');
|
|
10
|
+
var chunkVXOTYFEW_js = require('./chunk-VXOTYFEW.js');
|
|
11
|
+
var chunkOAQWRMTP_js = require('./chunk-OAQWRMTP.js');
|
|
12
|
+
var chunkACQ3TUFK_js = require('./chunk-ACQ3TUFK.js');
|
|
13
|
+
var chunkDC7YYE3U_js = require('./chunk-DC7YYE3U.js');
|
|
14
|
+
var chunkROAZLZA2_js = require('./chunk-ROAZLZA2.js');
|
|
15
|
+
var chunkCCB4OY2O_js = require('./chunk-CCB4OY2O.js');
|
|
16
|
+
var chunk7A3Y6O32_js = require('./chunk-7A3Y6O32.js');
|
|
17
|
+
var chunkP2BE2A2G_js = require('./chunk-P2BE2A2G.js');
|
|
18
|
+
var chunkT225DB55_js = require('./chunk-T225DB55.js');
|
|
19
|
+
var zod = require('zod');
|
|
20
|
+
|
|
21
|
+
function isEmptyValue(value) {
|
|
22
|
+
if (value === null || value === void 0) return true;
|
|
23
|
+
if (typeof value === "string" && value.trim() === "") return true;
|
|
24
|
+
if (value instanceof Date) return false;
|
|
25
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
26
|
+
return Object.values(value).every(
|
|
27
|
+
(v) => v === null || v === void 0 || typeof v === "string" && v.trim() === ""
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
function withEmptyToNull(validator) {
|
|
33
|
+
return zod.z.preprocess((val) => isEmptyValue(val) ? null : val, validator.nullish());
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/validation/object/attribute-factory.ts
|
|
37
|
+
function createAttributeValidator(attr, messages = chunkT225DB55_js.DEFAULT_VALIDATION_MESSAGES) {
|
|
38
|
+
switch (attr.type) {
|
|
39
|
+
case "text":
|
|
40
|
+
return chunk3JINFUN3_js.createTextValidator(attr, messages);
|
|
41
|
+
case "textarea":
|
|
42
|
+
return chunk3JINFUN3_js.createTextAreaValidator(attr, messages);
|
|
43
|
+
case "richtext":
|
|
44
|
+
return chunkQWZHR2OO_js.createRichtextValidator(attr, messages);
|
|
45
|
+
case "number":
|
|
46
|
+
return chunkTKN73433_js.createNumberValidator(attr, messages);
|
|
47
|
+
case "checkbox":
|
|
48
|
+
return chunkMAKSIK3P_js.createCheckboxValidator(attr, messages);
|
|
49
|
+
case "date":
|
|
50
|
+
return chunkJ5HRK3WD_js.createDateValidator(attr, messages);
|
|
51
|
+
case "phone":
|
|
52
|
+
return chunk7A3Y6O32_js.createPhoneValidator(attr, messages);
|
|
53
|
+
case "currency":
|
|
54
|
+
return chunkDC7YYE3U_js.createCurrencyValidator(attr, messages);
|
|
55
|
+
case "status":
|
|
56
|
+
return chunkEZHMVZQ4_js.createStatusValidator(attr, messages);
|
|
57
|
+
case "location":
|
|
58
|
+
return chunkCCB4OY2O_js.createLocationValidator(attr, messages);
|
|
59
|
+
case "select":
|
|
60
|
+
return chunkEZHMVZQ4_js.createSelectValidator(attr, messages);
|
|
61
|
+
case "multiselect":
|
|
62
|
+
return chunkEZHMVZQ4_js.createMultiselectValidator(attr, messages);
|
|
63
|
+
case "file":
|
|
64
|
+
return chunkROAZLZA2_js.createFileValidator(attr, messages);
|
|
65
|
+
case "user":
|
|
66
|
+
return chunkVXOTYFEW_js.createUserValidator(attr, messages);
|
|
67
|
+
case "relation":
|
|
68
|
+
return chunkP2BE2A2G_js.createRelationValidator(attr, messages);
|
|
69
|
+
case "rating":
|
|
70
|
+
return chunkD7WFIGXW_js.createRatingValidator(attr, messages);
|
|
71
|
+
case "formula":
|
|
72
|
+
return chunkOAQWRMTP_js.createFormulaValidator(attr, messages);
|
|
73
|
+
case "rollup":
|
|
74
|
+
return chunkACQ3TUFK_js.createRollupValidator(attr, messages);
|
|
75
|
+
default:
|
|
76
|
+
return zod.z.unknown();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function createFormAttributeValidator(attr, messages = chunkT225DB55_js.DEFAULT_VALIDATION_MESSAGES) {
|
|
80
|
+
const validator = createAttributeValidator(attr, messages);
|
|
81
|
+
if (!attr.required) {
|
|
82
|
+
return withEmptyToNull(validator);
|
|
83
|
+
}
|
|
84
|
+
return validator;
|
|
85
|
+
}
|
|
86
|
+
var objectValidatorCache = /* @__PURE__ */ new WeakMap();
|
|
87
|
+
var draftValidatorCache = /* @__PURE__ */ new WeakMap();
|
|
88
|
+
function createObjectValidator(objectDef) {
|
|
89
|
+
const cached = objectValidatorCache.get(objectDef);
|
|
90
|
+
if (cached) return cached;
|
|
91
|
+
const shape = {};
|
|
92
|
+
for (const attr of objectDef.attributes) {
|
|
93
|
+
const validator = createAttributeValidator(attr);
|
|
94
|
+
shape[attr.name] = attr.required ? validator : withEmptyToNull(validator);
|
|
95
|
+
}
|
|
96
|
+
const schema = zod.z.object(shape).passthrough();
|
|
97
|
+
objectValidatorCache.set(objectDef, schema);
|
|
98
|
+
return schema;
|
|
99
|
+
}
|
|
100
|
+
function createDraftValidator(objectDef) {
|
|
101
|
+
const cached = draftValidatorCache.get(objectDef);
|
|
102
|
+
if (cached) return cached;
|
|
103
|
+
const shape = {};
|
|
104
|
+
for (const attr of objectDef.attributes) {
|
|
105
|
+
const validator = createAttributeValidator(attr);
|
|
106
|
+
shape[attr.name] = withEmptyToNull(validator);
|
|
107
|
+
}
|
|
108
|
+
const schema = zod.z.object(shape).passthrough();
|
|
109
|
+
draftValidatorCache.set(objectDef, schema);
|
|
110
|
+
return schema;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// src/exceptions.ts
|
|
114
|
+
var SchemaErrorCode = {
|
|
115
|
+
// Generic
|
|
116
|
+
UNKNOWN: "SCHEMA_UNKNOWN_ERROR",
|
|
117
|
+
// Not Found
|
|
118
|
+
OBJECT_NOT_FOUND: "SCHEMA_OBJECT_NOT_FOUND",
|
|
119
|
+
ATTRIBUTE_NOT_FOUND: "SCHEMA_ATTRIBUTE_NOT_FOUND",
|
|
120
|
+
RECORD_NOT_FOUND: "SCHEMA_RECORD_NOT_FOUND",
|
|
121
|
+
ROLE_NOT_FOUND: "SCHEMA_ROLE_NOT_FOUND",
|
|
122
|
+
MEMORY_NOT_FOUND: "SCHEMA_MEMORY_NOT_FOUND",
|
|
123
|
+
// Validation
|
|
124
|
+
VALIDATION_FAILED: "SCHEMA_VALIDATION_FAILED",
|
|
125
|
+
INVALID_ATTRIBUTE_NAME: "SCHEMA_INVALID_ATTRIBUTE_NAME",
|
|
126
|
+
INVALID_OBJECT_NAME: "SCHEMA_INVALID_OBJECT_NAME",
|
|
127
|
+
// Protected Resources
|
|
128
|
+
PROTECTED_OBJECT: "SCHEMA_PROTECTED_OBJECT",
|
|
129
|
+
PROTECTED_ATTRIBUTE: "SCHEMA_PROTECTED_ATTRIBUTE",
|
|
130
|
+
PROTECTED_VIEW: "SCHEMA_PROTECTED_VIEW",
|
|
131
|
+
PROTECTED_ROLE: "SCHEMA_PROTECTED_ROLE",
|
|
132
|
+
// Permissions
|
|
133
|
+
FORBIDDEN: "SCHEMA_FORBIDDEN",
|
|
134
|
+
ACCESS_DENIED: "SCHEMA_ACCESS_DENIED",
|
|
135
|
+
// Sync
|
|
136
|
+
SYNC_FAILED: "SCHEMA_SYNC_FAILED",
|
|
137
|
+
SYNC_CONFLICT: "SCHEMA_SYNC_CONFLICT",
|
|
138
|
+
SYNC_CASCADE: "SCHEMA_SYNC_CASCADE",
|
|
139
|
+
ORPHAN_SYSTEM_ATTRIBUTE: "SCHEMA_ORPHAN_SYSTEM_ATTRIBUTE",
|
|
140
|
+
// System Entity Immutability
|
|
141
|
+
SYSTEM_ENTITY_IMMUTABLE: "SCHEMA_SYSTEM_ENTITY_IMMUTABLE",
|
|
142
|
+
// L2 — migration system simplification (2026-05-17)
|
|
143
|
+
DESTRUCTIVE_NOT_ALLOWED: "SCHEMA_DESTRUCTIVE_NOT_ALLOWED",
|
|
144
|
+
MIGRATION_TIMEOUT: "SCHEMA_MIGRATION_TIMEOUT",
|
|
145
|
+
CHANGE_TYPE_NOT_SUPPORTED: "SCHEMA_CHANGE_TYPE_NOT_SUPPORTED",
|
|
146
|
+
// Duplicates
|
|
147
|
+
DUPLICATE_OBJECT: "SCHEMA_DUPLICATE_OBJECT",
|
|
148
|
+
DUPLICATE_ATTRIBUTE: "SCHEMA_DUPLICATE_ATTRIBUTE",
|
|
149
|
+
// Concurrency
|
|
150
|
+
CONFLICT: "SCHEMA_CONFLICT",
|
|
151
|
+
// Storage
|
|
152
|
+
STORAGE_UPLOAD_FAILED: "SCHEMA_STORAGE_UPLOAD_FAILED",
|
|
153
|
+
STORAGE_DOWNLOAD_FAILED: "SCHEMA_STORAGE_DOWNLOAD_FAILED",
|
|
154
|
+
STORAGE_DELETE_FAILED: "SCHEMA_STORAGE_DELETE_FAILED",
|
|
155
|
+
STORAGE_URL_FAILED: "SCHEMA_STORAGE_URL_FAILED",
|
|
156
|
+
// Repository Operations
|
|
157
|
+
REPOSITORY_CREATE_FAILED: "SCHEMA_REPOSITORY_CREATE_FAILED",
|
|
158
|
+
REPOSITORY_UPDATE_FAILED: "SCHEMA_REPOSITORY_UPDATE_FAILED",
|
|
159
|
+
REPOSITORY_DELETE_FAILED: "SCHEMA_REPOSITORY_DELETE_FAILED",
|
|
160
|
+
REPOSITORY_QUERY_FAILED: "SCHEMA_REPOSITORY_QUERY_FAILED",
|
|
161
|
+
// Not Implemented
|
|
162
|
+
NOT_IMPLEMENTED: "SCHEMA_NOT_IMPLEMENTED",
|
|
163
|
+
// Timeout
|
|
164
|
+
TIMEOUT: "SCHEMA_TIMEOUT",
|
|
165
|
+
// Schema Integrity
|
|
166
|
+
RECORD_REFERENCED: "SCHEMA_RECORD_REFERENCED",
|
|
167
|
+
ATTRIBUTE_IN_USE: "SCHEMA_ATTRIBUTE_IN_USE",
|
|
168
|
+
OBJECT_REFERENCED: "SCHEMA_OBJECT_REFERENCED"
|
|
169
|
+
};
|
|
170
|
+
var SchemaError = class extends Error {
|
|
171
|
+
constructor(message, code = SchemaErrorCode.UNKNOWN, details) {
|
|
172
|
+
super(message);
|
|
173
|
+
this.name = "SchemaError";
|
|
174
|
+
this.code = code;
|
|
175
|
+
this.details = details;
|
|
176
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
177
|
+
}
|
|
178
|
+
toJSON() {
|
|
179
|
+
return {
|
|
180
|
+
name: this.name,
|
|
181
|
+
code: this.code,
|
|
182
|
+
message: this.message,
|
|
183
|
+
details: this.details
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
var NotFoundError = class extends SchemaError {
|
|
188
|
+
constructor(resourceType, resourceId, code = SchemaErrorCode.RECORD_NOT_FOUND) {
|
|
189
|
+
super(`${resourceType} with id "${resourceId}" not found`, code, {
|
|
190
|
+
resourceType,
|
|
191
|
+
resourceId
|
|
192
|
+
});
|
|
193
|
+
this.name = "NotFoundError";
|
|
194
|
+
this.resourceType = resourceType;
|
|
195
|
+
this.resourceId = resourceId;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
var ObjectNotFoundError = class extends NotFoundError {
|
|
199
|
+
constructor(objectId) {
|
|
200
|
+
super("Object", objectId, SchemaErrorCode.OBJECT_NOT_FOUND);
|
|
201
|
+
this.name = "ObjectNotFoundError";
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
var AttributeNotFoundError = class extends NotFoundError {
|
|
205
|
+
constructor(attributeId) {
|
|
206
|
+
super("Attribute", attributeId, SchemaErrorCode.ATTRIBUTE_NOT_FOUND);
|
|
207
|
+
this.name = "AttributeNotFoundError";
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
var RecordNotFoundError = class extends NotFoundError {
|
|
211
|
+
constructor(recordId) {
|
|
212
|
+
super("Record", recordId, SchemaErrorCode.RECORD_NOT_FOUND);
|
|
213
|
+
this.name = "RecordNotFoundError";
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
var ValidationError = class _ValidationError extends SchemaError {
|
|
217
|
+
constructor(message, errors) {
|
|
218
|
+
super(message, SchemaErrorCode.VALIDATION_FAILED, { errors });
|
|
219
|
+
this.name = "ValidationError";
|
|
220
|
+
this.errors = errors;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Create a validation error from Zod-style errors
|
|
224
|
+
*/
|
|
225
|
+
static fromZodErrors(errors) {
|
|
226
|
+
const details = errors.map((err) => ({
|
|
227
|
+
path: err.path.map(String),
|
|
228
|
+
message: err.message
|
|
229
|
+
}));
|
|
230
|
+
const message = `Validation failed: ${details.map((d) => `${d.path.join(".")}: ${d.message}`).join(", ")}`;
|
|
231
|
+
return new _ValidationError(message, details);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
var ProtectedResourceError = class extends SchemaError {
|
|
235
|
+
constructor(resourceType, resourceName, operation) {
|
|
236
|
+
const code = resourceType === "object" ? SchemaErrorCode.PROTECTED_OBJECT : resourceType === "view" ? SchemaErrorCode.PROTECTED_VIEW : SchemaErrorCode.PROTECTED_ATTRIBUTE;
|
|
237
|
+
super(`Cannot ${operation} system ${resourceType} "${resourceName}"`, code, {
|
|
238
|
+
resourceType,
|
|
239
|
+
resourceName,
|
|
240
|
+
operation
|
|
241
|
+
});
|
|
242
|
+
this.name = "ProtectedResourceError";
|
|
243
|
+
this.resourceType = resourceType;
|
|
244
|
+
this.resourceName = resourceName;
|
|
245
|
+
this.operation = operation;
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
var SyncError = class extends SchemaError {
|
|
249
|
+
constructor(objectName, message, cause) {
|
|
250
|
+
super(`Failed to sync object "${objectName}": ${message}`, SchemaErrorCode.SYNC_FAILED, {
|
|
251
|
+
objectName,
|
|
252
|
+
cause: cause?.message
|
|
253
|
+
});
|
|
254
|
+
this.name = "SyncError";
|
|
255
|
+
this.objectName = objectName;
|
|
256
|
+
this.cause = cause;
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
var SystemEntityImmutableError = class extends SchemaError {
|
|
260
|
+
constructor(entityType, entityName) {
|
|
261
|
+
super(
|
|
262
|
+
`Cannot mutate system ${entityType} "${entityName}". System entities are declared in code and immutable at runtime.`,
|
|
263
|
+
SchemaErrorCode.SYSTEM_ENTITY_IMMUTABLE,
|
|
264
|
+
{ entityType, entityName }
|
|
265
|
+
);
|
|
266
|
+
this.name = "SystemEntityImmutableError";
|
|
267
|
+
this.entityType = entityType;
|
|
268
|
+
this.entityName = entityName;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
var SyncConflictError = class extends SchemaError {
|
|
272
|
+
constructor(params) {
|
|
273
|
+
const target = params.attributeName ? `"${params.objectName}.${params.attributeName}"` : `"${params.objectName}"`;
|
|
274
|
+
super(
|
|
275
|
+
`Cannot promote runtime entity ${target}: ${params.reason}. Resolve via: (1) relax the code constraint, (2) migrate runtime values manually, or (3) rename the code declaration.`,
|
|
276
|
+
SchemaErrorCode.SYNC_CONFLICT,
|
|
277
|
+
{
|
|
278
|
+
objectName: params.objectName,
|
|
279
|
+
attributeName: params.attributeName,
|
|
280
|
+
reason: params.reason
|
|
281
|
+
}
|
|
282
|
+
);
|
|
283
|
+
this.name = "SyncConflictError";
|
|
284
|
+
this.objectName = params.objectName;
|
|
285
|
+
this.attributeName = params.attributeName;
|
|
286
|
+
this.reason = params.reason;
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
var SyncCascadeError = class extends SchemaError {
|
|
290
|
+
constructor(objectName, conflictingAttributes) {
|
|
291
|
+
super(
|
|
292
|
+
`Cannot demote object "${objectName}": attributes ${conflictingAttributes.join(", ")} are still declared in code. Remove them from code first, or restore the object.`,
|
|
293
|
+
SchemaErrorCode.SYNC_CASCADE,
|
|
294
|
+
{ objectName, conflictingAttributes }
|
|
295
|
+
);
|
|
296
|
+
this.name = "SyncCascadeError";
|
|
297
|
+
this.objectName = objectName;
|
|
298
|
+
this.conflictingAttributes = conflictingAttributes;
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
var OrphanSystemAttributeError = class extends SchemaError {
|
|
302
|
+
constructor(objectName, attributeName) {
|
|
303
|
+
super(
|
|
304
|
+
`Attribute "${objectName}.${attributeName}" is system:true but its object parent is system:false. This is an invariant violation.`,
|
|
305
|
+
SchemaErrorCode.ORPHAN_SYSTEM_ATTRIBUTE,
|
|
306
|
+
{ objectName, attributeName }
|
|
307
|
+
);
|
|
308
|
+
this.name = "OrphanSystemAttributeError";
|
|
309
|
+
this.objectName = objectName;
|
|
310
|
+
this.attributeName = attributeName;
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
var DestructiveSyncNotAllowedError = class extends SchemaError {
|
|
314
|
+
constructor(operations) {
|
|
315
|
+
const summary = operations.map((op) => {
|
|
316
|
+
if (op.type === "remove_attribute") {
|
|
317
|
+
return `- remove_attribute "${op.objectName ?? "?"}.${op.name}"`;
|
|
318
|
+
}
|
|
319
|
+
if (op.type === "remove_object") {
|
|
320
|
+
return "- remove_object";
|
|
321
|
+
}
|
|
322
|
+
return `- ${op.type}`;
|
|
323
|
+
}).join("\n");
|
|
324
|
+
super(
|
|
325
|
+
`Destructive schema operations detected (data will be lost):
|
|
326
|
+
${summary}
|
|
327
|
+
|
|
328
|
+
To allow Standards to apply these automatically, set the environment variable:
|
|
329
|
+
STANDARDS_ALLOW_DESTRUCTIVE_SYNC=1
|
|
330
|
+
|
|
331
|
+
Otherwise, revert your code changes or back up the data first.`,
|
|
332
|
+
SchemaErrorCode.DESTRUCTIVE_NOT_ALLOWED,
|
|
333
|
+
{ operations }
|
|
334
|
+
);
|
|
335
|
+
this.name = "DestructiveSyncNotAllowedError";
|
|
336
|
+
this.operations = operations;
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
var MigrationTimeoutError = class extends SchemaError {
|
|
340
|
+
constructor(objectName, migrated, total, maxDurationMs) {
|
|
341
|
+
super(
|
|
342
|
+
`Migration for "${objectName}" exceeded the maximum duration (${maxDurationMs}ms). Migrated ${migrated}/${total} records before timeout. Increase STANDARDS_MIGRATION_MAX_DURATION_MS or split the migration into smaller batches.`,
|
|
343
|
+
SchemaErrorCode.MIGRATION_TIMEOUT,
|
|
344
|
+
{ objectName, migrated, total, maxDurationMs }
|
|
345
|
+
);
|
|
346
|
+
this.name = "MigrationTimeoutError";
|
|
347
|
+
this.objectName = objectName;
|
|
348
|
+
this.migrated = migrated;
|
|
349
|
+
this.total = total;
|
|
350
|
+
this.maxDurationMs = maxDurationMs;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
var ChangeTypeNotSupportedError = class extends SchemaError {
|
|
354
|
+
constructor(objectName, attributeName) {
|
|
355
|
+
super(
|
|
356
|
+
`Cannot change type of attribute "${objectName}.${attributeName}" via runtime API. Declare a migration in your schema code:
|
|
357
|
+
object("${objectName}").migration(N, (m) =>
|
|
358
|
+
m.changeType("${attributeName}", from, to, { transform: (v) => ... })
|
|
359
|
+
)`,
|
|
360
|
+
SchemaErrorCode.CHANGE_TYPE_NOT_SUPPORTED,
|
|
361
|
+
{ objectName, attributeName }
|
|
362
|
+
);
|
|
363
|
+
this.name = "ChangeTypeNotSupportedError";
|
|
364
|
+
this.objectName = objectName;
|
|
365
|
+
this.attributeName = attributeName;
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
var DuplicateError = class extends SchemaError {
|
|
369
|
+
constructor(resourceType, resourceName) {
|
|
370
|
+
const code = resourceType === "object" ? SchemaErrorCode.DUPLICATE_OBJECT : SchemaErrorCode.DUPLICATE_ATTRIBUTE;
|
|
371
|
+
super(
|
|
372
|
+
`${resourceType === "object" ? "Object" : "Attribute"} "${resourceName}" already exists`,
|
|
373
|
+
code,
|
|
374
|
+
{ resourceType, resourceName }
|
|
375
|
+
);
|
|
376
|
+
this.name = "DuplicateError";
|
|
377
|
+
this.resourceType = resourceType;
|
|
378
|
+
this.resourceName = resourceName;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
function isSchemaError(error) {
|
|
382
|
+
return error instanceof SchemaError;
|
|
383
|
+
}
|
|
384
|
+
function isNotFoundError(error) {
|
|
385
|
+
return error instanceof NotFoundError;
|
|
386
|
+
}
|
|
387
|
+
function isValidationError(error) {
|
|
388
|
+
return error instanceof ValidationError;
|
|
389
|
+
}
|
|
390
|
+
function isProtectedResourceError(error) {
|
|
391
|
+
return error instanceof ProtectedResourceError;
|
|
392
|
+
}
|
|
393
|
+
var ForbiddenError = class extends SchemaError {
|
|
394
|
+
constructor(objectName, action, userId) {
|
|
395
|
+
super(`No ${action} permission on object "${objectName}"`, SchemaErrorCode.FORBIDDEN, {
|
|
396
|
+
objectName,
|
|
397
|
+
action,
|
|
398
|
+
userId
|
|
399
|
+
});
|
|
400
|
+
this.name = "ForbiddenError";
|
|
401
|
+
this.objectName = objectName;
|
|
402
|
+
this.action = action;
|
|
403
|
+
this.userId = userId;
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
var ProtectedRoleError = class extends SchemaError {
|
|
407
|
+
constructor(roleName, operation) {
|
|
408
|
+
super(`Cannot ${operation} system role "${roleName}"`, SchemaErrorCode.PROTECTED_ROLE, {
|
|
409
|
+
roleName,
|
|
410
|
+
operation
|
|
411
|
+
});
|
|
412
|
+
this.name = "ProtectedRoleError";
|
|
413
|
+
this.roleName = roleName;
|
|
414
|
+
this.operation = operation;
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
var RoleNotFoundError = class extends NotFoundError {
|
|
418
|
+
constructor(roleId) {
|
|
419
|
+
super("Role", roleId, SchemaErrorCode.ROLE_NOT_FOUND);
|
|
420
|
+
this.name = "RoleNotFoundError";
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
var ConcurrentModificationError = class extends SchemaError {
|
|
424
|
+
constructor(recordId) {
|
|
425
|
+
super(
|
|
426
|
+
`Record ${recordId} was modified by another request. Please refresh and try again.`,
|
|
427
|
+
SchemaErrorCode.CONFLICT,
|
|
428
|
+
{ recordId }
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
var StorageError = class extends SchemaError {
|
|
433
|
+
constructor(operation, message, path, cause) {
|
|
434
|
+
const code = operation === "upload" ? SchemaErrorCode.STORAGE_UPLOAD_FAILED : operation === "download" ? SchemaErrorCode.STORAGE_DOWNLOAD_FAILED : operation === "delete" ? SchemaErrorCode.STORAGE_DELETE_FAILED : SchemaErrorCode.STORAGE_URL_FAILED;
|
|
435
|
+
super(`Storage ${operation} failed: ${message}`, code, {
|
|
436
|
+
operation,
|
|
437
|
+
path,
|
|
438
|
+
cause
|
|
439
|
+
});
|
|
440
|
+
this.name = "StorageError";
|
|
441
|
+
this.operation = operation;
|
|
442
|
+
this.path = path;
|
|
443
|
+
this.cause = cause;
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
var AccessDeniedError = class extends SchemaError {
|
|
447
|
+
constructor(resource, reason) {
|
|
448
|
+
super(reason ?? `Access denied to resource: ${resource}`, SchemaErrorCode.ACCESS_DENIED, {
|
|
449
|
+
resource,
|
|
450
|
+
reason
|
|
451
|
+
});
|
|
452
|
+
this.name = "AccessDeniedError";
|
|
453
|
+
this.resource = resource;
|
|
454
|
+
this.reason = reason;
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
var RepositoryError = class extends SchemaError {
|
|
458
|
+
constructor(operation, entity, message, cause) {
|
|
459
|
+
const code = operation === "create" ? SchemaErrorCode.REPOSITORY_CREATE_FAILED : operation === "update" ? SchemaErrorCode.REPOSITORY_UPDATE_FAILED : operation === "delete" ? SchemaErrorCode.REPOSITORY_DELETE_FAILED : SchemaErrorCode.REPOSITORY_QUERY_FAILED;
|
|
460
|
+
super(`Failed to ${operation} ${entity}: ${message}`, code, {
|
|
461
|
+
operation,
|
|
462
|
+
entity,
|
|
463
|
+
cause
|
|
464
|
+
});
|
|
465
|
+
this.name = "RepositoryError";
|
|
466
|
+
this.operation = operation;
|
|
467
|
+
this.entity = entity;
|
|
468
|
+
this.cause = cause;
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
var NotImplementedError = class extends SchemaError {
|
|
472
|
+
constructor(feature, milestone) {
|
|
473
|
+
const message = milestone ? `${feature}: not implemented (${milestone})` : `${feature}: not implemented`;
|
|
474
|
+
super(message, SchemaErrorCode.NOT_IMPLEMENTED, {
|
|
475
|
+
feature,
|
|
476
|
+
milestone
|
|
477
|
+
});
|
|
478
|
+
this.name = "NotImplementedError";
|
|
479
|
+
this.feature = feature;
|
|
480
|
+
this.milestone = milestone;
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
var MemoryNotFoundError = class extends NotFoundError {
|
|
484
|
+
constructor(memoryId) {
|
|
485
|
+
super("Memory", memoryId, SchemaErrorCode.MEMORY_NOT_FOUND);
|
|
486
|
+
this.name = "MemoryNotFoundError";
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
var RecordReferencedError = class extends SchemaError {
|
|
490
|
+
constructor(recordId, references) {
|
|
491
|
+
const total = references.reduce((sum, r) => sum + r.count, 0);
|
|
492
|
+
super(
|
|
493
|
+
`Cannot delete record: referenced by ${total} record${total > 1 ? "s" : ""}`,
|
|
494
|
+
SchemaErrorCode.RECORD_REFERENCED,
|
|
495
|
+
{ recordId, references }
|
|
496
|
+
);
|
|
497
|
+
this.name = "RecordReferencedError";
|
|
498
|
+
this.recordId = recordId;
|
|
499
|
+
this.references = references;
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
var AttributeInUseError = class extends SchemaError {
|
|
503
|
+
constructor(attributeName, usage) {
|
|
504
|
+
super(
|
|
505
|
+
`Cannot delete attribute "${attributeName}": used in ${usage}`,
|
|
506
|
+
SchemaErrorCode.ATTRIBUTE_IN_USE,
|
|
507
|
+
{ attributeName, usage }
|
|
508
|
+
);
|
|
509
|
+
this.name = "AttributeInUseError";
|
|
510
|
+
this.attributeName = attributeName;
|
|
511
|
+
this.usage = usage;
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
var ObjectReferencedError = class extends SchemaError {
|
|
515
|
+
constructor(objectName, referencingObjects) {
|
|
516
|
+
super(
|
|
517
|
+
`Cannot delete object "${objectName}": target of relations in ${referencingObjects.join(", ")}`,
|
|
518
|
+
SchemaErrorCode.OBJECT_REFERENCED,
|
|
519
|
+
{ objectName, referencingObjects }
|
|
520
|
+
);
|
|
521
|
+
this.name = "ObjectReferencedError";
|
|
522
|
+
this.objectName = objectName;
|
|
523
|
+
this.referencingObjects = referencingObjects;
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
function isRecordReferencedError(error) {
|
|
527
|
+
return error instanceof RecordReferencedError;
|
|
528
|
+
}
|
|
529
|
+
function isAttributeInUseError(error) {
|
|
530
|
+
return error instanceof AttributeInUseError;
|
|
531
|
+
}
|
|
532
|
+
function isObjectReferencedError(error) {
|
|
533
|
+
return error instanceof ObjectReferencedError;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// src/validation/object/helpers.ts
|
|
537
|
+
function validateAttribute(attr, value) {
|
|
538
|
+
const validator = createAttributeValidator(attr);
|
|
539
|
+
if (!attr.required && (value === void 0 || value === null)) {
|
|
540
|
+
return { success: true, data: { [attr.name]: value } };
|
|
541
|
+
}
|
|
542
|
+
const result = validator.safeParse(value);
|
|
543
|
+
if (result.success) {
|
|
544
|
+
return {
|
|
545
|
+
success: true,
|
|
546
|
+
data: { [attr.name]: result.data }
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
return {
|
|
550
|
+
success: false,
|
|
551
|
+
errors: chunkT225DB55_js.formatZodErrors(result.error).map((err) => ({
|
|
552
|
+
path: [attr.name, ...err.path],
|
|
553
|
+
message: err.message
|
|
554
|
+
}))
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
function validateObject(objectDef, data) {
|
|
558
|
+
const validator = createObjectValidator(objectDef);
|
|
559
|
+
const result = validator.safeParse(data);
|
|
560
|
+
if (result.success) {
|
|
561
|
+
return {
|
|
562
|
+
success: true,
|
|
563
|
+
data: result.data
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
return {
|
|
567
|
+
success: false,
|
|
568
|
+
errors: chunkT225DB55_js.formatZodErrors(result.error)
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
function validateObjectOrThrow(objectDef, data) {
|
|
572
|
+
const result = validateObject(objectDef, data);
|
|
573
|
+
if (!result.success) {
|
|
574
|
+
const errors = result.errors ?? [];
|
|
575
|
+
const detail = errors.map((err) => `${err.path.join(".")}: ${err.message}`).join("\n") || "Unknown validation error";
|
|
576
|
+
throw new ValidationError(
|
|
577
|
+
`Validation failed for ${objectDef.label}:
|
|
578
|
+
${detail}`,
|
|
579
|
+
errors.map((err) => ({ path: err.path, message: err.message }))
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
return result.data;
|
|
583
|
+
}
|
|
584
|
+
function validateDraft(objectDef, data) {
|
|
585
|
+
const validator = createDraftValidator(objectDef);
|
|
586
|
+
const result = validator.safeParse(data);
|
|
587
|
+
if (result.success) {
|
|
588
|
+
return {
|
|
589
|
+
success: true,
|
|
590
|
+
data: result.data
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
return {
|
|
594
|
+
success: false,
|
|
595
|
+
errors: chunkT225DB55_js.formatZodErrors(result.error)
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
function validateDraftOrThrow(objectDef, data) {
|
|
599
|
+
const result = validateDraft(objectDef, data);
|
|
600
|
+
if (!result.success) {
|
|
601
|
+
const errors = result.errors ?? [];
|
|
602
|
+
const detail = errors.map((err) => `${err.path.join(".")}: ${err.message}`).join("\n") || "Unknown validation error";
|
|
603
|
+
throw new ValidationError(
|
|
604
|
+
`Draft validation failed for ${objectDef.label}:
|
|
605
|
+
${detail}`,
|
|
606
|
+
errors.map((err) => ({ path: err.path, message: err.message }))
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
return result.data;
|
|
610
|
+
}
|
|
611
|
+
function isValuePresent(value) {
|
|
612
|
+
if (value === void 0 || value === null) {
|
|
613
|
+
return false;
|
|
614
|
+
}
|
|
615
|
+
if (typeof value === "string" && value.trim() === "") {
|
|
616
|
+
return false;
|
|
617
|
+
}
|
|
618
|
+
return true;
|
|
619
|
+
}
|
|
620
|
+
function getMissingRequiredAttributes(objectDef, data) {
|
|
621
|
+
const missing = [];
|
|
622
|
+
for (const attr of objectDef.attributes) {
|
|
623
|
+
if (attr.required && !isValuePresent(data[attr.name])) {
|
|
624
|
+
missing.push(attr);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
return missing;
|
|
628
|
+
}
|
|
629
|
+
function isRecordComplete(objectDef, data) {
|
|
630
|
+
const missing = getMissingRequiredAttributes(objectDef, data);
|
|
631
|
+
if (missing.length > 0) {
|
|
632
|
+
return false;
|
|
633
|
+
}
|
|
634
|
+
const validation = validateObject(objectDef, data);
|
|
635
|
+
return validation.success;
|
|
636
|
+
}
|
|
637
|
+
function computeRecordStatus(objectDef, data) {
|
|
638
|
+
return isRecordComplete(objectDef, data) ? "complete" : "draft";
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
exports.AccessDeniedError = AccessDeniedError;
|
|
642
|
+
exports.AttributeInUseError = AttributeInUseError;
|
|
643
|
+
exports.AttributeNotFoundError = AttributeNotFoundError;
|
|
644
|
+
exports.ChangeTypeNotSupportedError = ChangeTypeNotSupportedError;
|
|
645
|
+
exports.ConcurrentModificationError = ConcurrentModificationError;
|
|
646
|
+
exports.DestructiveSyncNotAllowedError = DestructiveSyncNotAllowedError;
|
|
647
|
+
exports.DuplicateError = DuplicateError;
|
|
648
|
+
exports.ForbiddenError = ForbiddenError;
|
|
649
|
+
exports.MemoryNotFoundError = MemoryNotFoundError;
|
|
650
|
+
exports.MigrationTimeoutError = MigrationTimeoutError;
|
|
651
|
+
exports.NotFoundError = NotFoundError;
|
|
652
|
+
exports.NotImplementedError = NotImplementedError;
|
|
653
|
+
exports.ObjectNotFoundError = ObjectNotFoundError;
|
|
654
|
+
exports.ObjectReferencedError = ObjectReferencedError;
|
|
655
|
+
exports.OrphanSystemAttributeError = OrphanSystemAttributeError;
|
|
656
|
+
exports.ProtectedResourceError = ProtectedResourceError;
|
|
657
|
+
exports.ProtectedRoleError = ProtectedRoleError;
|
|
658
|
+
exports.RecordNotFoundError = RecordNotFoundError;
|
|
659
|
+
exports.RecordReferencedError = RecordReferencedError;
|
|
660
|
+
exports.RepositoryError = RepositoryError;
|
|
661
|
+
exports.RoleNotFoundError = RoleNotFoundError;
|
|
662
|
+
exports.SchemaError = SchemaError;
|
|
663
|
+
exports.SchemaErrorCode = SchemaErrorCode;
|
|
664
|
+
exports.StorageError = StorageError;
|
|
665
|
+
exports.SyncCascadeError = SyncCascadeError;
|
|
666
|
+
exports.SyncConflictError = SyncConflictError;
|
|
667
|
+
exports.SyncError = SyncError;
|
|
668
|
+
exports.SystemEntityImmutableError = SystemEntityImmutableError;
|
|
669
|
+
exports.ValidationError = ValidationError;
|
|
670
|
+
exports.computeRecordStatus = computeRecordStatus;
|
|
671
|
+
exports.createAttributeValidator = createAttributeValidator;
|
|
672
|
+
exports.createDraftValidator = createDraftValidator;
|
|
673
|
+
exports.createFormAttributeValidator = createFormAttributeValidator;
|
|
674
|
+
exports.createObjectValidator = createObjectValidator;
|
|
675
|
+
exports.getMissingRequiredAttributes = getMissingRequiredAttributes;
|
|
676
|
+
exports.isAttributeInUseError = isAttributeInUseError;
|
|
677
|
+
exports.isNotFoundError = isNotFoundError;
|
|
678
|
+
exports.isObjectReferencedError = isObjectReferencedError;
|
|
679
|
+
exports.isProtectedResourceError = isProtectedResourceError;
|
|
680
|
+
exports.isRecordComplete = isRecordComplete;
|
|
681
|
+
exports.isRecordReferencedError = isRecordReferencedError;
|
|
682
|
+
exports.isSchemaError = isSchemaError;
|
|
683
|
+
exports.isValidationError = isValidationError;
|
|
684
|
+
exports.validateAttribute = validateAttribute;
|
|
685
|
+
exports.validateDraft = validateDraft;
|
|
686
|
+
exports.validateDraftOrThrow = validateDraftOrThrow;
|
|
687
|
+
exports.validateObject = validateObject;
|
|
688
|
+
exports.validateObjectOrThrow = validateObjectOrThrow;
|