@nexload-sdk/payload-schema 1.1.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/CHANGELOG.md +18 -0
- package/README.md +78 -0
- package/dist/default-id.d.ts +4 -0
- package/dist/default-id.d.ts.map +1 -0
- package/dist/entity/define-entity.d.ts +4 -0
- package/dist/entity/define-entity.d.ts.map +1 -0
- package/dist/errors.d.ts +139 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/fields/index.d.ts +45 -0
- package/dist/fields/index.d.ts.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1336 -0
- package/dist/index.js.map +7 -0
- package/dist/internal/clone.d.ts +2 -0
- package/dist/internal/clone.d.ts.map +1 -0
- package/dist/internal/field-definition.d.ts +35 -0
- package/dist/internal/field-definition.d.ts.map +1 -0
- package/dist/types.d.ts +198 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +71 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1336 @@
|
|
|
1
|
+
// @nexload-sdk/payload-schema v1.0.0 (build 2026-07-20T16:56:56.720Z)
|
|
2
|
+
|
|
3
|
+
// src/default-id.ts
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
var defaultIdSchema = z.union([
|
|
6
|
+
z.string().min(1),
|
|
7
|
+
z.number().int().safe()
|
|
8
|
+
]);
|
|
9
|
+
|
|
10
|
+
// src/entity/define-entity.ts
|
|
11
|
+
import { ValidationError } from "payload";
|
|
12
|
+
import { z as z3 } from "zod";
|
|
13
|
+
|
|
14
|
+
// src/errors.ts
|
|
15
|
+
var PayloadSchemaError = class extends Error {
|
|
16
|
+
code;
|
|
17
|
+
phase;
|
|
18
|
+
data;
|
|
19
|
+
cause;
|
|
20
|
+
constructor(code, options) {
|
|
21
|
+
super(
|
|
22
|
+
options.message,
|
|
23
|
+
options.cause === void 0 ? void 0 : { cause: options.cause }
|
|
24
|
+
);
|
|
25
|
+
this.name = "PayloadSchemaError";
|
|
26
|
+
this.code = code;
|
|
27
|
+
this.phase = options.phase;
|
|
28
|
+
this.data = options.data;
|
|
29
|
+
this.cause = options.cause;
|
|
30
|
+
}
|
|
31
|
+
toJSON() {
|
|
32
|
+
return {
|
|
33
|
+
name: "PayloadSchemaError",
|
|
34
|
+
code: this.code,
|
|
35
|
+
phase: this.phase,
|
|
36
|
+
message: this.message,
|
|
37
|
+
data: this.data
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
function isPayloadSchemaError(error, code) {
|
|
42
|
+
return error instanceof PayloadSchemaError && (code === void 0 || error.code === code);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/fields/index.ts
|
|
46
|
+
import { z as z2 } from "zod";
|
|
47
|
+
|
|
48
|
+
// src/internal/field-definition.ts
|
|
49
|
+
var fieldSeedSymbol = /* @__PURE__ */ Symbol("payload-schema.field-seed");
|
|
50
|
+
function createFieldDefinition(seed) {
|
|
51
|
+
const definition = { kind: seed.kind, schema: seed.schema };
|
|
52
|
+
Object.defineProperty(
|
|
53
|
+
definition,
|
|
54
|
+
fieldSeedSymbol,
|
|
55
|
+
{ value: seed, enumerable: false }
|
|
56
|
+
);
|
|
57
|
+
return Object.freeze(definition);
|
|
58
|
+
}
|
|
59
|
+
function getFieldSeed(field2) {
|
|
60
|
+
const seed = field2[fieldSeedSymbol];
|
|
61
|
+
if (!seed) throw new TypeError("Value is not a payload-schema field definition.");
|
|
62
|
+
return seed;
|
|
63
|
+
}
|
|
64
|
+
function fieldEntries(fields) {
|
|
65
|
+
return Object.entries(fields).map(([
|
|
66
|
+
name,
|
|
67
|
+
definition
|
|
68
|
+
]) => [
|
|
69
|
+
name,
|
|
70
|
+
getFieldSeed(definition)
|
|
71
|
+
]);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/fields/index.ts
|
|
75
|
+
var unboundPath = "<unbound>";
|
|
76
|
+
function asPayloadExtras(value) {
|
|
77
|
+
return value ?? {};
|
|
78
|
+
}
|
|
79
|
+
function withDefaults(seed, options) {
|
|
80
|
+
return {
|
|
81
|
+
...seed,
|
|
82
|
+
staticDefaultConfigured: Object.hasOwn(
|
|
83
|
+
options,
|
|
84
|
+
"defaultValue"
|
|
85
|
+
),
|
|
86
|
+
staticDefault: options.defaultValue,
|
|
87
|
+
dynamicDefaultConfigured: Object.hasOwn(
|
|
88
|
+
options,
|
|
89
|
+
"dynamicDefaultValue"
|
|
90
|
+
),
|
|
91
|
+
dynamicDefault: options.dynamicDefaultValue
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function validateRange(fieldKind, constraint, minimum, maximum) {
|
|
95
|
+
if (minimum !== void 0 && maximum !== void 0 && minimum > maximum) {
|
|
96
|
+
throw new PayloadSchemaError(
|
|
97
|
+
"INVALID_CONSTRAINT_RANGE",
|
|
98
|
+
{
|
|
99
|
+
phase: "definition",
|
|
100
|
+
message: `${fieldKind} minimum cannot be greater than maximum.`,
|
|
101
|
+
data: { fieldPath: unboundPath, constraint, minimum, maximum }
|
|
102
|
+
}
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function applyNullable(schema, nullable) {
|
|
107
|
+
return nullable ? schema.nullable() : schema;
|
|
108
|
+
}
|
|
109
|
+
function buildTextSchema(options, slug2) {
|
|
110
|
+
if (options.lowercase && options.uppercase) {
|
|
111
|
+
throw new PayloadSchemaError(
|
|
112
|
+
"INVALID_FIELD_CONFIGURATION",
|
|
113
|
+
{
|
|
114
|
+
phase: "definition",
|
|
115
|
+
message: "lowercase and uppercase are mutually exclusive.",
|
|
116
|
+
data: { fieldPath: unboundPath, fieldKind: slug2 ? "slug" : "text", reason: "conflicting case normalization" }
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
validateRange(
|
|
121
|
+
slug2 ? "slug" : "text",
|
|
122
|
+
"length",
|
|
123
|
+
options.minLength,
|
|
124
|
+
options.maxLength
|
|
125
|
+
);
|
|
126
|
+
let schema = z2.string();
|
|
127
|
+
if (slug2) {
|
|
128
|
+
schema = schema.transform((value) => value.normalize("NFKC").trim().toLowerCase().replace(
|
|
129
|
+
/[\s_]+/gu,
|
|
130
|
+
"-"
|
|
131
|
+
).replace(
|
|
132
|
+
/-+/gu,
|
|
133
|
+
"-"
|
|
134
|
+
).replace(
|
|
135
|
+
/^-|-$/gu,
|
|
136
|
+
""
|
|
137
|
+
));
|
|
138
|
+
} else {
|
|
139
|
+
if (options.trim) schema = schema.transform((value) => value.trim());
|
|
140
|
+
if (options.lowercase) schema = schema.transform((value) => value.toLowerCase());
|
|
141
|
+
if (options.uppercase) schema = schema.transform((value) => value.toUpperCase());
|
|
142
|
+
}
|
|
143
|
+
let constraints = z2.string();
|
|
144
|
+
if (options.minLength !== void 0) constraints = constraints.min(options.minLength);
|
|
145
|
+
if (options.maxLength !== void 0) constraints = constraints.max(options.maxLength);
|
|
146
|
+
if (options.pattern) constraints = constraints.regex(options.pattern);
|
|
147
|
+
schema = schema.pipe(constraints);
|
|
148
|
+
if (options.schema) schema = options.schema(schema);
|
|
149
|
+
return applyNullable(
|
|
150
|
+
schema,
|
|
151
|
+
options.nullable
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
function text(options = {}) {
|
|
155
|
+
const schema = buildTextSchema(
|
|
156
|
+
options,
|
|
157
|
+
false
|
|
158
|
+
);
|
|
159
|
+
return createFieldDefinition(withDefaults(
|
|
160
|
+
{
|
|
161
|
+
kind: "text",
|
|
162
|
+
payloadType: "text",
|
|
163
|
+
schema,
|
|
164
|
+
runtimeSchema: schema,
|
|
165
|
+
payloadCore: {
|
|
166
|
+
...options.required === void 0 ? {} : { required: options.required },
|
|
167
|
+
...options.minLength === void 0 ? {} : { minLength: options.minLength },
|
|
168
|
+
...options.maxLength === void 0 ? {} : { maxLength: options.maxLength }
|
|
169
|
+
},
|
|
170
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
171
|
+
required: options.required ?? false,
|
|
172
|
+
nullable: options.nullable ?? false
|
|
173
|
+
},
|
|
174
|
+
options
|
|
175
|
+
));
|
|
176
|
+
}
|
|
177
|
+
function textarea(options = {}) {
|
|
178
|
+
const schema = buildTextSchema(
|
|
179
|
+
options,
|
|
180
|
+
false
|
|
181
|
+
);
|
|
182
|
+
return createFieldDefinition(withDefaults(
|
|
183
|
+
{
|
|
184
|
+
kind: "textarea",
|
|
185
|
+
payloadType: "textarea",
|
|
186
|
+
schema,
|
|
187
|
+
runtimeSchema: schema,
|
|
188
|
+
payloadCore: {
|
|
189
|
+
...options.required === void 0 ? {} : { required: options.required },
|
|
190
|
+
...options.minLength === void 0 ? {} : { minLength: options.minLength },
|
|
191
|
+
...options.maxLength === void 0 ? {} : { maxLength: options.maxLength }
|
|
192
|
+
},
|
|
193
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
194
|
+
required: options.required ?? false,
|
|
195
|
+
nullable: options.nullable ?? false
|
|
196
|
+
},
|
|
197
|
+
options
|
|
198
|
+
));
|
|
199
|
+
}
|
|
200
|
+
function slug(options = {}) {
|
|
201
|
+
const schema = buildTextSchema(
|
|
202
|
+
options,
|
|
203
|
+
true
|
|
204
|
+
);
|
|
205
|
+
return createFieldDefinition(withDefaults(
|
|
206
|
+
{
|
|
207
|
+
kind: "slug",
|
|
208
|
+
payloadType: "text",
|
|
209
|
+
schema,
|
|
210
|
+
runtimeSchema: schema,
|
|
211
|
+
payloadCore: {
|
|
212
|
+
...options.required === void 0 ? {} : { required: options.required },
|
|
213
|
+
...options.minLength === void 0 ? {} : { minLength: options.minLength },
|
|
214
|
+
...options.maxLength === void 0 ? {} : { maxLength: options.maxLength }
|
|
215
|
+
},
|
|
216
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
217
|
+
required: options.required ?? false,
|
|
218
|
+
nullable: options.nullable ?? false
|
|
219
|
+
},
|
|
220
|
+
options
|
|
221
|
+
));
|
|
222
|
+
}
|
|
223
|
+
function buildNumberSchema(options, forceSafeInteger = false) {
|
|
224
|
+
validateRange(
|
|
225
|
+
forceSafeInteger ? "money" : "number",
|
|
226
|
+
"value",
|
|
227
|
+
options.minimum,
|
|
228
|
+
options.maximum
|
|
229
|
+
);
|
|
230
|
+
let schema = z2.number().refine(
|
|
231
|
+
Number.isFinite,
|
|
232
|
+
{ error: "Expected a finite number" }
|
|
233
|
+
);
|
|
234
|
+
if (forceSafeInteger || options.integer) schema = schema.int();
|
|
235
|
+
if (forceSafeInteger || options.safe) schema = schema.safe();
|
|
236
|
+
if (options.minimum !== void 0) schema = schema.min(options.minimum);
|
|
237
|
+
if (options.maximum !== void 0) schema = schema.max(options.maximum);
|
|
238
|
+
if (options.multipleOf !== void 0) schema = schema.multipleOf(options.multipleOf);
|
|
239
|
+
let output = schema;
|
|
240
|
+
if (options.schema) output = options.schema(output);
|
|
241
|
+
return applyNullable(
|
|
242
|
+
output,
|
|
243
|
+
options.nullable
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
function number(options = {}) {
|
|
247
|
+
const schema = buildNumberSchema(options);
|
|
248
|
+
return createFieldDefinition(withDefaults(
|
|
249
|
+
{
|
|
250
|
+
kind: "number",
|
|
251
|
+
payloadType: "number",
|
|
252
|
+
schema,
|
|
253
|
+
runtimeSchema: schema,
|
|
254
|
+
payloadCore: {
|
|
255
|
+
...options.required === void 0 ? {} : { required: options.required },
|
|
256
|
+
...options.minimum === void 0 ? {} : { min: options.minimum },
|
|
257
|
+
...options.maximum === void 0 ? {} : { max: options.maximum }
|
|
258
|
+
},
|
|
259
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
260
|
+
required: options.required ?? false,
|
|
261
|
+
nullable: options.nullable ?? false
|
|
262
|
+
},
|
|
263
|
+
options
|
|
264
|
+
));
|
|
265
|
+
}
|
|
266
|
+
function money(options) {
|
|
267
|
+
if (!options.currency.trim()) {
|
|
268
|
+
throw new PayloadSchemaError(
|
|
269
|
+
"INVALID_FIELD_CONFIGURATION",
|
|
270
|
+
{
|
|
271
|
+
phase: "definition",
|
|
272
|
+
message: "Money currency must not be empty.",
|
|
273
|
+
data: { fieldPath: unboundPath, fieldKind: "money", reason: "empty currency" }
|
|
274
|
+
}
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
const schema = buildNumberSchema(
|
|
278
|
+
options,
|
|
279
|
+
true
|
|
280
|
+
);
|
|
281
|
+
return createFieldDefinition(withDefaults(
|
|
282
|
+
{
|
|
283
|
+
kind: "money",
|
|
284
|
+
payloadType: "number",
|
|
285
|
+
schema,
|
|
286
|
+
runtimeSchema: schema,
|
|
287
|
+
payloadCore: {
|
|
288
|
+
...options.required === void 0 ? {} : { required: options.required },
|
|
289
|
+
...options.minimum === void 0 ? {} : { min: options.minimum },
|
|
290
|
+
...options.maximum === void 0 ? {} : { max: options.maximum }
|
|
291
|
+
},
|
|
292
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
293
|
+
required: options.required ?? false,
|
|
294
|
+
nullable: options.nullable ?? false,
|
|
295
|
+
currency: options.currency
|
|
296
|
+
},
|
|
297
|
+
options
|
|
298
|
+
));
|
|
299
|
+
}
|
|
300
|
+
function boolean(options = {}) {
|
|
301
|
+
let base = z2.boolean();
|
|
302
|
+
if (options.schema) base = options.schema(base);
|
|
303
|
+
const schema = applyNullable(
|
|
304
|
+
base,
|
|
305
|
+
options.nullable
|
|
306
|
+
);
|
|
307
|
+
return createFieldDefinition(withDefaults(
|
|
308
|
+
{
|
|
309
|
+
kind: "boolean",
|
|
310
|
+
payloadType: "checkbox",
|
|
311
|
+
schema,
|
|
312
|
+
runtimeSchema: schema,
|
|
313
|
+
payloadCore: options.required === void 0 ? {} : { required: options.required },
|
|
314
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
315
|
+
required: options.required ?? false,
|
|
316
|
+
nullable: options.nullable ?? false
|
|
317
|
+
},
|
|
318
|
+
options
|
|
319
|
+
));
|
|
320
|
+
}
|
|
321
|
+
function date(options = {}) {
|
|
322
|
+
const iso = z2.iso.datetime({ offset: true });
|
|
323
|
+
const parseBound = (value, name) => {
|
|
324
|
+
if (value === void 0) return void 0;
|
|
325
|
+
const parsed = iso.safeParse(value);
|
|
326
|
+
if (!parsed.success) {
|
|
327
|
+
throw new PayloadSchemaError(
|
|
328
|
+
"INVALID_FIELD_CONFIGURATION",
|
|
329
|
+
{
|
|
330
|
+
phase: "definition",
|
|
331
|
+
message: `Date ${name} must be an ISO datetime with an explicit timezone.`,
|
|
332
|
+
data: { fieldPath: unboundPath, fieldKind: "date", reason: `invalid ${name}` }
|
|
333
|
+
}
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
return Date.parse(parsed.data);
|
|
337
|
+
};
|
|
338
|
+
const minimum = parseBound(
|
|
339
|
+
options.minimum,
|
|
340
|
+
"minimum"
|
|
341
|
+
);
|
|
342
|
+
const maximum = parseBound(
|
|
343
|
+
options.maximum,
|
|
344
|
+
"maximum"
|
|
345
|
+
);
|
|
346
|
+
validateRange(
|
|
347
|
+
"date",
|
|
348
|
+
"instant",
|
|
349
|
+
minimum,
|
|
350
|
+
maximum
|
|
351
|
+
);
|
|
352
|
+
let base = iso.transform((value) => new Date(value).toISOString());
|
|
353
|
+
if (minimum !== void 0) base = base.refine(
|
|
354
|
+
(value) => Date.parse(value) >= minimum,
|
|
355
|
+
{ error: "Date is before minimum" }
|
|
356
|
+
);
|
|
357
|
+
if (maximum !== void 0) base = base.refine(
|
|
358
|
+
(value) => Date.parse(value) <= maximum,
|
|
359
|
+
{ error: "Date is after maximum" }
|
|
360
|
+
);
|
|
361
|
+
if (options.schema) base = options.schema(base);
|
|
362
|
+
const schema = applyNullable(
|
|
363
|
+
base,
|
|
364
|
+
options.nullable
|
|
365
|
+
);
|
|
366
|
+
return createFieldDefinition(withDefaults(
|
|
367
|
+
{
|
|
368
|
+
kind: "date",
|
|
369
|
+
payloadType: "date",
|
|
370
|
+
schema,
|
|
371
|
+
runtimeSchema: schema,
|
|
372
|
+
payloadCore: options.required === void 0 ? {} : { required: options.required },
|
|
373
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
374
|
+
required: options.required ?? false,
|
|
375
|
+
nullable: options.nullable ?? false
|
|
376
|
+
},
|
|
377
|
+
options
|
|
378
|
+
));
|
|
379
|
+
}
|
|
380
|
+
function select(options) {
|
|
381
|
+
const values = [...options.values];
|
|
382
|
+
if (!values.length) {
|
|
383
|
+
throw new PayloadSchemaError(
|
|
384
|
+
"EMPTY_SELECT_VALUES",
|
|
385
|
+
{
|
|
386
|
+
phase: "definition",
|
|
387
|
+
message: "Select values must not be empty.",
|
|
388
|
+
data: { fieldPath: unboundPath }
|
|
389
|
+
}
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
if (new Set(values).size !== values.length || values.some((value) => !value)) {
|
|
393
|
+
throw new PayloadSchemaError(
|
|
394
|
+
"INVALID_FIELD_CONFIGURATION",
|
|
395
|
+
{
|
|
396
|
+
phase: "definition",
|
|
397
|
+
message: "Select values must be unique non-empty strings.",
|
|
398
|
+
data: { fieldPath: unboundPath, fieldKind: "select", reason: "invalid select values" }
|
|
399
|
+
}
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
const enumSchema = z2.enum(values);
|
|
403
|
+
const valueSchema = options.hasMany ? z2.array(enumSchema) : enumSchema;
|
|
404
|
+
const schema = applyNullable(
|
|
405
|
+
valueSchema,
|
|
406
|
+
options.nullable
|
|
407
|
+
);
|
|
408
|
+
const payloadOptions = values.map((value) => options.labels?.[value] === void 0 ? value : { label: options.labels[value], value });
|
|
409
|
+
return createFieldDefinition(withDefaults(
|
|
410
|
+
{
|
|
411
|
+
kind: "select",
|
|
412
|
+
payloadType: "select",
|
|
413
|
+
schema,
|
|
414
|
+
runtimeSchema: schema,
|
|
415
|
+
payloadCore: {
|
|
416
|
+
options: payloadOptions,
|
|
417
|
+
...options.hasMany === void 0 ? {} : { hasMany: options.hasMany },
|
|
418
|
+
...options.required === void 0 ? {} : { required: options.required }
|
|
419
|
+
},
|
|
420
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
421
|
+
required: options.required ?? false,
|
|
422
|
+
nullable: options.nullable ?? false,
|
|
423
|
+
hasMany: options.hasMany ?? false
|
|
424
|
+
},
|
|
425
|
+
options
|
|
426
|
+
));
|
|
427
|
+
}
|
|
428
|
+
function createRelationshipSchema(relationTo, hasMany, idSchema) {
|
|
429
|
+
const base = Array.isArray(relationTo) ? z2.discriminatedUnion(
|
|
430
|
+
"relationTo",
|
|
431
|
+
relationTo.map((slug2) => z2.strictObject({
|
|
432
|
+
relationTo: z2.literal(slug2),
|
|
433
|
+
value: idSchema
|
|
434
|
+
}))
|
|
435
|
+
) : idSchema;
|
|
436
|
+
return hasMany ? z2.array(base) : base;
|
|
437
|
+
}
|
|
438
|
+
function validateRelationTo(relationTo) {
|
|
439
|
+
if (Array.isArray(relationTo) && (!relationTo.length || relationTo.some((slug2) => !slug2)) || !Array.isArray(relationTo) && !relationTo) {
|
|
440
|
+
throw new PayloadSchemaError(
|
|
441
|
+
"INVALID_RELATIONSHIP_CONFIGURATION",
|
|
442
|
+
{
|
|
443
|
+
phase: "definition",
|
|
444
|
+
message: "relationTo must contain at least one collection slug.",
|
|
445
|
+
data: {
|
|
446
|
+
fieldPath: unboundPath,
|
|
447
|
+
reason: "empty relationTo",
|
|
448
|
+
relationTo: Array.isArray(relationTo) ? relationTo : []
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
function relationship(options) {
|
|
455
|
+
const relationTo = Array.isArray(options.relationTo) ? [...options.relationTo] : options.relationTo;
|
|
456
|
+
validateRelationTo(relationTo);
|
|
457
|
+
const idSchema = options.idSchema ?? defaultIdSchema;
|
|
458
|
+
const base = createRelationshipSchema(
|
|
459
|
+
relationTo,
|
|
460
|
+
options.hasMany ?? false,
|
|
461
|
+
idSchema
|
|
462
|
+
);
|
|
463
|
+
const schema = applyNullable(
|
|
464
|
+
base,
|
|
465
|
+
options.nullable
|
|
466
|
+
);
|
|
467
|
+
return createFieldDefinition(withDefaults(
|
|
468
|
+
{
|
|
469
|
+
kind: "relationship",
|
|
470
|
+
payloadType: "relationship",
|
|
471
|
+
schema,
|
|
472
|
+
runtimeSchema: schema,
|
|
473
|
+
payloadCore: {
|
|
474
|
+
relationTo,
|
|
475
|
+
...options.hasMany === void 0 ? {} : { hasMany: options.hasMany },
|
|
476
|
+
...options.required === void 0 ? {} : { required: options.required }
|
|
477
|
+
},
|
|
478
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
479
|
+
required: options.required ?? false,
|
|
480
|
+
nullable: options.nullable ?? false,
|
|
481
|
+
relationTo: Array.isArray(relationTo) ? relationTo : [relationTo],
|
|
482
|
+
hasMany: options.hasMany ?? false,
|
|
483
|
+
relation: { relationTo, hasMany: options.hasMany ?? false, idSchema: options.idSchema }
|
|
484
|
+
},
|
|
485
|
+
options
|
|
486
|
+
));
|
|
487
|
+
}
|
|
488
|
+
function upload(options) {
|
|
489
|
+
const relationTo = Array.isArray(options.relationTo) ? [...options.relationTo] : options.relationTo;
|
|
490
|
+
validateRelationTo(relationTo);
|
|
491
|
+
const idSchema = options.idSchema ?? defaultIdSchema;
|
|
492
|
+
const schema = applyNullable(
|
|
493
|
+
createRelationshipSchema(
|
|
494
|
+
relationTo,
|
|
495
|
+
options.hasMany ?? false,
|
|
496
|
+
idSchema
|
|
497
|
+
),
|
|
498
|
+
options.nullable
|
|
499
|
+
);
|
|
500
|
+
return createFieldDefinition(withDefaults(
|
|
501
|
+
{
|
|
502
|
+
kind: "upload",
|
|
503
|
+
payloadType: "upload",
|
|
504
|
+
schema,
|
|
505
|
+
runtimeSchema: schema,
|
|
506
|
+
payloadCore: {
|
|
507
|
+
relationTo,
|
|
508
|
+
...options.hasMany === void 0 ? {} : { hasMany: options.hasMany },
|
|
509
|
+
...options.required === void 0 ? {} : { required: options.required }
|
|
510
|
+
},
|
|
511
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
512
|
+
required: options.required ?? false,
|
|
513
|
+
nullable: options.nullable ?? false,
|
|
514
|
+
relationTo: Array.isArray(relationTo) ? relationTo : [relationTo],
|
|
515
|
+
hasMany: options.hasMany ?? false,
|
|
516
|
+
relation: { relationTo, hasMany: options.hasMany ?? false, idSchema: options.idSchema }
|
|
517
|
+
},
|
|
518
|
+
options
|
|
519
|
+
));
|
|
520
|
+
}
|
|
521
|
+
function composeObject(fields) {
|
|
522
|
+
const children = fieldEntries(fields);
|
|
523
|
+
const blocking = children.find(([, seed]) => !seed.schema)?.[0];
|
|
524
|
+
if (blocking) return { children, blocking };
|
|
525
|
+
const shape = Object.fromEntries(children.map(([
|
|
526
|
+
name,
|
|
527
|
+
seed
|
|
528
|
+
]) => [
|
|
529
|
+
name,
|
|
530
|
+
seed.schema
|
|
531
|
+
]));
|
|
532
|
+
return { children, schema: z2.strictObject(shape) };
|
|
533
|
+
}
|
|
534
|
+
function group(options) {
|
|
535
|
+
const composed = composeObject(options.fields);
|
|
536
|
+
const schema = composed.schema ? applyNullable(
|
|
537
|
+
composed.schema,
|
|
538
|
+
options.nullable
|
|
539
|
+
) : void 0;
|
|
540
|
+
const runtimeSchema = applyNullable(
|
|
541
|
+
z2.record(
|
|
542
|
+
z2.string(),
|
|
543
|
+
z2.unknown()
|
|
544
|
+
),
|
|
545
|
+
options.nullable
|
|
546
|
+
);
|
|
547
|
+
return createFieldDefinition(withDefaults(
|
|
548
|
+
{
|
|
549
|
+
kind: "group",
|
|
550
|
+
payloadType: "group",
|
|
551
|
+
schema,
|
|
552
|
+
runtimeSchema,
|
|
553
|
+
payloadCore: options.required === void 0 ? {} : { required: options.required },
|
|
554
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
555
|
+
required: options.required ?? false,
|
|
556
|
+
nullable: options.nullable ?? false,
|
|
557
|
+
children: composed.children,
|
|
558
|
+
blockingFieldPath: composed.blocking,
|
|
559
|
+
composite: "group"
|
|
560
|
+
},
|
|
561
|
+
options
|
|
562
|
+
));
|
|
563
|
+
}
|
|
564
|
+
function array(options) {
|
|
565
|
+
validateRange(
|
|
566
|
+
"array",
|
|
567
|
+
"rows",
|
|
568
|
+
options.minRows,
|
|
569
|
+
options.maxRows
|
|
570
|
+
);
|
|
571
|
+
const composed = composeObject(options.fields);
|
|
572
|
+
const schema = composed.schema ? applyNullable(
|
|
573
|
+
z2.array(composed.schema),
|
|
574
|
+
options.nullable
|
|
575
|
+
) : void 0;
|
|
576
|
+
let container = z2.array(z2.unknown());
|
|
577
|
+
if (options.minRows !== void 0) container = container.min(options.minRows);
|
|
578
|
+
if (options.maxRows !== void 0) container = container.max(options.maxRows);
|
|
579
|
+
const runtimeSchema = applyNullable(
|
|
580
|
+
container,
|
|
581
|
+
options.nullable
|
|
582
|
+
);
|
|
583
|
+
return createFieldDefinition(withDefaults(
|
|
584
|
+
{
|
|
585
|
+
kind: "array",
|
|
586
|
+
payloadType: "array",
|
|
587
|
+
schema,
|
|
588
|
+
runtimeSchema,
|
|
589
|
+
payloadCore: {
|
|
590
|
+
...options.required === void 0 ? {} : { required: options.required },
|
|
591
|
+
...options.minRows === void 0 ? {} : { minRows: options.minRows },
|
|
592
|
+
...options.maxRows === void 0 ? {} : { maxRows: options.maxRows }
|
|
593
|
+
},
|
|
594
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
595
|
+
required: options.required ?? false,
|
|
596
|
+
nullable: options.nullable ?? false,
|
|
597
|
+
children: composed.children,
|
|
598
|
+
blockingFieldPath: composed.blocking,
|
|
599
|
+
composite: "array"
|
|
600
|
+
},
|
|
601
|
+
options
|
|
602
|
+
));
|
|
603
|
+
}
|
|
604
|
+
function richText(options) {
|
|
605
|
+
const schema = options.nullable ? options.schema.nullable() : options.schema;
|
|
606
|
+
return createFieldDefinition(withDefaults(
|
|
607
|
+
{
|
|
608
|
+
kind: "richText",
|
|
609
|
+
payloadType: "richText",
|
|
610
|
+
schema,
|
|
611
|
+
runtimeSchema: schema,
|
|
612
|
+
payloadCore: options.required === void 0 ? {} : { required: options.required },
|
|
613
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
614
|
+
required: options.required ?? false,
|
|
615
|
+
nullable: options.nullable ?? false
|
|
616
|
+
},
|
|
617
|
+
options
|
|
618
|
+
));
|
|
619
|
+
}
|
|
620
|
+
function native(options) {
|
|
621
|
+
const payload = options.payload;
|
|
622
|
+
const payloadType = typeof payload.type === "string" ? payload.type : "unknown";
|
|
623
|
+
return createFieldDefinition(withDefaults(
|
|
624
|
+
{
|
|
625
|
+
kind: "native",
|
|
626
|
+
payloadType,
|
|
627
|
+
schema: options.schema,
|
|
628
|
+
runtimeSchema: options.schema,
|
|
629
|
+
payloadCore: {},
|
|
630
|
+
payloadExtras: payload,
|
|
631
|
+
required: payload.required === true,
|
|
632
|
+
nullable: false
|
|
633
|
+
},
|
|
634
|
+
options
|
|
635
|
+
));
|
|
636
|
+
}
|
|
637
|
+
var field = Object.freeze({
|
|
638
|
+
text,
|
|
639
|
+
textarea,
|
|
640
|
+
slug,
|
|
641
|
+
number,
|
|
642
|
+
money,
|
|
643
|
+
boolean,
|
|
644
|
+
date,
|
|
645
|
+
select,
|
|
646
|
+
relationship,
|
|
647
|
+
upload,
|
|
648
|
+
group,
|
|
649
|
+
array,
|
|
650
|
+
richText,
|
|
651
|
+
native
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
// src/internal/clone.ts
|
|
655
|
+
function isPlainObject(value) {
|
|
656
|
+
const prototype = Object.getPrototypeOf(value);
|
|
657
|
+
return prototype === Object.prototype || prototype === null;
|
|
658
|
+
}
|
|
659
|
+
function cloneConfig(value) {
|
|
660
|
+
if (Array.isArray(value)) return value.map((item) => cloneConfig(item));
|
|
661
|
+
if (value === null || typeof value !== "object" || !isPlainObject(value)) return value;
|
|
662
|
+
const clone = Object.create(Object.getPrototypeOf(value));
|
|
663
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
664
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
665
|
+
value,
|
|
666
|
+
key
|
|
667
|
+
);
|
|
668
|
+
if (!descriptor) continue;
|
|
669
|
+
if ("value" in descriptor) descriptor.value = cloneConfig(descriptor.value);
|
|
670
|
+
Object.defineProperty(
|
|
671
|
+
clone,
|
|
672
|
+
key,
|
|
673
|
+
descriptor
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
return clone;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
// src/entity/define-entity.ts
|
|
680
|
+
var reservedFieldNames = /* @__PURE__ */ new Set([
|
|
681
|
+
"__v",
|
|
682
|
+
"file",
|
|
683
|
+
"hash",
|
|
684
|
+
"salt"
|
|
685
|
+
]);
|
|
686
|
+
var dataFieldTypes = /* @__PURE__ */ new Set([
|
|
687
|
+
"array",
|
|
688
|
+
"blocks",
|
|
689
|
+
"checkbox",
|
|
690
|
+
"code",
|
|
691
|
+
"date",
|
|
692
|
+
"email",
|
|
693
|
+
"group",
|
|
694
|
+
"join",
|
|
695
|
+
"json",
|
|
696
|
+
"number",
|
|
697
|
+
"point",
|
|
698
|
+
"radio",
|
|
699
|
+
"relationship",
|
|
700
|
+
"richText",
|
|
701
|
+
"select",
|
|
702
|
+
"text",
|
|
703
|
+
"textarea",
|
|
704
|
+
"upload"
|
|
705
|
+
]);
|
|
706
|
+
var commonReserved = [
|
|
707
|
+
"defaultValue",
|
|
708
|
+
"localized",
|
|
709
|
+
"name",
|
|
710
|
+
"required",
|
|
711
|
+
"type",
|
|
712
|
+
"virtual"
|
|
713
|
+
];
|
|
714
|
+
function errorName(error) {
|
|
715
|
+
return error instanceof Error ? error.name : void 0;
|
|
716
|
+
}
|
|
717
|
+
function isAsyncParseError(error) {
|
|
718
|
+
return error instanceof Error && (error.constructor.name === "$ZodAsyncError" || error.message.includes("Encountered Promise during synchronous parse"));
|
|
719
|
+
}
|
|
720
|
+
function issueSummaries(error) {
|
|
721
|
+
return error.issues.map((issue) => ({
|
|
722
|
+
code: issue.code,
|
|
723
|
+
path: issue.path.map((part) => typeof part === "symbol" ? part.description ?? part.toString() : part),
|
|
724
|
+
message: issue.message
|
|
725
|
+
}));
|
|
726
|
+
}
|
|
727
|
+
function parseCanonical(schema, value, context) {
|
|
728
|
+
try {
|
|
729
|
+
return schema.safeParse(value);
|
|
730
|
+
} catch (error) {
|
|
731
|
+
if (isAsyncParseError(error)) {
|
|
732
|
+
throw new PayloadSchemaError(
|
|
733
|
+
"ASYNC_CANONICAL_SCHEMA_UNSUPPORTED",
|
|
734
|
+
{
|
|
735
|
+
phase: "definition",
|
|
736
|
+
message: `Canonical schema for field "${context.fieldPath}" must be synchronous.`,
|
|
737
|
+
data: context,
|
|
738
|
+
cause: error
|
|
739
|
+
}
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
throw error;
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
function reservedKeys(seed) {
|
|
746
|
+
switch (seed.kind) {
|
|
747
|
+
case "array":
|
|
748
|
+
return [
|
|
749
|
+
...commonReserved,
|
|
750
|
+
"fields",
|
|
751
|
+
"maxRows",
|
|
752
|
+
"minRows"
|
|
753
|
+
];
|
|
754
|
+
case "group":
|
|
755
|
+
return [
|
|
756
|
+
...commonReserved,
|
|
757
|
+
"fields"
|
|
758
|
+
];
|
|
759
|
+
case "money":
|
|
760
|
+
case "number":
|
|
761
|
+
return [
|
|
762
|
+
...commonReserved,
|
|
763
|
+
"max",
|
|
764
|
+
"min"
|
|
765
|
+
];
|
|
766
|
+
case "relationship":
|
|
767
|
+
case "upload":
|
|
768
|
+
return [
|
|
769
|
+
...commonReserved,
|
|
770
|
+
"hasMany",
|
|
771
|
+
"relationTo"
|
|
772
|
+
];
|
|
773
|
+
case "select":
|
|
774
|
+
return [
|
|
775
|
+
...commonReserved,
|
|
776
|
+
"hasMany",
|
|
777
|
+
"options"
|
|
778
|
+
];
|
|
779
|
+
case "slug":
|
|
780
|
+
case "text":
|
|
781
|
+
case "textarea":
|
|
782
|
+
return [
|
|
783
|
+
...commonReserved,
|
|
784
|
+
"maxLength",
|
|
785
|
+
"minLength"
|
|
786
|
+
];
|
|
787
|
+
case "native":
|
|
788
|
+
return [
|
|
789
|
+
"defaultValue",
|
|
790
|
+
"name"
|
|
791
|
+
];
|
|
792
|
+
default:
|
|
793
|
+
return commonReserved;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
function validatePayloadExtras(seed, entity, fieldPath) {
|
|
797
|
+
for (const key of reservedKeys(seed)) {
|
|
798
|
+
if (Object.hasOwn(
|
|
799
|
+
seed.payloadExtras,
|
|
800
|
+
key
|
|
801
|
+
)) {
|
|
802
|
+
throw new PayloadSchemaError(
|
|
803
|
+
"RESERVED_PAYLOAD_OPTION",
|
|
804
|
+
{
|
|
805
|
+
phase: "definition",
|
|
806
|
+
message: `Field "${fieldPath}" cannot configure reserved Payload option "${key}".`,
|
|
807
|
+
data: { entity, fieldPath, fieldKind: seed.kind, option: key }
|
|
808
|
+
}
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
function bindSeed(source, entity, fieldPath, relationshipIdSchema) {
|
|
814
|
+
validatePayloadExtras(
|
|
815
|
+
source,
|
|
816
|
+
entity,
|
|
817
|
+
fieldPath
|
|
818
|
+
);
|
|
819
|
+
if (source.kind === "native" && !dataFieldTypes.has(source.payloadType)) {
|
|
820
|
+
throw new PayloadSchemaError(
|
|
821
|
+
"INVALID_PAYLOAD_FIELD",
|
|
822
|
+
{
|
|
823
|
+
phase: "payload-compilation",
|
|
824
|
+
message: `Field "${fieldPath}" is not a data-affecting Payload field.`,
|
|
825
|
+
data: { entity, fieldPath, fieldKind: source.kind, reason: `unsupported type ${source.payloadType}` }
|
|
826
|
+
}
|
|
827
|
+
);
|
|
828
|
+
}
|
|
829
|
+
if (source.staticDefaultConfigured && source.dynamicDefaultConfigured) {
|
|
830
|
+
throw new PayloadSchemaError(
|
|
831
|
+
"CONFLICTING_DEFAULT_CONFIGURATION",
|
|
832
|
+
{
|
|
833
|
+
phase: "definition",
|
|
834
|
+
message: `Field "${fieldPath}" cannot define both "defaultValue" and "dynamicDefaultValue".`,
|
|
835
|
+
data: {
|
|
836
|
+
entity,
|
|
837
|
+
fieldPath,
|
|
838
|
+
fieldType: source.kind,
|
|
839
|
+
configuredDefaults: [
|
|
840
|
+
"defaultValue",
|
|
841
|
+
"dynamicDefaultValue"
|
|
842
|
+
]
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
if (source.dynamicDefaultConfigured && typeof source.dynamicDefault !== "function") {
|
|
848
|
+
throw new PayloadSchemaError(
|
|
849
|
+
"INVALID_FIELD_CONFIGURATION",
|
|
850
|
+
{
|
|
851
|
+
phase: "definition",
|
|
852
|
+
message: `Field "${fieldPath}" dynamicDefaultValue must be a function.`,
|
|
853
|
+
data: { entity, fieldPath, fieldKind: source.kind, reason: "dynamicDefaultValue is not a function" }
|
|
854
|
+
}
|
|
855
|
+
);
|
|
856
|
+
}
|
|
857
|
+
const children = source.children?.map(([
|
|
858
|
+
name,
|
|
859
|
+
seed
|
|
860
|
+
]) => [
|
|
861
|
+
name,
|
|
862
|
+
bindSeed(
|
|
863
|
+
seed,
|
|
864
|
+
entity,
|
|
865
|
+
`${fieldPath}.${name}`,
|
|
866
|
+
relationshipIdSchema
|
|
867
|
+
)
|
|
868
|
+
]);
|
|
869
|
+
let schema = source.schema;
|
|
870
|
+
let runtimeSchema = source.runtimeSchema;
|
|
871
|
+
let blockingFieldPath = source.blockingFieldPath;
|
|
872
|
+
if (source.relation) {
|
|
873
|
+
const base = createRelationshipSchema(
|
|
874
|
+
source.relation.relationTo,
|
|
875
|
+
source.relation.hasMany,
|
|
876
|
+
source.relation.idSchema ?? relationshipIdSchema
|
|
877
|
+
);
|
|
878
|
+
schema = source.nullable ? base.nullable() : base;
|
|
879
|
+
runtimeSchema = schema;
|
|
880
|
+
}
|
|
881
|
+
if (source.composite && children) {
|
|
882
|
+
const blocking = children.find(([, child]) => !child.schema);
|
|
883
|
+
blockingFieldPath = blocking ? blocking[1].blockingFieldPath ?? `${fieldPath}.${blocking[0]}` : void 0;
|
|
884
|
+
if (blocking) {
|
|
885
|
+
schema = void 0;
|
|
886
|
+
} else {
|
|
887
|
+
const shape = Object.fromEntries(children.map(([
|
|
888
|
+
name,
|
|
889
|
+
child
|
|
890
|
+
]) => [
|
|
891
|
+
name,
|
|
892
|
+
child.schema
|
|
893
|
+
]));
|
|
894
|
+
const objectSchema = z3.strictObject(shape);
|
|
895
|
+
schema = source.composite === "array" ? z3.array(objectSchema) : objectSchema;
|
|
896
|
+
if (source.nullable) schema = schema.nullable();
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
const payloadCore = { ...source.payloadCore };
|
|
900
|
+
if (source.staticDefaultConfigured) {
|
|
901
|
+
if (!schema) {
|
|
902
|
+
throw new PayloadSchemaError(
|
|
903
|
+
"INVALID_FIELD_CONFIGURATION",
|
|
904
|
+
{
|
|
905
|
+
phase: "definition",
|
|
906
|
+
message: `Field "${fieldPath}" cannot define a static default without a canonical schema.`,
|
|
907
|
+
data: { entity, fieldPath, fieldKind: source.kind, reason: "static default requires canonical schema" }
|
|
908
|
+
}
|
|
909
|
+
);
|
|
910
|
+
}
|
|
911
|
+
const result = parseCanonical(
|
|
912
|
+
schema,
|
|
913
|
+
source.staticDefault,
|
|
914
|
+
{ entity, fieldPath, fieldKind: source.kind }
|
|
915
|
+
);
|
|
916
|
+
if (!result.success) {
|
|
917
|
+
throw new PayloadSchemaError(
|
|
918
|
+
"INVALID_DEFAULT_VALUE",
|
|
919
|
+
{
|
|
920
|
+
phase: "definition",
|
|
921
|
+
message: `Field "${fieldPath}" has an invalid defaultValue.`,
|
|
922
|
+
data: { entity, fieldPath, fieldKind: source.kind, issues: issueSummaries(result.error) }
|
|
923
|
+
}
|
|
924
|
+
);
|
|
925
|
+
}
|
|
926
|
+
payloadCore.defaultValue = result.data;
|
|
927
|
+
} else if (source.dynamicDefaultConfigured) {
|
|
928
|
+
payloadCore.defaultValue = source.dynamicDefault;
|
|
929
|
+
}
|
|
930
|
+
return {
|
|
931
|
+
...source,
|
|
932
|
+
schema,
|
|
933
|
+
runtimeSchema,
|
|
934
|
+
children,
|
|
935
|
+
blockingFieldPath,
|
|
936
|
+
payloadCore
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
function validationPath(basePath, issuePath) {
|
|
940
|
+
return [
|
|
941
|
+
...basePath,
|
|
942
|
+
...issuePath
|
|
943
|
+
].map(String).join(".");
|
|
944
|
+
}
|
|
945
|
+
function canonicalHook(seed, entity, fieldPath) {
|
|
946
|
+
return ({ path, req, value }) => {
|
|
947
|
+
if (value === void 0 || !seed.runtimeSchema) return value;
|
|
948
|
+
const result = parseCanonical(
|
|
949
|
+
seed.runtimeSchema,
|
|
950
|
+
value,
|
|
951
|
+
{ entity, fieldPath, fieldKind: seed.kind }
|
|
952
|
+
);
|
|
953
|
+
if (result.success) return result.data;
|
|
954
|
+
const basePath = Array.isArray(path) && path.length ? path : fieldPath.split(".");
|
|
955
|
+
throw new ValidationError({
|
|
956
|
+
req,
|
|
957
|
+
errors: result.error.issues.map((issue) => ({
|
|
958
|
+
path: validationPath(
|
|
959
|
+
basePath,
|
|
960
|
+
issue.path
|
|
961
|
+
),
|
|
962
|
+
message: issue.message
|
|
963
|
+
}))
|
|
964
|
+
});
|
|
965
|
+
};
|
|
966
|
+
}
|
|
967
|
+
function appendCanonicalHook(value, hook) {
|
|
968
|
+
const hooks = value && typeof value === "object" ? cloneConfig(value) : {};
|
|
969
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
970
|
+
hooks,
|
|
971
|
+
"beforeValidate"
|
|
972
|
+
);
|
|
973
|
+
if (descriptor && !("value" in descriptor)) {
|
|
974
|
+
Object.defineProperty(
|
|
975
|
+
hooks,
|
|
976
|
+
"beforeValidate",
|
|
977
|
+
{
|
|
978
|
+
...descriptor,
|
|
979
|
+
get: descriptor.get ? function() {
|
|
980
|
+
const consumerHooks2 = descriptor.get?.call(this);
|
|
981
|
+
return [
|
|
982
|
+
...Array.isArray(consumerHooks2) ? consumerHooks2 : [],
|
|
983
|
+
hook
|
|
984
|
+
];
|
|
985
|
+
} : () => [hook]
|
|
986
|
+
}
|
|
987
|
+
);
|
|
988
|
+
return hooks;
|
|
989
|
+
}
|
|
990
|
+
const consumerHooks = descriptor && "value" in descriptor && Array.isArray(descriptor.value) ? descriptor.value : [];
|
|
991
|
+
Object.defineProperty(
|
|
992
|
+
hooks,
|
|
993
|
+
"beforeValidate",
|
|
994
|
+
{
|
|
995
|
+
configurable: true,
|
|
996
|
+
enumerable: true,
|
|
997
|
+
value: [
|
|
998
|
+
...consumerHooks,
|
|
999
|
+
hook
|
|
1000
|
+
],
|
|
1001
|
+
writable: true
|
|
1002
|
+
}
|
|
1003
|
+
);
|
|
1004
|
+
return hooks;
|
|
1005
|
+
}
|
|
1006
|
+
function compileField(seed, name, entity, fieldPath, operation) {
|
|
1007
|
+
try {
|
|
1008
|
+
const extras = cloneConfig(seed.payloadExtras);
|
|
1009
|
+
const core = cloneConfig(seed.payloadCore);
|
|
1010
|
+
const compiled = Object.create(Object.prototype);
|
|
1011
|
+
const extraDescriptors = Object.getOwnPropertyDescriptors(extras);
|
|
1012
|
+
const hooksDescriptor = extraDescriptors.hooks;
|
|
1013
|
+
delete extraDescriptors.hooks;
|
|
1014
|
+
Object.defineProperties(
|
|
1015
|
+
compiled,
|
|
1016
|
+
extraDescriptors
|
|
1017
|
+
);
|
|
1018
|
+
Object.defineProperties(
|
|
1019
|
+
compiled,
|
|
1020
|
+
Object.getOwnPropertyDescriptors(core)
|
|
1021
|
+
);
|
|
1022
|
+
Object.defineProperties(
|
|
1023
|
+
compiled,
|
|
1024
|
+
{
|
|
1025
|
+
name: { configurable: true, enumerable: true, value: name, writable: true },
|
|
1026
|
+
type: { configurable: true, enumerable: true, value: seed.payloadType, writable: true }
|
|
1027
|
+
}
|
|
1028
|
+
);
|
|
1029
|
+
if (seed.children) {
|
|
1030
|
+
compiled.fields = seed.children.map(([
|
|
1031
|
+
childName,
|
|
1032
|
+
child
|
|
1033
|
+
]) => compileField(
|
|
1034
|
+
child,
|
|
1035
|
+
childName,
|
|
1036
|
+
entity,
|
|
1037
|
+
`${fieldPath}.${childName}`,
|
|
1038
|
+
operation
|
|
1039
|
+
));
|
|
1040
|
+
}
|
|
1041
|
+
if (seed.runtimeSchema) {
|
|
1042
|
+
const hook = canonicalHook(
|
|
1043
|
+
seed,
|
|
1044
|
+
entity,
|
|
1045
|
+
fieldPath
|
|
1046
|
+
);
|
|
1047
|
+
if (hooksDescriptor && !("value" in hooksDescriptor)) {
|
|
1048
|
+
Object.defineProperty(
|
|
1049
|
+
compiled,
|
|
1050
|
+
"hooks",
|
|
1051
|
+
{
|
|
1052
|
+
...hooksDescriptor,
|
|
1053
|
+
get: hooksDescriptor.get ? function() {
|
|
1054
|
+
return appendCanonicalHook(
|
|
1055
|
+
hooksDescriptor.get?.call(this),
|
|
1056
|
+
hook
|
|
1057
|
+
);
|
|
1058
|
+
} : () => appendCanonicalHook(
|
|
1059
|
+
void 0,
|
|
1060
|
+
hook
|
|
1061
|
+
)
|
|
1062
|
+
}
|
|
1063
|
+
);
|
|
1064
|
+
} else {
|
|
1065
|
+
Object.defineProperty(
|
|
1066
|
+
compiled,
|
|
1067
|
+
"hooks",
|
|
1068
|
+
{
|
|
1069
|
+
configurable: true,
|
|
1070
|
+
enumerable: true,
|
|
1071
|
+
value: appendCanonicalHook(
|
|
1072
|
+
hooksDescriptor && "value" in hooksDescriptor ? hooksDescriptor.value : void 0,
|
|
1073
|
+
hook
|
|
1074
|
+
),
|
|
1075
|
+
writable: true
|
|
1076
|
+
}
|
|
1077
|
+
);
|
|
1078
|
+
}
|
|
1079
|
+
} else if (hooksDescriptor) {
|
|
1080
|
+
Object.defineProperty(
|
|
1081
|
+
compiled,
|
|
1082
|
+
"hooks",
|
|
1083
|
+
hooksDescriptor
|
|
1084
|
+
);
|
|
1085
|
+
}
|
|
1086
|
+
if (!dataFieldTypes.has(seed.payloadType)) {
|
|
1087
|
+
throw new PayloadSchemaError(
|
|
1088
|
+
"INVALID_PAYLOAD_FIELD",
|
|
1089
|
+
{
|
|
1090
|
+
phase: "payload-compilation",
|
|
1091
|
+
message: `Field "${fieldPath}" does not compile to a data-affecting Payload field.`,
|
|
1092
|
+
data: { entity, fieldPath, fieldKind: seed.kind, reason: `unsupported type ${seed.payloadType}` }
|
|
1093
|
+
}
|
|
1094
|
+
);
|
|
1095
|
+
}
|
|
1096
|
+
return compiled;
|
|
1097
|
+
} catch (error) {
|
|
1098
|
+
if (isPayloadSchemaError(error)) throw error;
|
|
1099
|
+
throw new PayloadSchemaError(
|
|
1100
|
+
"PAYLOAD_COMPILATION_FAILED",
|
|
1101
|
+
{
|
|
1102
|
+
phase: "payload-compilation",
|
|
1103
|
+
message: `Failed to compile Payload field "${fieldPath}".`,
|
|
1104
|
+
data: {
|
|
1105
|
+
entity,
|
|
1106
|
+
fieldPath,
|
|
1107
|
+
fieldKind: seed.kind,
|
|
1108
|
+
operation,
|
|
1109
|
+
causeName: errorName(error)
|
|
1110
|
+
},
|
|
1111
|
+
cause: error
|
|
1112
|
+
}
|
|
1113
|
+
);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
function assertFieldName(entity, name) {
|
|
1117
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(name)) {
|
|
1118
|
+
throw new PayloadSchemaError(
|
|
1119
|
+
"INVALID_FIELD_NAME",
|
|
1120
|
+
{
|
|
1121
|
+
phase: "definition",
|
|
1122
|
+
message: `Invalid field name "${name}" in entity "${entity}".`,
|
|
1123
|
+
data: { entity, field: name }
|
|
1124
|
+
}
|
|
1125
|
+
);
|
|
1126
|
+
}
|
|
1127
|
+
if (reservedFieldNames.has(name)) {
|
|
1128
|
+
throw new PayloadSchemaError(
|
|
1129
|
+
"RESERVED_FIELD_NAME",
|
|
1130
|
+
{
|
|
1131
|
+
phase: "definition",
|
|
1132
|
+
message: `Field name "${name}" is reserved by Payload.`,
|
|
1133
|
+
data: { entity, field: name, reservedName: name }
|
|
1134
|
+
}
|
|
1135
|
+
);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
function schemaCapableFields(fields) {
|
|
1139
|
+
return Object.fromEntries(Object.entries(fields).filter(([, definition]) => definition.schema).map(([
|
|
1140
|
+
name,
|
|
1141
|
+
definition
|
|
1142
|
+
]) => [
|
|
1143
|
+
name,
|
|
1144
|
+
definition.schema
|
|
1145
|
+
]));
|
|
1146
|
+
}
|
|
1147
|
+
function createInspection(name, seeds) {
|
|
1148
|
+
const inspectFields = (entries) => Object.fromEntries(entries.map(([
|
|
1149
|
+
fieldName,
|
|
1150
|
+
seed
|
|
1151
|
+
]) => [
|
|
1152
|
+
fieldName,
|
|
1153
|
+
{
|
|
1154
|
+
kind: seed.kind,
|
|
1155
|
+
payloadType: seed.payloadType,
|
|
1156
|
+
requiredInPayload: seed.required,
|
|
1157
|
+
nullableInSchema: seed.nullable,
|
|
1158
|
+
hasSchema: Boolean(seed.schema),
|
|
1159
|
+
...seed.relationTo ? { relationTo: [...seed.relationTo] } : {},
|
|
1160
|
+
...seed.hasMany === void 0 ? {} : { hasMany: seed.hasMany },
|
|
1161
|
+
...seed.currency === void 0 ? {} : { currency: seed.currency },
|
|
1162
|
+
...seed.children ? { nested: inspectFields(seed.children) } : {},
|
|
1163
|
+
...seed.blockingFieldPath ? { blockingFieldPath: seed.blockingFieldPath } : {}
|
|
1164
|
+
}
|
|
1165
|
+
]));
|
|
1166
|
+
return { name, fields: inspectFields(seeds) };
|
|
1167
|
+
}
|
|
1168
|
+
function defineEntity(options) {
|
|
1169
|
+
if (typeof options.name !== "string" || !options.name.trim()) {
|
|
1170
|
+
throw new PayloadSchemaError(
|
|
1171
|
+
"INVALID_ENTITY_NAME",
|
|
1172
|
+
{
|
|
1173
|
+
phase: "definition",
|
|
1174
|
+
message: "Entity name must be a non-empty string.",
|
|
1175
|
+
data: { received: typeof options.name === "string" ? options.name : String(options.name) }
|
|
1176
|
+
}
|
|
1177
|
+
);
|
|
1178
|
+
}
|
|
1179
|
+
const relationshipIdSchema = options.relationshipIdSchema ?? defaultIdSchema;
|
|
1180
|
+
const boundEntries = fieldEntries(options.fields).map(([
|
|
1181
|
+
name,
|
|
1182
|
+
seed
|
|
1183
|
+
]) => {
|
|
1184
|
+
assertFieldName(
|
|
1185
|
+
options.name,
|
|
1186
|
+
name
|
|
1187
|
+
);
|
|
1188
|
+
return [
|
|
1189
|
+
name,
|
|
1190
|
+
bindSeed(
|
|
1191
|
+
seed,
|
|
1192
|
+
options.name,
|
|
1193
|
+
name,
|
|
1194
|
+
relationshipIdSchema
|
|
1195
|
+
)
|
|
1196
|
+
];
|
|
1197
|
+
});
|
|
1198
|
+
const boundFields = Object.freeze(Object.fromEntries(boundEntries.map(([
|
|
1199
|
+
name,
|
|
1200
|
+
seed
|
|
1201
|
+
]) => [
|
|
1202
|
+
name,
|
|
1203
|
+
createFieldDefinition(seed)
|
|
1204
|
+
])));
|
|
1205
|
+
const fieldSchemas = schemaCapableFields(boundFields);
|
|
1206
|
+
const availableFields = Object.keys(boundFields);
|
|
1207
|
+
const getEntry = (key) => {
|
|
1208
|
+
const entry = boundEntries.find(([name]) => name === key);
|
|
1209
|
+
if (!entry) {
|
|
1210
|
+
throw new PayloadSchemaError(
|
|
1211
|
+
"UNKNOWN_FIELD",
|
|
1212
|
+
{
|
|
1213
|
+
phase: "schema-derivation",
|
|
1214
|
+
message: `Unknown field "${String(key)}" in entity "${options.name}".`,
|
|
1215
|
+
data: { entity: options.name, field: String(key), availableFields }
|
|
1216
|
+
}
|
|
1217
|
+
);
|
|
1218
|
+
}
|
|
1219
|
+
return entry;
|
|
1220
|
+
};
|
|
1221
|
+
const payload = Object.freeze({
|
|
1222
|
+
all: () => boundEntries.map(([
|
|
1223
|
+
name,
|
|
1224
|
+
seed
|
|
1225
|
+
]) => compileField(
|
|
1226
|
+
seed,
|
|
1227
|
+
name,
|
|
1228
|
+
options.name,
|
|
1229
|
+
name,
|
|
1230
|
+
"all"
|
|
1231
|
+
)),
|
|
1232
|
+
field: (key) => {
|
|
1233
|
+
const [
|
|
1234
|
+
name,
|
|
1235
|
+
seed
|
|
1236
|
+
] = getEntry(key);
|
|
1237
|
+
return compileField(
|
|
1238
|
+
seed,
|
|
1239
|
+
name,
|
|
1240
|
+
options.name,
|
|
1241
|
+
name,
|
|
1242
|
+
"field"
|
|
1243
|
+
);
|
|
1244
|
+
},
|
|
1245
|
+
pick: (keys) => keys.map((key) => {
|
|
1246
|
+
const [
|
|
1247
|
+
name,
|
|
1248
|
+
seed
|
|
1249
|
+
] = getEntry(key);
|
|
1250
|
+
return compileField(
|
|
1251
|
+
seed,
|
|
1252
|
+
name,
|
|
1253
|
+
options.name,
|
|
1254
|
+
name,
|
|
1255
|
+
"pick"
|
|
1256
|
+
);
|
|
1257
|
+
})
|
|
1258
|
+
});
|
|
1259
|
+
const pick = ((keys, pickOptions) => {
|
|
1260
|
+
const required = new Set(pickOptions?.required ?? []);
|
|
1261
|
+
const optional = pickOptions?.optional;
|
|
1262
|
+
const shape = {};
|
|
1263
|
+
for (const key of keys) {
|
|
1264
|
+
const [
|
|
1265
|
+
name,
|
|
1266
|
+
seed
|
|
1267
|
+
] = getEntry(key);
|
|
1268
|
+
if (!seed.schema) {
|
|
1269
|
+
throw new PayloadSchemaError(
|
|
1270
|
+
"SCHEMA_UNAVAILABLE",
|
|
1271
|
+
{
|
|
1272
|
+
phase: "schema-derivation",
|
|
1273
|
+
message: `Canonical schema is unavailable for field "${name}".`,
|
|
1274
|
+
data: {
|
|
1275
|
+
entity: options.name,
|
|
1276
|
+
fieldPath: name,
|
|
1277
|
+
fieldKind: seed.kind,
|
|
1278
|
+
reason: seed.kind === "native" ? "native-without-schema" : "schema-less-descendant",
|
|
1279
|
+
blockingFieldPath: seed.blockingFieldPath
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1284
|
+
const makeOptional = !required.has(key) && (optional === "all" || Array.isArray(optional) && optional.includes(key));
|
|
1285
|
+
shape[name] = makeOptional ? seed.schema.optional() : seed.schema;
|
|
1286
|
+
}
|
|
1287
|
+
return pickOptions?.strict === false ? z3.object(shape) : z3.strictObject(shape);
|
|
1288
|
+
});
|
|
1289
|
+
const context = Object.freeze({ fields: fieldSchemas, pick, z: z3 });
|
|
1290
|
+
const entity = Object.freeze({
|
|
1291
|
+
name: options.name,
|
|
1292
|
+
idSchema: options.idSchema ?? defaultIdSchema,
|
|
1293
|
+
fields: boundFields,
|
|
1294
|
+
payload,
|
|
1295
|
+
schema(factory) {
|
|
1296
|
+
try {
|
|
1297
|
+
const result = factory(context);
|
|
1298
|
+
if (!result || typeof result !== "object" || typeof result.safeParse !== "function") {
|
|
1299
|
+
throw new PayloadSchemaError(
|
|
1300
|
+
"INVALID_SCHEMA_FACTORY_RESULT",
|
|
1301
|
+
{
|
|
1302
|
+
phase: "schema-derivation",
|
|
1303
|
+
message: `Schema factory for entity "${options.name}" did not return a Zod schema.`,
|
|
1304
|
+
data: { entity: options.name, receivedType: result === null ? "null" : typeof result }
|
|
1305
|
+
}
|
|
1306
|
+
);
|
|
1307
|
+
}
|
|
1308
|
+
return result;
|
|
1309
|
+
} catch (error) {
|
|
1310
|
+
if (isPayloadSchemaError(error)) throw error;
|
|
1311
|
+
throw new PayloadSchemaError(
|
|
1312
|
+
"SCHEMA_DERIVATION_FAILED",
|
|
1313
|
+
{
|
|
1314
|
+
phase: "schema-derivation",
|
|
1315
|
+
message: `Failed to derive a schema for entity "${options.name}".`,
|
|
1316
|
+
data: { entity: options.name, causeName: errorName(error) },
|
|
1317
|
+
cause: error
|
|
1318
|
+
}
|
|
1319
|
+
);
|
|
1320
|
+
}
|
|
1321
|
+
},
|
|
1322
|
+
inspect: () => createInspection(
|
|
1323
|
+
options.name,
|
|
1324
|
+
boundEntries
|
|
1325
|
+
)
|
|
1326
|
+
});
|
|
1327
|
+
return entity;
|
|
1328
|
+
}
|
|
1329
|
+
export {
|
|
1330
|
+
PayloadSchemaError,
|
|
1331
|
+
defaultIdSchema,
|
|
1332
|
+
defineEntity,
|
|
1333
|
+
field,
|
|
1334
|
+
isPayloadSchemaError
|
|
1335
|
+
};
|
|
1336
|
+
//# sourceMappingURL=index.js.map
|