@nexload-sdk/payload-schema 1.1.0 → 3.0.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 +30 -0
- package/README.md +162 -24
- package/dist/core/default-id.d.ts.map +1 -0
- package/dist/{errors.d.ts → core/errors.d.ts} +3 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/{types.d.ts → core/types.d.ts} +50 -31
- package/dist/core/types.d.ts.map +1 -0
- package/dist/entity/define-entity.d.ts +2 -2
- package/dist/entity/define-entity.d.ts.map +1 -1
- package/dist/fields/index.d.ts +42 -17
- package/dist/fields/index.d.ts.map +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +286 -374
- package/dist/index.js.map +4 -4
- package/dist/internal/blocks-schema.d.ts +9 -0
- package/dist/internal/blocks-schema.d.ts.map +1 -0
- package/dist/internal/canonical-parse.d.ts +11 -0
- package/dist/internal/canonical-parse.d.ts.map +1 -0
- package/dist/internal/chunk-6S4TDSLR.js +157 -0
- package/dist/internal/chunk-6S4TDSLR.js.map +7 -0
- package/dist/internal/chunk-BX5LWYAU.js +157 -0
- package/dist/internal/chunk-BX5LWYAU.js.map +7 -0
- package/dist/internal/clone.d.ts.map +1 -1
- package/dist/internal/composite-child.d.ts +4 -0
- package/dist/internal/composite-child.d.ts.map +1 -0
- package/dist/internal/data-field-types.d.ts +3 -0
- package/dist/internal/data-field-types.d.ts.map +1 -0
- package/dist/internal/entity-binding.d.ts +10 -0
- package/dist/internal/entity-binding.d.ts.map +1 -0
- package/dist/internal/field-definition.d.ts +11 -2
- package/dist/internal/field-definition.d.ts.map +1 -1
- package/dist/payload/canonical-hook.d.ts +5 -0
- package/dist/payload/canonical-hook.d.ts.map +1 -0
- package/dist/payload/compile-field.d.ts +4 -0
- package/dist/payload/compile-field.d.ts.map +1 -0
- package/dist/payload/index.d.ts +3 -0
- package/dist/payload/index.d.ts.map +1 -0
- package/dist/payload/index.js +291 -0
- package/dist/payload/index.js.map +7 -0
- package/dist/payload/payload-fields.d.ts +7 -0
- package/dist/payload/payload-fields.d.ts.map +1 -0
- package/package.json +10 -5
- package/dist/default-id.d.ts.map +0 -1
- package/dist/errors.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- /package/dist/{default-id.d.ts → core/default-id.d.ts} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
// @nexload-sdk/payload-schema
|
|
1
|
+
// @nexload-sdk/payload-schema v3.0.0 (build 2026-11-25T11:31:32.762Z)
|
|
2
|
+
import {
|
|
3
|
+
PayloadSchemaError,
|
|
4
|
+
attachEntityBinding,
|
|
5
|
+
dataFieldTypes,
|
|
6
|
+
errorName,
|
|
7
|
+
findEntityEntry,
|
|
8
|
+
isPayloadSchemaError,
|
|
9
|
+
issueSummaries,
|
|
10
|
+
parseCanonical
|
|
11
|
+
} from "./internal/chunk-BX5LWYAU.js";
|
|
2
12
|
|
|
3
|
-
// src/default-id.ts
|
|
13
|
+
// src/core/default-id.ts
|
|
4
14
|
import { z } from "zod";
|
|
5
15
|
var defaultIdSchema = z.union([
|
|
6
16
|
z.string().min(1),
|
|
@@ -8,42 +18,56 @@ var defaultIdSchema = z.union([
|
|
|
8
18
|
]);
|
|
9
19
|
|
|
10
20
|
// src/entity/define-entity.ts
|
|
11
|
-
import {
|
|
21
|
+
import { z as z5 } from "zod";
|
|
22
|
+
|
|
23
|
+
// src/fields/index.ts
|
|
24
|
+
import { z as z4 } from "zod";
|
|
25
|
+
|
|
26
|
+
// src/internal/blocks-schema.ts
|
|
12
27
|
import { z as z3 } from "zod";
|
|
13
28
|
|
|
14
|
-
// src/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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;
|
|
29
|
+
// src/internal/composite-child.ts
|
|
30
|
+
import { z as z2 } from "zod";
|
|
31
|
+
function compositeChildSchema(seed) {
|
|
32
|
+
if (!seed.schema) {
|
|
33
|
+
throw new Error("Cannot build composite child schema without canonical schema.");
|
|
30
34
|
}
|
|
31
|
-
|
|
32
|
-
return
|
|
33
|
-
name: "PayloadSchemaError",
|
|
34
|
-
code: this.code,
|
|
35
|
-
phase: this.phase,
|
|
36
|
-
message: this.message,
|
|
37
|
-
data: this.data
|
|
38
|
-
};
|
|
35
|
+
if (seed.required === true) {
|
|
36
|
+
return seed.schema;
|
|
39
37
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return
|
|
38
|
+
const isAlreadyNullable = seed.nullable === true || seed.schema instanceof z2.ZodNullable || typeof seed.schema.isNullable === "function" && seed.schema.isNullable();
|
|
39
|
+
const nullableSchema = isAlreadyNullable ? seed.schema : seed.schema.nullable();
|
|
40
|
+
return nullableSchema.optional();
|
|
43
41
|
}
|
|
44
42
|
|
|
45
|
-
// src/
|
|
46
|
-
|
|
43
|
+
// src/internal/blocks-schema.ts
|
|
44
|
+
function buildBlocksSchema(blocks2, options = {}) {
|
|
45
|
+
const blockSchemas = blocks2.map((block) => {
|
|
46
|
+
const shape = Object.fromEntries(
|
|
47
|
+
block.children.map(([
|
|
48
|
+
name,
|
|
49
|
+
child
|
|
50
|
+
]) => [
|
|
51
|
+
name,
|
|
52
|
+
compositeChildSchema(child)
|
|
53
|
+
])
|
|
54
|
+
);
|
|
55
|
+
return z3.strictObject({
|
|
56
|
+
blockType: z3.literal(block.slug),
|
|
57
|
+
...shape,
|
|
58
|
+
id: z3.string().optional(),
|
|
59
|
+
blockName: z3.string().nullable().optional()
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
const union = z3.discriminatedUnion(
|
|
63
|
+
"blockType",
|
|
64
|
+
blockSchemas
|
|
65
|
+
);
|
|
66
|
+
let arraySchema = z3.array(union);
|
|
67
|
+
if (options.minRows !== void 0) arraySchema = arraySchema.min(options.minRows);
|
|
68
|
+
if (options.maxRows !== void 0) arraySchema = arraySchema.max(options.maxRows);
|
|
69
|
+
return options.nullable ? arraySchema.nullable() : arraySchema;
|
|
70
|
+
}
|
|
47
71
|
|
|
48
72
|
// src/internal/field-definition.ts
|
|
49
73
|
var fieldSeedSymbol = /* @__PURE__ */ Symbol("payload-schema.field-seed");
|
|
@@ -123,9 +147,9 @@ function buildTextSchema(options, slug2) {
|
|
|
123
147
|
options.minLength,
|
|
124
148
|
options.maxLength
|
|
125
149
|
);
|
|
126
|
-
let schema =
|
|
150
|
+
let schema = z4.string();
|
|
127
151
|
if (slug2) {
|
|
128
|
-
schema = schema.
|
|
152
|
+
schema = schema.overwrite((value) => value.normalize("NFKC").trim().toLowerCase().replace(
|
|
129
153
|
/[\s_]+/gu,
|
|
130
154
|
"-"
|
|
131
155
|
).replace(
|
|
@@ -136,11 +160,11 @@ function buildTextSchema(options, slug2) {
|
|
|
136
160
|
""
|
|
137
161
|
));
|
|
138
162
|
} else {
|
|
139
|
-
if (options.trim) schema = schema.
|
|
140
|
-
if (options.lowercase) schema = schema.
|
|
141
|
-
if (options.uppercase) schema = schema.
|
|
163
|
+
if (options.trim) schema = schema.overwrite((value) => value.trim());
|
|
164
|
+
if (options.lowercase) schema = schema.overwrite((value) => value.toLowerCase());
|
|
165
|
+
if (options.uppercase) schema = schema.overwrite((value) => value.toUpperCase());
|
|
142
166
|
}
|
|
143
|
-
let constraints =
|
|
167
|
+
let constraints = z4.string();
|
|
144
168
|
if (options.minLength !== void 0) constraints = constraints.min(options.minLength);
|
|
145
169
|
if (options.maxLength !== void 0) constraints = constraints.max(options.maxLength);
|
|
146
170
|
if (options.pattern) constraints = constraints.regex(options.pattern);
|
|
@@ -227,7 +251,7 @@ function buildNumberSchema(options, forceSafeInteger = false) {
|
|
|
227
251
|
options.minimum,
|
|
228
252
|
options.maximum
|
|
229
253
|
);
|
|
230
|
-
let schema =
|
|
254
|
+
let schema = z4.number().refine(
|
|
231
255
|
Number.isFinite,
|
|
232
256
|
{ error: "Expected a finite number" }
|
|
233
257
|
);
|
|
@@ -298,7 +322,7 @@ function money(options) {
|
|
|
298
322
|
));
|
|
299
323
|
}
|
|
300
324
|
function boolean(options = {}) {
|
|
301
|
-
let base =
|
|
325
|
+
let base = z4.boolean();
|
|
302
326
|
if (options.schema) base = options.schema(base);
|
|
303
327
|
const schema = applyNullable(
|
|
304
328
|
base,
|
|
@@ -319,7 +343,7 @@ function boolean(options = {}) {
|
|
|
319
343
|
));
|
|
320
344
|
}
|
|
321
345
|
function date(options = {}) {
|
|
322
|
-
const iso =
|
|
346
|
+
const iso = z4.iso.datetime({ offset: true });
|
|
323
347
|
const parseBound = (value, name) => {
|
|
324
348
|
if (value === void 0) return void 0;
|
|
325
349
|
const parsed = iso.safeParse(value);
|
|
@@ -349,7 +373,7 @@ function date(options = {}) {
|
|
|
349
373
|
minimum,
|
|
350
374
|
maximum
|
|
351
375
|
);
|
|
352
|
-
let base = iso.
|
|
376
|
+
let base = iso.overwrite((value) => new Date(value).toISOString());
|
|
353
377
|
if (minimum !== void 0) base = base.refine(
|
|
354
378
|
(value) => Date.parse(value) >= minimum,
|
|
355
379
|
{ error: "Date is before minimum" }
|
|
@@ -363,12 +387,27 @@ function date(options = {}) {
|
|
|
363
387
|
base,
|
|
364
388
|
options.nullable
|
|
365
389
|
);
|
|
390
|
+
const runtimeDate = z4.unknown().transform((value, ctx) => {
|
|
391
|
+
if (value instanceof Date) {
|
|
392
|
+
const dateValue = value;
|
|
393
|
+
if (Number.isNaN(dateValue.getTime())) {
|
|
394
|
+
ctx.addIssue({
|
|
395
|
+
code: "custom",
|
|
396
|
+
message: "Invalid date"
|
|
397
|
+
});
|
|
398
|
+
return z4.NEVER;
|
|
399
|
+
}
|
|
400
|
+
return dateValue.toISOString();
|
|
401
|
+
}
|
|
402
|
+
return value;
|
|
403
|
+
});
|
|
404
|
+
const runtimeSchema = runtimeDate.pipe(schema);
|
|
366
405
|
return createFieldDefinition(withDefaults(
|
|
367
406
|
{
|
|
368
407
|
kind: "date",
|
|
369
408
|
payloadType: "date",
|
|
370
409
|
schema,
|
|
371
|
-
runtimeSchema
|
|
410
|
+
runtimeSchema,
|
|
372
411
|
payloadCore: options.required === void 0 ? {} : { required: options.required },
|
|
373
412
|
payloadExtras: asPayloadExtras(options.payload),
|
|
374
413
|
required: options.required ?? false,
|
|
@@ -399,8 +438,8 @@ function select(options) {
|
|
|
399
438
|
}
|
|
400
439
|
);
|
|
401
440
|
}
|
|
402
|
-
const enumSchema =
|
|
403
|
-
const valueSchema = options.hasMany ?
|
|
441
|
+
const enumSchema = z4.enum(values);
|
|
442
|
+
const valueSchema = options.hasMany ? z4.array(enumSchema) : enumSchema;
|
|
404
443
|
const schema = applyNullable(
|
|
405
444
|
valueSchema,
|
|
406
445
|
options.nullable
|
|
@@ -426,14 +465,14 @@ function select(options) {
|
|
|
426
465
|
));
|
|
427
466
|
}
|
|
428
467
|
function createRelationshipSchema(relationTo, hasMany, idSchema) {
|
|
429
|
-
const base = Array.isArray(relationTo) ?
|
|
468
|
+
const base = Array.isArray(relationTo) ? z4.discriminatedUnion(
|
|
430
469
|
"relationTo",
|
|
431
|
-
relationTo.map((slug2) =>
|
|
432
|
-
relationTo:
|
|
470
|
+
relationTo.map((slug2) => z4.strictObject({
|
|
471
|
+
relationTo: z4.literal(slug2),
|
|
433
472
|
value: idSchema
|
|
434
473
|
}))
|
|
435
474
|
) : idSchema;
|
|
436
|
-
return hasMany ?
|
|
475
|
+
return hasMany ? z4.array(base) : base;
|
|
437
476
|
}
|
|
438
477
|
function validateRelationTo(relationTo) {
|
|
439
478
|
if (Array.isArray(relationTo) && (!relationTo.length || relationTo.some((slug2) => !slug2)) || !Array.isArray(relationTo) && !relationTo) {
|
|
@@ -520,16 +559,19 @@ function upload(options) {
|
|
|
520
559
|
}
|
|
521
560
|
function composeObject(fields) {
|
|
522
561
|
const children = fieldEntries(fields);
|
|
523
|
-
const
|
|
524
|
-
if (
|
|
562
|
+
const blockingEntry = children.find(([, seed]) => !seed.schema);
|
|
563
|
+
if (blockingEntry) {
|
|
564
|
+
const blocking = blockingEntry[1].blockingFieldPath ? `${blockingEntry[0]}.${blockingEntry[1].blockingFieldPath}` : blockingEntry[0];
|
|
565
|
+
return { children, blocking };
|
|
566
|
+
}
|
|
525
567
|
const shape = Object.fromEntries(children.map(([
|
|
526
568
|
name,
|
|
527
569
|
seed
|
|
528
570
|
]) => [
|
|
529
571
|
name,
|
|
530
|
-
seed
|
|
572
|
+
compositeChildSchema(seed)
|
|
531
573
|
]));
|
|
532
|
-
return { children, schema:
|
|
574
|
+
return { children, schema: z4.strictObject(shape) };
|
|
533
575
|
}
|
|
534
576
|
function group(options) {
|
|
535
577
|
const composed = composeObject(options.fields);
|
|
@@ -538,9 +580,9 @@ function group(options) {
|
|
|
538
580
|
options.nullable
|
|
539
581
|
) : void 0;
|
|
540
582
|
const runtimeSchema = applyNullable(
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
583
|
+
z4.record(
|
|
584
|
+
z4.string(),
|
|
585
|
+
z4.unknown()
|
|
544
586
|
),
|
|
545
587
|
options.nullable
|
|
546
588
|
);
|
|
@@ -570,10 +612,10 @@ function array(options) {
|
|
|
570
612
|
);
|
|
571
613
|
const composed = composeObject(options.fields);
|
|
572
614
|
const schema = composed.schema ? applyNullable(
|
|
573
|
-
|
|
615
|
+
z4.array(composed.schema),
|
|
574
616
|
options.nullable
|
|
575
617
|
) : void 0;
|
|
576
|
-
let container =
|
|
618
|
+
let container = z4.array(z4.unknown());
|
|
577
619
|
if (options.minRows !== void 0) container = container.min(options.minRows);
|
|
578
620
|
if (options.maxRows !== void 0) container = container.max(options.maxRows);
|
|
579
621
|
const runtimeSchema = applyNullable(
|
|
@@ -601,6 +643,126 @@ function array(options) {
|
|
|
601
643
|
options
|
|
602
644
|
));
|
|
603
645
|
}
|
|
646
|
+
function blocks(options) {
|
|
647
|
+
validateRange(
|
|
648
|
+
"blocks",
|
|
649
|
+
"rows",
|
|
650
|
+
options.minRows,
|
|
651
|
+
options.maxRows
|
|
652
|
+
);
|
|
653
|
+
if (!options.blocks || Object.keys(options.blocks).length === 0) {
|
|
654
|
+
throw new PayloadSchemaError(
|
|
655
|
+
"INVALID_FIELD_CONFIGURATION",
|
|
656
|
+
{
|
|
657
|
+
phase: "definition",
|
|
658
|
+
message: "Blocks field must define at least one block.",
|
|
659
|
+
data: { fieldPath: unboundPath, fieldKind: "blocks", reason: "empty blocks configuration" }
|
|
660
|
+
}
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
const blockEntries = Object.entries(options.blocks);
|
|
664
|
+
const reservedBlockChildNames = /* @__PURE__ */ new Set([
|
|
665
|
+
"id",
|
|
666
|
+
"blockName",
|
|
667
|
+
"blockType"
|
|
668
|
+
]);
|
|
669
|
+
for (const [slug2, blockDef] of blockEntries) {
|
|
670
|
+
if (typeof slug2 !== "string" || !slug2.trim()) {
|
|
671
|
+
throw new PayloadSchemaError(
|
|
672
|
+
"INVALID_FIELD_CONFIGURATION",
|
|
673
|
+
{
|
|
674
|
+
phase: "definition",
|
|
675
|
+
message: "Block slug must be a non-empty string.",
|
|
676
|
+
data: { fieldPath: unboundPath, fieldKind: "blocks", reason: "empty block slug" }
|
|
677
|
+
}
|
|
678
|
+
);
|
|
679
|
+
}
|
|
680
|
+
for (const childName of Object.keys(blockDef.fields)) {
|
|
681
|
+
if (reservedBlockChildNames.has(childName)) {
|
|
682
|
+
throw new PayloadSchemaError(
|
|
683
|
+
"INVALID_FIELD_CONFIGURATION",
|
|
684
|
+
{
|
|
685
|
+
phase: "definition",
|
|
686
|
+
message: `Block "${slug2}" child field cannot be named "${childName}".`,
|
|
687
|
+
data: {
|
|
688
|
+
fieldPath: `${unboundPath}.${slug2}.${childName}`,
|
|
689
|
+
fieldKind: "blocks",
|
|
690
|
+
reason: `reserved block child name "${childName}"`
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
if (blockDef.payload) {
|
|
697
|
+
for (const key of ["fields", "slug"]) {
|
|
698
|
+
if (Object.hasOwn(blockDef.payload, key)) {
|
|
699
|
+
throw new PayloadSchemaError(
|
|
700
|
+
"RESERVED_PAYLOAD_OPTION",
|
|
701
|
+
{
|
|
702
|
+
phase: "definition",
|
|
703
|
+
message: `Block "${slug2}" cannot configure reserved Payload option "${key}".`,
|
|
704
|
+
data: { fieldPath: `${unboundPath}.${slug2}`, fieldKind: "blocks", option: key }
|
|
705
|
+
}
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
const blockSeeds = [];
|
|
712
|
+
let firstBlocking;
|
|
713
|
+
for (const [slug2, blockDef] of blockEntries) {
|
|
714
|
+
const children = fieldEntries(blockDef.fields);
|
|
715
|
+
const blockingEntry = children.find(([, seed]) => !seed.schema);
|
|
716
|
+
const blockingChild = blockingEntry ? blockingEntry[1].blockingFieldPath ? `${blockingEntry[0]}.${blockingEntry[1].blockingFieldPath}` : blockingEntry[0] : void 0;
|
|
717
|
+
const blockingPath = blockingChild ? `${slug2}.${blockingChild}` : void 0;
|
|
718
|
+
if (blockingPath && !firstBlocking) {
|
|
719
|
+
firstBlocking = blockingPath;
|
|
720
|
+
}
|
|
721
|
+
blockSeeds.push({
|
|
722
|
+
slug: slug2,
|
|
723
|
+
payloadExtras: asPayloadExtras(blockDef.payload),
|
|
724
|
+
children,
|
|
725
|
+
blockingFieldPath: blockingPath
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
const schema = firstBlocking ? void 0 : buildBlocksSchema(
|
|
729
|
+
blockSeeds,
|
|
730
|
+
{
|
|
731
|
+
minRows: options.minRows,
|
|
732
|
+
maxRows: options.maxRows,
|
|
733
|
+
nullable: options.nullable
|
|
734
|
+
}
|
|
735
|
+
);
|
|
736
|
+
const slugs = blockEntries.map(([slug2]) => slug2);
|
|
737
|
+
const rowSchema = z4.looseObject({
|
|
738
|
+
blockType: z4.enum(slugs)
|
|
739
|
+
});
|
|
740
|
+
let container = z4.array(rowSchema);
|
|
741
|
+
if (options.minRows !== void 0) container = container.min(options.minRows);
|
|
742
|
+
if (options.maxRows !== void 0) container = container.max(options.maxRows);
|
|
743
|
+
const runtimeSchema = applyNullable(container, options.nullable);
|
|
744
|
+
return createFieldDefinition(withDefaults(
|
|
745
|
+
{
|
|
746
|
+
kind: "blocks",
|
|
747
|
+
payloadType: "blocks",
|
|
748
|
+
schema,
|
|
749
|
+
runtimeSchema,
|
|
750
|
+
payloadCore: {
|
|
751
|
+
...options.required === void 0 ? {} : { required: options.required },
|
|
752
|
+
...options.minRows === void 0 ? {} : { minRows: options.minRows },
|
|
753
|
+
...options.maxRows === void 0 ? {} : { maxRows: options.maxRows }
|
|
754
|
+
},
|
|
755
|
+
payloadExtras: asPayloadExtras(options.payload),
|
|
756
|
+
required: options.required ?? false,
|
|
757
|
+
nullable: options.nullable ?? false,
|
|
758
|
+
blocks: blockSeeds,
|
|
759
|
+
blockingFieldPath: firstBlocking,
|
|
760
|
+
minRows: options.minRows,
|
|
761
|
+
maxRows: options.maxRows
|
|
762
|
+
},
|
|
763
|
+
options
|
|
764
|
+
));
|
|
765
|
+
}
|
|
604
766
|
function richText(options) {
|
|
605
767
|
const schema = options.nullable ? options.schema.nullable() : options.schema;
|
|
606
768
|
return createFieldDefinition(withDefaults(
|
|
@@ -647,35 +809,11 @@ var field = Object.freeze({
|
|
|
647
809
|
upload,
|
|
648
810
|
group,
|
|
649
811
|
array,
|
|
812
|
+
blocks,
|
|
650
813
|
richText,
|
|
651
814
|
native
|
|
652
815
|
});
|
|
653
816
|
|
|
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
817
|
// src/entity/define-entity.ts
|
|
680
818
|
var reservedFieldNames = /* @__PURE__ */ new Set([
|
|
681
819
|
"__v",
|
|
@@ -683,26 +821,6 @@ var reservedFieldNames = /* @__PURE__ */ new Set([
|
|
|
683
821
|
"hash",
|
|
684
822
|
"salt"
|
|
685
823
|
]);
|
|
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
824
|
var commonReserved = [
|
|
707
825
|
"defaultValue",
|
|
708
826
|
"localized",
|
|
@@ -711,37 +829,6 @@ var commonReserved = [
|
|
|
711
829
|
"type",
|
|
712
830
|
"virtual"
|
|
713
831
|
];
|
|
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
832
|
function reservedKeys(seed) {
|
|
746
833
|
switch (seed.kind) {
|
|
747
834
|
case "array":
|
|
@@ -751,6 +838,13 @@ function reservedKeys(seed) {
|
|
|
751
838
|
"maxRows",
|
|
752
839
|
"minRows"
|
|
753
840
|
];
|
|
841
|
+
case "blocks":
|
|
842
|
+
return [
|
|
843
|
+
...commonReserved,
|
|
844
|
+
"blocks",
|
|
845
|
+
"maxRows",
|
|
846
|
+
"minRows"
|
|
847
|
+
];
|
|
754
848
|
case "group":
|
|
755
849
|
return [
|
|
756
850
|
...commonReserved,
|
|
@@ -889,13 +983,52 @@ function bindSeed(source, entity, fieldPath, relationshipIdSchema) {
|
|
|
889
983
|
child
|
|
890
984
|
]) => [
|
|
891
985
|
name,
|
|
892
|
-
child
|
|
986
|
+
compositeChildSchema(child)
|
|
893
987
|
]));
|
|
894
|
-
const objectSchema =
|
|
895
|
-
schema = source.composite === "array" ?
|
|
988
|
+
const objectSchema = z5.strictObject(shape);
|
|
989
|
+
schema = source.composite === "array" ? z5.array(objectSchema) : objectSchema;
|
|
896
990
|
if (source.nullable) schema = schema.nullable();
|
|
897
991
|
}
|
|
898
992
|
}
|
|
993
|
+
let boundBlocks;
|
|
994
|
+
if (source.blocks) {
|
|
995
|
+
boundBlocks = [];
|
|
996
|
+
let firstBlockingChild;
|
|
997
|
+
for (const block of source.blocks) {
|
|
998
|
+
const boundChildren = block.children.map(([
|
|
999
|
+
childName,
|
|
1000
|
+
childSeed
|
|
1001
|
+
]) => [
|
|
1002
|
+
childName,
|
|
1003
|
+
bindSeed(
|
|
1004
|
+
childSeed,
|
|
1005
|
+
entity,
|
|
1006
|
+
`${fieldPath}.${block.slug}.${childName}`,
|
|
1007
|
+
relationshipIdSchema
|
|
1008
|
+
)
|
|
1009
|
+
]);
|
|
1010
|
+
const blocking = boundChildren.find(([, child]) => !child.schema);
|
|
1011
|
+
const blockingPath = blocking ? blocking[1].blockingFieldPath ? blocking[1].blockingFieldPath.startsWith(`${fieldPath}.${block.slug}.${blocking[0]}`) ? blocking[1].blockingFieldPath : `${fieldPath}.${block.slug}.${blocking[0]}.${blocking[1].blockingFieldPath}` : `${fieldPath}.${block.slug}.${blocking[0]}` : void 0;
|
|
1012
|
+
if (blockingPath && !firstBlockingChild) {
|
|
1013
|
+
firstBlockingChild = blockingPath;
|
|
1014
|
+
}
|
|
1015
|
+
boundBlocks.push({
|
|
1016
|
+
slug: block.slug,
|
|
1017
|
+
payloadExtras: block.payloadExtras,
|
|
1018
|
+
children: boundChildren,
|
|
1019
|
+
blockingFieldPath: blockingPath
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
blockingFieldPath = firstBlockingChild;
|
|
1023
|
+
schema = firstBlockingChild ? void 0 : buildBlocksSchema(
|
|
1024
|
+
boundBlocks,
|
|
1025
|
+
{
|
|
1026
|
+
minRows: source.minRows,
|
|
1027
|
+
maxRows: source.maxRows,
|
|
1028
|
+
nullable: source.nullable
|
|
1029
|
+
}
|
|
1030
|
+
);
|
|
1031
|
+
}
|
|
899
1032
|
const payloadCore = { ...source.payloadCore };
|
|
900
1033
|
if (source.staticDefaultConfigured) {
|
|
901
1034
|
if (!schema) {
|
|
@@ -932,187 +1065,11 @@ function bindSeed(source, entity, fieldPath, relationshipIdSchema) {
|
|
|
932
1065
|
schema,
|
|
933
1066
|
runtimeSchema,
|
|
934
1067
|
children,
|
|
1068
|
+
blocks: boundBlocks ?? source.blocks,
|
|
935
1069
|
blockingFieldPath,
|
|
936
1070
|
payloadCore
|
|
937
1071
|
};
|
|
938
1072
|
}
|
|
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
1073
|
function assertFieldName(entity, name) {
|
|
1117
1074
|
if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(name)) {
|
|
1118
1075
|
throw new PayloadSchemaError(
|
|
@@ -1203,59 +1160,11 @@ function defineEntity(options) {
|
|
|
1203
1160
|
createFieldDefinition(seed)
|
|
1204
1161
|
])));
|
|
1205
1162
|
const fieldSchemas = schemaCapableFields(boundFields);
|
|
1206
|
-
const
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
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
|
-
});
|
|
1163
|
+
const getEntry = (key) => findEntityEntry(
|
|
1164
|
+
options.name,
|
|
1165
|
+
boundEntries,
|
|
1166
|
+
key
|
|
1167
|
+
);
|
|
1259
1168
|
const pick = ((keys, pickOptions) => {
|
|
1260
1169
|
const required = new Set(pickOptions?.required ?? []);
|
|
1261
1170
|
const optional = pickOptions?.optional;
|
|
@@ -1281,17 +1190,16 @@ function defineEntity(options) {
|
|
|
1281
1190
|
}
|
|
1282
1191
|
);
|
|
1283
1192
|
}
|
|
1284
|
-
const makeOptional = !required.has(key) && (optional === "all" || Array.isArray(optional) && optional.includes(key));
|
|
1193
|
+
const makeOptional = !required.has(key) && (optional === "all" || optional === "auto" && !seed.required || Array.isArray(optional) && optional.includes(key));
|
|
1285
1194
|
shape[name] = makeOptional ? seed.schema.optional() : seed.schema;
|
|
1286
1195
|
}
|
|
1287
|
-
return pickOptions?.strict === false ?
|
|
1196
|
+
return pickOptions?.strict === false ? z5.object(shape) : z5.strictObject(shape);
|
|
1288
1197
|
});
|
|
1289
|
-
const context = Object.freeze({ fields: fieldSchemas, pick, z:
|
|
1290
|
-
const entity =
|
|
1198
|
+
const context = Object.freeze({ fields: fieldSchemas, pick, z: z5 });
|
|
1199
|
+
const entity = {
|
|
1291
1200
|
name: options.name,
|
|
1292
1201
|
idSchema: options.idSchema ?? defaultIdSchema,
|
|
1293
1202
|
fields: boundFields,
|
|
1294
|
-
payload,
|
|
1295
1203
|
schema(factory) {
|
|
1296
1204
|
try {
|
|
1297
1205
|
const result = factory(context);
|
|
@@ -1323,8 +1231,12 @@ function defineEntity(options) {
|
|
|
1323
1231
|
options.name,
|
|
1324
1232
|
boundEntries
|
|
1325
1233
|
)
|
|
1326
|
-
}
|
|
1327
|
-
|
|
1234
|
+
};
|
|
1235
|
+
attachEntityBinding(
|
|
1236
|
+
entity,
|
|
1237
|
+
{ name: options.name, entries: boundEntries }
|
|
1238
|
+
);
|
|
1239
|
+
return Object.freeze(entity);
|
|
1328
1240
|
}
|
|
1329
1241
|
export {
|
|
1330
1242
|
PayloadSchemaError,
|