@conloca/astro-cms 0.2.1 → 0.2.3

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.
Files changed (101) hide show
  1. package/README.md +21 -19
  2. package/dist/internal/page-handler.astro +8 -2
  3. package/dist/ts/.build-inputs.json +88 -0
  4. package/dist/ts/node/content.d.ts +1 -2
  5. package/dist/ts/node/content.d.ts.map +1 -1
  6. package/dist/ts/node/content.js +1 -1
  7. package/dist/ts/node/content.js.map +1 -1
  8. package/dist/ts/node/index.d.ts +1 -1
  9. package/dist/ts/node/index.d.ts.map +1 -1
  10. package/dist/ts/node/index.js +3 -3
  11. package/dist/ts/node/index.js.map +1 -1
  12. package/dist/ts/src/collections.d.ts +31 -53
  13. package/dist/ts/src/collections.d.ts.map +1 -1
  14. package/dist/ts/src/collections.js +66 -71
  15. package/dist/ts/src/collections.js.map +1 -1
  16. package/dist/ts/src/editor-descriptors.d.ts +19 -5
  17. package/dist/ts/src/editor-descriptors.d.ts.map +1 -1
  18. package/dist/ts/src/editor-descriptors.js +26 -5
  19. package/dist/ts/src/editor-descriptors.js.map +1 -1
  20. package/dist/ts/src/entries.d.ts +44 -0
  21. package/dist/ts/src/entries.d.ts.map +1 -0
  22. package/dist/ts/src/entries.js +48 -0
  23. package/dist/ts/src/entries.js.map +1 -0
  24. package/dist/ts/src/handlers/collection-content-api.d.ts.map +1 -1
  25. package/dist/ts/src/handlers/collection-content-api.js +7 -2
  26. package/dist/ts/src/handlers/collection-content-api.js.map +1 -1
  27. package/dist/ts/src/handlers/collection-names.d.ts +9 -0
  28. package/dist/ts/src/handlers/collection-names.d.ts.map +1 -0
  29. package/dist/ts/src/handlers/collection-names.js +9 -0
  30. package/dist/ts/src/handlers/collection-names.js.map +1 -0
  31. package/dist/ts/src/handlers/page-render-core.d.ts +31 -12
  32. package/dist/ts/src/handlers/page-render-core.d.ts.map +1 -1
  33. package/dist/ts/src/handlers/page-render-core.js +38 -17
  34. package/dist/ts/src/handlers/page-render-core.js.map +1 -1
  35. package/dist/ts/src/index.d.ts +2 -2
  36. package/dist/ts/src/index.d.ts.map +1 -1
  37. package/dist/ts/src/index.js +1 -3
  38. package/dist/ts/src/index.js.map +1 -1
  39. package/dist/ts/src/internal/collection-parsers.d.ts +26 -0
  40. package/dist/ts/src/internal/collection-parsers.d.ts.map +1 -0
  41. package/dist/ts/src/internal/collection-parsers.js +34 -0
  42. package/dist/ts/src/internal/collection-parsers.js.map +1 -0
  43. package/dist/ts/src/internal/entry-issues.d.ts +8 -0
  44. package/dist/ts/src/internal/entry-issues.d.ts.map +1 -0
  45. package/dist/ts/src/internal/entry-issues.js +19 -0
  46. package/dist/ts/src/internal/entry-issues.js.map +1 -0
  47. package/dist/ts/src/internal/entry-validation.d.ts +93 -0
  48. package/dist/ts/src/internal/entry-validation.d.ts.map +1 -0
  49. package/dist/ts/src/internal/entry-validation.js +228 -0
  50. package/dist/ts/src/internal/entry-validation.js.map +1 -0
  51. package/dist/ts/src/lib/virtual-module-generators.d.ts +13 -0
  52. package/dist/ts/src/lib/virtual-module-generators.d.ts.map +1 -1
  53. package/dist/ts/src/lib/virtual-module-generators.js +18 -0
  54. package/dist/ts/src/lib/virtual-module-generators.js.map +1 -1
  55. package/dist/ts/src/loader.d.ts +9 -12
  56. package/dist/ts/src/loader.d.ts.map +1 -1
  57. package/dist/ts/src/loader.js +19 -16
  58. package/dist/ts/src/loader.js.map +1 -1
  59. package/dist/ts/src/page-schemas.d.ts +1 -1
  60. package/dist/ts/src/page-schemas.d.ts.map +1 -1
  61. package/dist/ts/src/page-schemas.js +1 -1
  62. package/dist/ts/src/page-schemas.js.map +1 -1
  63. package/dist/ts/src/plugin-spa.d.ts +12 -0
  64. package/dist/ts/src/plugin-spa.d.ts.map +1 -1
  65. package/dist/ts/src/plugin-spa.js +39 -14
  66. package/dist/ts/src/plugin-spa.js.map +1 -1
  67. package/dist/ts/src/sandbox-frame-handler.d.ts +4 -1
  68. package/dist/ts/src/sandbox-frame-handler.d.ts.map +1 -1
  69. package/dist/ts/src/sandbox-frame-handler.js +22 -11
  70. package/dist/ts/src/sandbox-frame-handler.js.map +1 -1
  71. package/dist/ts/src/site-styles/content-wrapper-endpoint.d.ts +3 -62
  72. package/dist/ts/src/site-styles/content-wrapper-endpoint.d.ts.map +1 -1
  73. package/dist/ts/src/site-styles/content-wrapper-endpoint.js +12 -193
  74. package/dist/ts/src/site-styles/content-wrapper-endpoint.js.map +1 -1
  75. package/dist/virtual-modules.d.ts +14 -0
  76. package/node/content.ts +10 -1
  77. package/node/index.ts +7 -7
  78. package/package.json +12 -9
  79. package/src/collections.ts +107 -76
  80. package/src/editor-descriptors.ts +43 -5
  81. package/src/entries.ts +111 -0
  82. package/src/handlers/collection-content-api.ts +7 -2
  83. package/src/handlers/collection-names.ts +8 -0
  84. package/src/handlers/page-handler.astro +8 -2
  85. package/src/handlers/page-render-core.ts +65 -17
  86. package/src/index.ts +6 -7
  87. package/src/internal/collection-parsers.ts +51 -0
  88. package/src/internal/entry-issues.ts +23 -0
  89. package/src/internal/entry-validation.ts +305 -0
  90. package/src/lib/virtual-module-generators.ts +20 -0
  91. package/src/loader.ts +33 -17
  92. package/src/page-schemas.ts +4 -0
  93. package/src/plugin-spa.ts +58 -13
  94. package/src/sandbox-frame-handler.ts +16 -5
  95. package/src/site-styles/content-wrapper-endpoint.ts +4 -285
  96. package/src/virtual-modules.d.ts +14 -0
  97. package/dist/ts/src/internal/page-collection-schema.d.ts +0 -44
  98. package/dist/ts/src/internal/page-collection-schema.d.ts.map +0 -1
  99. package/dist/ts/src/internal/page-collection-schema.js +0 -152
  100. package/dist/ts/src/internal/page-collection-schema.js.map +0 -1
  101. package/src/internal/page-collection-schema.ts +0 -210
@@ -1,152 +0,0 @@
1
- import { toPageSchemaDescriptor } from '@conloca/cms-spa/page-schema-define';
2
- import { pageMetaSchema } from '@conloca/content-api/schemas';
3
- import { z } from 'zod';
4
- import { LOADER_RESERVED_FIELDS } from '../loader.js';
5
- const CORE_FIELD_NAMES = new Set(Object.keys(pageMetaSchema.shape));
6
- const RESERVED_FIELD_NAMES = new Set(LOADER_RESERVED_FIELDS);
7
- /**
8
- * `seen` breaks cycles through shared schema instances, but a z.lazy whose
9
- * factory builds fresh instances on every call defeats it — the depth cap is
10
- * the backstop. No real page-meta schema nests this deep.
11
- */
12
- const MAX_REWRITE_WALK_DEPTH = 32;
13
- function findRewrite(schema, path, seen, depth = 0) {
14
- if (seen.has(schema) || depth > MAX_REWRITE_WALK_DEPTH)
15
- return null;
16
- seen.add(schema);
17
- const def = schema.def;
18
- if (def.type === 'default')
19
- return { kind: 'default', path };
20
- if (def.type === 'catch')
21
- return { kind: 'catch', path };
22
- if (def.type === 'pipe')
23
- return { kind: 'transform', path };
24
- if (def.coerce)
25
- return { kind: 'coerce', path };
26
- // Every child schema paired with the path it is reported under, in the
27
- // order the walk visits them — the first rewrite found is the one named
28
- // in the error, so this order is part of the contract.
29
- const shape = typeof def.shape === 'function' ? def.shape() : def.shape;
30
- const children = [[def.innerType ?? def.in, path]];
31
- for (const [key, child] of Object.entries(shape ?? {})) {
32
- children.push([child, path ? `${path}.${key}` : key]);
33
- }
34
- children.push([def.element, `${path}[]`]);
35
- for (const option of def.options ?? []) {
36
- children.push([option, path]);
37
- }
38
- for (const [index, item] of (def.items ?? []).entries()) {
39
- children.push([item, `${path}[${index}]`]);
40
- }
41
- children.push([def.rest, `${path}[]`], [def.keyType, path], [def.valueType, path], [def.left, path], [def.right, path], [def.getter?.(), path], [def.catchall, `${path}[*]`]);
42
- for (const [child, childPath] of children) {
43
- if (!child)
44
- continue;
45
- const found = findRewrite(child, childPath, seen, depth + 1);
46
- if (found)
47
- return found;
48
- }
49
- return null;
50
- }
51
- function assertHostFieldsAreSafe(shape) {
52
- for (const [name, schema] of Object.entries(shape)) {
53
- if (CORE_FIELD_NAMES.has(name)) {
54
- throw new Error(`[conloca] pageSchemas cannot redeclare Conloca field "${name}". ` +
55
- 'Title, pathname, locale, and the other core fields come from the CMS — put constraints on them in the editor schema, not here.');
56
- }
57
- if (RESERVED_FIELD_NAMES.has(name)) {
58
- throw new Error(`[conloca] pageSchemas cannot declare "${name}". ` +
59
- 'That field is owned by the content loader; a host schema of the same name would replace page content on every page.');
60
- }
61
- const rewrite = findRewrite(schema, name, new Set());
62
- if (rewrite) {
63
- throw new Error(`[conloca] pageSchemas field "${rewrite.path}" uses .${rewrite.kind}(), which rewrites values when Astro validates the collection ` +
64
- 'and does not run in the editor preview. Use a plain field (optional string, array, …) and parse in one reader instead.');
65
- }
66
- }
67
- }
68
- /**
69
- * Fold every page-schema descriptor into one Zod object of host meta fields.
70
- * Requiredness is not preserved — the caller always `.partial()`s this for the
71
- * collection, because Astro validates the whole `pages` pile.
72
- *
73
- * The merged object has one slot per field name, so two descriptors may only
74
- * declare the same name by sharing the same field schema instance — last-wins
75
- * would silently rewrite what the earlier prefix's pages are validated
76
- * against, failing them with a Zod error that names neither descriptor.
77
- */
78
- export function hostFieldsFromPageSchemas(pageSchemas) {
79
- // z.ZodRawShape's index signature is readonly, so the accumulator is spelled
80
- // with a mutable one.
81
- const shape = {};
82
- const fieldOwner = new Map();
83
- for (const [key, entry] of Object.entries(pageSchemas)) {
84
- const descriptor = toPageSchemaDescriptor(entry);
85
- // Rebuilding from .shape below silently drops object-level refinements,
86
- // so the refusal written for them has to run here, on the public path.
87
- assertNoObjectRefinements(descriptor.schema);
88
- // 'minimal'/'none' descriptors declare title for the form; strip those names so the collection guard does not fire.
89
- const mode = descriptor.coreFields?.mode;
90
- const stripCore = mode === 'minimal' || mode === 'none';
91
- for (const [name, field] of Object.entries(descriptor.schema.shape)) {
92
- if (stripCore && CORE_FIELD_NAMES.has(name))
93
- continue;
94
- const owner = fieldOwner.get(name);
95
- if (owner !== undefined && shape[name] !== field) {
96
- throw new Error(`[conloca] pageSchemas "${owner}" and "${key}" both declare field "${name}" with different schemas. ` +
97
- 'The merged collection schema has one slot per field name — declare the field once and import it ' +
98
- 'into both schemas, or rename one of the fields.');
99
- }
100
- fieldOwner.set(name, owner ?? key);
101
- shape[name] = field;
102
- }
103
- }
104
- return z.object(shape);
105
- }
106
- /**
107
- * The schema Astro validates page entries against: the host's page-meta fields
108
- * (all optional) with conloca's structural fields layered over them, loose so
109
- * undeclared top-level fields still reach the rendered page.
110
- *
111
- * Looseness is TOP-LEVEL ONLY. A nested `z.object()` still strips undeclared
112
- * nested keys; a host that wants them preserved has to write `z.looseObject()`.
113
- *
114
- * Rewrite bans (.default / .coerce / .transform / .catch) walk nested objects,
115
- * arrays, and unions. `.partial()` is still shallow.
116
- *
117
- * Internal on purpose: `src/collections.ts` is a published entry (the
118
- * `./collections` subpath), so anything exported from there is frozen under
119
- * semver. Nothing outside this package consumes it.
120
- */
121
- export function buildPageCollectionSchema(hostFields) {
122
- assertNoObjectRefinements(hostFields);
123
- assertHostFieldsAreSafe(hostFields.shape);
124
- // The one declared seam in the typed-collections design: the runtime
125
- // schema validates core + host fields and loosely admits the loader-written
126
- // record fields, but TypeScript cannot see host fields through the loose
127
- // index. The static type is asserted as the flattened entry instead, which
128
- // is the same contract listings already rely on — a host declaring
129
- // `HostPageMeta` promises it matches its `pageSchemas`. Pinned by the type
130
- // proofs here and in the type proofs of a site that declares `HostPageMeta`.
131
- return z.looseObject({
132
- ...hostFields.partial().shape,
133
- ...pageMetaSchema.shape,
134
- });
135
- }
136
- /**
137
- * .partial() throws on a refined object, and a refined object still satisfies
138
- * both the host-fields signature and the pageSchemas field type, so the raw
139
- * zod error would surface while Astro loads content.config.ts naming neither
140
- * conloca nor pageSchemas. Refuse it with something the host can act on —
141
- * both on the fold over pageSchemas (which would otherwise silently DROP the
142
- * refinement while rebuilding from .shape) and on direct host-fields callers.
143
- */
144
- function assertNoObjectRefinements(schema) {
145
- if ((schema.def.checks?.length ?? 0) > 0) {
146
- throw new Error('[conloca] pageSchemas cannot carry object-level refinements (.refine, .superRefine or .check). ' +
147
- 'Conloca makes every host field optional — page schemas are prefix-scoped in the editor while ' +
148
- 'Astro validates a whole collection — and Zod cannot make a refined object partial. ' +
149
- 'Move the rule onto the individual field schemas instead.');
150
- }
151
- }
152
- //# sourceMappingURL=page-collection-schema.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"page-collection-schema.js","sourceRoot":"","sources":["../../../../src/internal/page-collection-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAE/F,OAAO,EAAiB,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC7E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAEtD,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;AACpE,MAAM,oBAAoB,GAAG,IAAI,GAAG,CAAS,sBAAsB,CAAC,CAAC;AAsBrE;;;;GAIG;AACH,MAAM,sBAAsB,GAAG,EAAE,CAAC;AAElC,SAAS,WAAW,CAClB,MAAiB,EACjB,IAAY,EACZ,IAAoB,EACpB,KAAK,GAAG,CAAC;IAET,IAAI,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,KAAK,GAAG,sBAAsB;QAAE,OAAO,IAAI,CAAC;IACpE,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACjB,MAAM,GAAG,GAAG,MAAM,CAAC,GAAa,CAAC;IACjC,IAAI,GAAG,CAAC,IAAI,KAAK,SAAS;QAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IAC7D,IAAI,GAAG,CAAC,IAAI,KAAK,OAAO;QAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IACzD,IAAI,GAAG,CAAC,IAAI,KAAK,MAAM;QAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;IAC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nC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;QACtB,CAAC;IACH,CAAC;IACD,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACzB,CAAC;AAgBD;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,yBAAyB,CAAC,UAAsC;IAC9E,yBAAyB,CAAC,UAAU,CAAC,CAAC;IACtC,uBAAuB,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IAC1C,qEAAqE;IACrE,4EAA4E;IAC5E,yEAAyE;IACzE,2EAA2E;IAC3E,mEAAmE;IACnE,2EAA2E;IAC3E,6EAA6E;IAC7E,OAAO,CAAC,CAAC,WAAW,CAAC;QACnB,GAAG,UAAU,CAAC,OAAO,EAAE,CAAC,KAAK;QAC7B,GAAG,cAAc,CAAC,KAAK;KACxB,CAA2C,CAAC;AAC/C,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,yBAAyB,CAAC,MAAkC;IACnE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CACb,iGAAiG;YAC/F,+FAA+F;YAC/F,qFAAqF;YACrF,0DAA0D,CAC7D,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -1,210 +0,0 @@
1
- import { type PageSchemas, toPageSchemaDescriptor } from '@conloca/cms-spa/page-schema-define';
2
- import type { ContentMeta } from '@conloca/content-api';
3
- import { type PageMeta, pageMetaSchema } from '@conloca/content-api/schemas';
4
- import { z } from 'zod';
5
- import { LOADER_RESERVED_FIELDS } from '../loader.js';
6
-
7
- const CORE_FIELD_NAMES = new Set(Object.keys(pageMetaSchema.shape));
8
- const RESERVED_FIELD_NAMES = new Set<string>(LOADER_RESERVED_FIELDS);
9
-
10
- type RewriteKind = 'default' | 'catch' | 'transform' | 'coerce';
11
-
12
- type ZodDef = {
13
- type?: string;
14
- coerce?: boolean;
15
- innerType?: z.ZodType;
16
- in?: z.ZodType;
17
- shape?: Record<string, z.ZodType> | (() => Record<string, z.ZodType>);
18
- element?: z.ZodType;
19
- options?: z.ZodType[];
20
- items?: z.ZodType[];
21
- valueType?: z.ZodType;
22
- keyType?: z.ZodType;
23
- rest?: z.ZodType;
24
- left?: z.ZodType;
25
- right?: z.ZodType;
26
- getter?: () => z.ZodType;
27
- catchall?: z.ZodType;
28
- };
29
-
30
- /**
31
- * `seen` breaks cycles through shared schema instances, but a z.lazy whose
32
- * factory builds fresh instances on every call defeats it — the depth cap is
33
- * the backstop. No real page-meta schema nests this deep.
34
- */
35
- const MAX_REWRITE_WALK_DEPTH = 32;
36
-
37
- function findRewrite(
38
- schema: z.ZodType,
39
- path: string,
40
- seen: Set<z.ZodType>,
41
- depth = 0,
42
- ): { kind: RewriteKind; path: string } | null {
43
- if (seen.has(schema) || depth > MAX_REWRITE_WALK_DEPTH) return null;
44
- seen.add(schema);
45
- const def = schema.def as ZodDef;
46
- if (def.type === 'default') return { kind: 'default', path };
47
- if (def.type === 'catch') return { kind: 'catch', path };
48
- if (def.type === 'pipe') return { kind: 'transform', path };
49
- if (def.coerce) return { kind: 'coerce', path };
50
-
51
- // Every child schema paired with the path it is reported under, in the
52
- // order the walk visits them — the first rewrite found is the one named
53
- // in the error, so this order is part of the contract.
54
- const shape = typeof def.shape === 'function' ? def.shape() : def.shape;
55
- const children: [z.ZodType | undefined, string][] = [[def.innerType ?? def.in, path]];
56
- for (const [key, child] of Object.entries(shape ?? {})) {
57
- children.push([child, path ? `${path}.${key}` : key]);
58
- }
59
- children.push([def.element, `${path}[]`]);
60
- for (const option of def.options ?? []) {
61
- children.push([option, path]);
62
- }
63
- for (const [index, item] of (def.items ?? []).entries()) {
64
- children.push([item, `${path}[${index}]`]);
65
- }
66
- children.push(
67
- [def.rest, `${path}[]`],
68
- [def.keyType, path],
69
- [def.valueType, path],
70
- [def.left, path],
71
- [def.right, path],
72
- [def.getter?.(), path],
73
- [def.catchall, `${path}[*]`],
74
- );
75
-
76
- for (const [child, childPath] of children) {
77
- if (!child) continue;
78
- const found = findRewrite(child, childPath, seen, depth + 1);
79
- if (found) return found;
80
- }
81
- return null;
82
- }
83
-
84
- function assertHostFieldsAreSafe(shape: z.ZodRawShape): void {
85
- for (const [name, schema] of Object.entries(shape)) {
86
- if (CORE_FIELD_NAMES.has(name)) {
87
- throw new Error(
88
- `[conloca] pageSchemas cannot redeclare Conloca field "${name}". ` +
89
- 'Title, pathname, locale, and the other core fields come from the CMS — put constraints on them in the editor schema, not here.',
90
- );
91
- }
92
- if (RESERVED_FIELD_NAMES.has(name)) {
93
- throw new Error(
94
- `[conloca] pageSchemas cannot declare "${name}". ` +
95
- 'That field is owned by the content loader; a host schema of the same name would replace page content on every page.',
96
- );
97
- }
98
- const rewrite = findRewrite(schema as z.ZodType, name, new Set());
99
- if (rewrite) {
100
- throw new Error(
101
- `[conloca] pageSchemas field "${rewrite.path}" uses .${rewrite.kind}(), which rewrites values when Astro validates the collection ` +
102
- 'and does not run in the editor preview. Use a plain field (optional string, array, …) and parse in one reader instead.',
103
- );
104
- }
105
- }
106
- }
107
-
108
- /**
109
- * Fold every page-schema descriptor into one Zod object of host meta fields.
110
- * Requiredness is not preserved — the caller always `.partial()`s this for the
111
- * collection, because Astro validates the whole `pages` pile.
112
- *
113
- * The merged object has one slot per field name, so two descriptors may only
114
- * declare the same name by sharing the same field schema instance — last-wins
115
- * would silently rewrite what the earlier prefix's pages are validated
116
- * against, failing them with a Zod error that names neither descriptor.
117
- */
118
- export function hostFieldsFromPageSchemas(pageSchemas: PageSchemas): z.ZodObject<z.ZodRawShape> {
119
- // z.ZodRawShape's index signature is readonly, so the accumulator is spelled
120
- // with a mutable one.
121
- const shape: Record<string, z.ZodRawShape[string]> = {};
122
- const fieldOwner = new Map<string, string>();
123
- for (const [key, entry] of Object.entries(pageSchemas)) {
124
- const descriptor = toPageSchemaDescriptor(entry);
125
- // Rebuilding from .shape below silently drops object-level refinements,
126
- // so the refusal written for them has to run here, on the public path.
127
- assertNoObjectRefinements(descriptor.schema);
128
- // 'minimal'/'none' descriptors declare title for the form; strip those names so the collection guard does not fire.
129
- const mode = descriptor.coreFields?.mode;
130
- const stripCore = mode === 'minimal' || mode === 'none';
131
- for (const [name, field] of Object.entries(descriptor.schema.shape)) {
132
- if (stripCore && CORE_FIELD_NAMES.has(name)) continue;
133
- const owner = fieldOwner.get(name);
134
- if (owner !== undefined && shape[name] !== field) {
135
- throw new Error(
136
- `[conloca] pageSchemas "${owner}" and "${key}" both declare field "${name}" with different schemas. ` +
137
- 'The merged collection schema has one slot per field name — declare the field once and import it ' +
138
- 'into both schemas, or rename one of the fields.',
139
- );
140
- }
141
- fieldOwner.set(name, owner ?? key);
142
- shape[name] = field;
143
- }
144
- }
145
- return z.object(shape);
146
- }
147
-
148
- /**
149
- * A page collection entry as the loader delivers it: the validated
150
- * `pageMetaSchema` fields, the loader-written record fields (`created`,
151
- * `modified`, `name`, `puckData` — admitted by the loose index at runtime),
152
- * and the host's meta fields through `ContentMeta`/`HostPageMeta`.
153
- */
154
- export type ConlocaPageEntry = ContentMeta &
155
- PageMeta & {
156
- created?: string | Date;
157
- modified?: string | Date;
158
- name?: string;
159
- puckData?: unknown;
160
- };
161
-
162
- /**
163
- * The schema Astro validates page entries against: the host's page-meta fields
164
- * (all optional) with conloca's structural fields layered over them, loose so
165
- * undeclared top-level fields still reach the rendered page.
166
- *
167
- * Looseness is TOP-LEVEL ONLY. A nested `z.object()` still strips undeclared
168
- * nested keys; a host that wants them preserved has to write `z.looseObject()`.
169
- *
170
- * Rewrite bans (.default / .coerce / .transform / .catch) walk nested objects,
171
- * arrays, and unions. `.partial()` is still shallow.
172
- *
173
- * Internal on purpose: `src/collections.ts` is a published entry (the
174
- * `./collections` subpath), so anything exported from there is frozen under
175
- * semver. Nothing outside this package consumes it.
176
- */
177
- export function buildPageCollectionSchema(hostFields: z.ZodObject<z.ZodRawShape>): z.ZodType<ConlocaPageEntry> {
178
- assertNoObjectRefinements(hostFields);
179
- assertHostFieldsAreSafe(hostFields.shape);
180
- // The one declared seam in the typed-collections design: the runtime
181
- // schema validates core + host fields and loosely admits the loader-written
182
- // record fields, but TypeScript cannot see host fields through the loose
183
- // index. The static type is asserted as the flattened entry instead, which
184
- // is the same contract listings already rely on — a host declaring
185
- // `HostPageMeta` promises it matches its `pageSchemas`. Pinned by the type
186
- // proofs here and in the type proofs of a site that declares `HostPageMeta`.
187
- return z.looseObject({
188
- ...hostFields.partial().shape,
189
- ...pageMetaSchema.shape,
190
- }) as unknown as z.ZodType<ConlocaPageEntry>;
191
- }
192
-
193
- /**
194
- * .partial() throws on a refined object, and a refined object still satisfies
195
- * both the host-fields signature and the pageSchemas field type, so the raw
196
- * zod error would surface while Astro loads content.config.ts naming neither
197
- * conloca nor pageSchemas. Refuse it with something the host can act on —
198
- * both on the fold over pageSchemas (which would otherwise silently DROP the
199
- * refinement while rebuilding from .shape) and on direct host-fields callers.
200
- */
201
- function assertNoObjectRefinements(schema: z.ZodObject<z.ZodRawShape>): void {
202
- if ((schema.def.checks?.length ?? 0) > 0) {
203
- throw new Error(
204
- '[conloca] pageSchemas cannot carry object-level refinements (.refine, .superRefine or .check). ' +
205
- 'Conloca makes every host field optional — page schemas are prefix-scoped in the editor while ' +
206
- 'Astro validates a whole collection — and Zod cannot make a refined object partial. ' +
207
- 'Move the rule onto the individual field schemas instead.',
208
- );
209
- }
210
- }