@conloca/astro-cms 0.2.1 → 0.2.2
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/README.md +21 -19
- package/dist/internal/page-handler.astro +8 -2
- package/dist/ts/node/content.d.ts +1 -2
- package/dist/ts/node/content.d.ts.map +1 -1
- package/dist/ts/node/content.js +1 -1
- package/dist/ts/node/content.js.map +1 -1
- package/dist/ts/src/collections.d.ts +31 -53
- package/dist/ts/src/collections.d.ts.map +1 -1
- package/dist/ts/src/collections.js +66 -71
- package/dist/ts/src/collections.js.map +1 -1
- package/dist/ts/src/entries.d.ts +44 -0
- package/dist/ts/src/entries.d.ts.map +1 -0
- package/dist/ts/src/entries.js +48 -0
- package/dist/ts/src/entries.js.map +1 -0
- package/dist/ts/src/handlers/collection-content-api.d.ts.map +1 -1
- package/dist/ts/src/handlers/collection-content-api.js +7 -2
- package/dist/ts/src/handlers/collection-content-api.js.map +1 -1
- package/dist/ts/src/handlers/collection-names.d.ts +9 -0
- package/dist/ts/src/handlers/collection-names.d.ts.map +1 -0
- package/dist/ts/src/handlers/collection-names.js +9 -0
- package/dist/ts/src/handlers/collection-names.js.map +1 -0
- package/dist/ts/src/handlers/page-render-core.d.ts +31 -12
- package/dist/ts/src/handlers/page-render-core.d.ts.map +1 -1
- package/dist/ts/src/handlers/page-render-core.js +38 -17
- package/dist/ts/src/handlers/page-render-core.js.map +1 -1
- package/dist/ts/src/index.d.ts +2 -2
- package/dist/ts/src/index.d.ts.map +1 -1
- package/dist/ts/src/index.js +1 -3
- package/dist/ts/src/index.js.map +1 -1
- package/dist/ts/src/internal/collection-parsers.d.ts +26 -0
- package/dist/ts/src/internal/collection-parsers.d.ts.map +1 -0
- package/dist/ts/src/internal/collection-parsers.js +34 -0
- package/dist/ts/src/internal/collection-parsers.js.map +1 -0
- package/dist/ts/src/internal/entry-issues.d.ts +8 -0
- package/dist/ts/src/internal/entry-issues.d.ts.map +1 -0
- package/dist/ts/src/internal/entry-issues.js +19 -0
- package/dist/ts/src/internal/entry-issues.js.map +1 -0
- package/dist/ts/src/internal/entry-validation.d.ts +93 -0
- package/dist/ts/src/internal/entry-validation.d.ts.map +1 -0
- package/dist/ts/src/internal/entry-validation.js +228 -0
- package/dist/ts/src/internal/entry-validation.js.map +1 -0
- package/dist/ts/src/lib/virtual-module-generators.d.ts +13 -0
- package/dist/ts/src/lib/virtual-module-generators.d.ts.map +1 -1
- package/dist/ts/src/lib/virtual-module-generators.js +18 -0
- package/dist/ts/src/lib/virtual-module-generators.js.map +1 -1
- package/dist/ts/src/loader.d.ts +9 -12
- package/dist/ts/src/loader.d.ts.map +1 -1
- package/dist/ts/src/loader.js +19 -16
- package/dist/ts/src/loader.js.map +1 -1
- package/dist/ts/src/page-schemas.d.ts +1 -1
- package/dist/ts/src/page-schemas.d.ts.map +1 -1
- package/dist/ts/src/page-schemas.js +1 -1
- package/dist/ts/src/page-schemas.js.map +1 -1
- package/dist/ts/src/plugin-spa.d.ts +12 -0
- package/dist/ts/src/plugin-spa.d.ts.map +1 -1
- package/dist/ts/src/plugin-spa.js +9 -2
- package/dist/ts/src/plugin-spa.js.map +1 -1
- package/dist/virtual-modules.d.ts +14 -0
- package/node/content.ts +10 -1
- package/package.json +12 -9
- package/src/collections.ts +107 -76
- package/src/entries.ts +111 -0
- package/src/handlers/collection-content-api.ts +7 -2
- package/src/handlers/collection-names.ts +8 -0
- package/src/handlers/page-handler.astro +8 -2
- package/src/handlers/page-render-core.ts +65 -17
- package/src/index.ts +6 -7
- package/src/internal/collection-parsers.ts +51 -0
- package/src/internal/entry-issues.ts +23 -0
- package/src/internal/entry-validation.ts +305 -0
- package/src/lib/virtual-module-generators.ts +20 -0
- package/src/loader.ts +33 -17
- package/src/page-schemas.ts +4 -0
- package/src/plugin-spa.ts +24 -1
- package/src/virtual-modules.d.ts +14 -0
- package/dist/ts/src/internal/page-collection-schema.d.ts +0 -44
- package/dist/ts/src/internal/page-collection-schema.d.ts.map +0 -1
- package/dist/ts/src/internal/page-collection-schema.js +0 -152
- package/dist/ts/src/internal/page-collection-schema.js.map +0 -1
- package/src/internal/page-collection-schema.ts +0 -210
package/src/loader.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { createContentAPI } from '@conloca/content-api/reader';
|
|
2
2
|
import type { Loader, LoaderContext } from 'astro/loaders';
|
|
3
|
+
import { describeEntryIssues } from './internal/entry-issues.js';
|
|
4
|
+
import type { EntryParser } from './internal/entry-validation.js';
|
|
3
5
|
|
|
4
6
|
export interface ConlocaLoaderOptions {
|
|
5
7
|
/** Must equal the content.config key this loader is mounted under — load() asserts it. */
|
|
@@ -8,21 +10,16 @@ export interface ConlocaLoaderOptions {
|
|
|
8
10
|
site?: string;
|
|
9
11
|
/** Collection kind. If not provided, defaults to 'fragment' for 'fragments' collection, 'page' otherwise. */
|
|
10
12
|
kind?: 'page' | 'fragment' | 'data';
|
|
13
|
+
/**
|
|
14
|
+
* Validates every entry before it is stored; a failing entry fails the load
|
|
15
|
+
* with its file and every issue. The collection factories pass the generated
|
|
16
|
+
* Typia parsers for the collection's kind and host schemas. Without it, the
|
|
17
|
+
* entry is stored as loaded (and whatever `schema` the collection was
|
|
18
|
+
* declared with still runs through `parseData`).
|
|
19
|
+
*/
|
|
20
|
+
parseEntry?: EntryParser<Record<string, unknown>>;
|
|
11
21
|
}
|
|
12
22
|
|
|
13
|
-
/**
|
|
14
|
-
* Entry fields this loader owns, written into every entry's data in load().
|
|
15
|
-
*
|
|
16
|
-
* None of them belong to `pageMetaSchema`, so under a page collection's schema
|
|
17
|
-
* they survive only through the loose index signature. A host page-meta field
|
|
18
|
-
* of the same name would replace the loader's value — a host `puckData` shape
|
|
19
|
-
* would empty the page tree of every page in the built site.
|
|
20
|
-
* `buildPageCollectionSchema` refuses those names with an error.
|
|
21
|
-
*
|
|
22
|
-
* Adding a field to the record built in load() also means reserving it here.
|
|
23
|
-
*/
|
|
24
|
-
export const LOADER_RESERVED_FIELDS = ['created', 'modified', 'name', 'data', 'mdx', 'puckData'] as const;
|
|
25
|
-
|
|
26
23
|
/**
|
|
27
24
|
* Custom Astro content loader for Conloca CMS.
|
|
28
25
|
* Loads content from the Conloca content directory into Astro collections.
|
|
@@ -97,9 +94,9 @@ export function conlocaLoader(options: ConlocaLoaderOptions): Loader {
|
|
|
97
94
|
}
|
|
98
95
|
}
|
|
99
96
|
|
|
100
|
-
// Any key added here that is not part of
|
|
101
|
-
// listed in LOADER_RESERVED_FIELDS
|
|
102
|
-
// same name will replace it on every page.
|
|
97
|
+
// Any key added here that is not part of PageMeta must also be
|
|
98
|
+
// listed in LOADER_RESERVED_FIELDS (internal/entry-validation.ts),
|
|
99
|
+
// or a host page-meta field of the same name will replace it on every page.
|
|
103
100
|
const data: Record<string, unknown> = {
|
|
104
101
|
id: manifest.id,
|
|
105
102
|
locale,
|
|
@@ -123,11 +120,19 @@ export function conlocaLoader(options: ConlocaLoaderOptions): Loader {
|
|
|
123
120
|
data.puckData = puckData;
|
|
124
121
|
}
|
|
125
122
|
|
|
123
|
+
const validatedData = options.parseEntry
|
|
124
|
+
? validEntryData(options.parseEntry, data, {
|
|
125
|
+
collection: context.collection,
|
|
126
|
+
id: entryId,
|
|
127
|
+
filePath: localeVersion.sourcePath,
|
|
128
|
+
})
|
|
129
|
+
: data;
|
|
130
|
+
|
|
126
131
|
// filePath feeds Astro's error location: without it a validation
|
|
127
132
|
// failure names only the opaque content id, not a file to open.
|
|
128
133
|
const parsedData = await parseData({
|
|
129
134
|
id: entryId,
|
|
130
|
-
data,
|
|
135
|
+
data: validatedData,
|
|
131
136
|
...(localeVersion.sourcePath ? { filePath: localeVersion.sourcePath } : {}),
|
|
132
137
|
});
|
|
133
138
|
|
|
@@ -143,3 +148,14 @@ export function conlocaLoader(options: ConlocaLoaderOptions): Loader {
|
|
|
143
148
|
},
|
|
144
149
|
};
|
|
145
150
|
}
|
|
151
|
+
|
|
152
|
+
/** The parsed entry data, or the build error naming the entry, its file and every issue. */
|
|
153
|
+
function validEntryData(
|
|
154
|
+
parseEntry: EntryParser<Record<string, unknown>>,
|
|
155
|
+
data: Record<string, unknown>,
|
|
156
|
+
where: { collection: string; id: string; filePath?: string },
|
|
157
|
+
): Record<string, unknown> {
|
|
158
|
+
const result = parseEntry(data);
|
|
159
|
+
if (!result.success) throw new Error(describeEntryIssues(result.issues, where));
|
|
160
|
+
return result.data;
|
|
161
|
+
}
|
package/src/page-schemas.ts
CHANGED
|
@@ -4,11 +4,15 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export {
|
|
6
6
|
type CoreFieldsMode,
|
|
7
|
+
type DataSchemas,
|
|
8
|
+
defineHostSchema,
|
|
7
9
|
definePageSchema,
|
|
8
10
|
type FieldHint,
|
|
9
11
|
type FieldHintCommon,
|
|
10
12
|
type FieldHints,
|
|
11
13
|
type FieldHintVariant,
|
|
14
|
+
type HostSchema,
|
|
15
|
+
type HostSchemaType,
|
|
12
16
|
type PageSchemaDescriptor,
|
|
13
17
|
type PageSchemaEntry,
|
|
14
18
|
type PageSchemaGroup,
|
package/src/plugin-spa.ts
CHANGED
|
@@ -55,6 +55,7 @@ const ACORN_DEFAULT_SHIM_PATH = join(__dirname, '..', '..', 'internal', 'acorn-d
|
|
|
55
55
|
const TIPTAP_HTML_SHIM_PATH = join(__dirname, 'tiptap-html-static.js');
|
|
56
56
|
|
|
57
57
|
import {
|
|
58
|
+
generateContentSchemasModule,
|
|
58
59
|
generateLayoutModule,
|
|
59
60
|
generatePuckConfigModule,
|
|
60
61
|
generateRoutingConfigModule,
|
|
@@ -69,11 +70,13 @@ const RESOLVED_VIRTUAL_CONFIG = `\0${VIRTUAL_CONFIG_MODULE}`;
|
|
|
69
70
|
const VIRTUAL_ROUTING_CONFIG = 'virtual:conloca-routing-config';
|
|
70
71
|
const VIRTUAL_LAYOUT = 'virtual:conloca-layout';
|
|
71
72
|
const VIRTUAL_PUCK_CONFIG = 'virtual:conloca-puck-config';
|
|
73
|
+
const VIRTUAL_CONTENT_SCHEMAS = 'virtual:conloca-content-schemas';
|
|
72
74
|
|
|
73
75
|
// Resolved IDs (with \0 prefix to prevent file resolution)
|
|
74
76
|
const RESOLVED_ROUTING_CONFIG = `\0${VIRTUAL_ROUTING_CONFIG}`;
|
|
75
77
|
const RESOLVED_LAYOUT = `\0${VIRTUAL_LAYOUT}`;
|
|
76
78
|
const RESOLVED_PUCK_CONFIG = `\0${VIRTUAL_PUCK_CONFIG}`;
|
|
79
|
+
const RESOLVED_CONTENT_SCHEMAS = `\0${VIRTUAL_CONTENT_SCHEMAS}`;
|
|
77
80
|
|
|
78
81
|
// Virtual modules for hydration system
|
|
79
82
|
const VIRTUAL_HYDRATION_REGISTRY = 'virtual:conloca-hydration-registry';
|
|
@@ -107,6 +110,19 @@ export interface ConlocaCMSOptions extends Omit<UIConfig, 'basename'> {
|
|
|
107
110
|
puckConfigPath: string; // Path to the puck config module (should be .tsx file with React components)
|
|
108
111
|
schemasPath?: string; // Path to the schemas module (exports { dataSchemas, pageSchemas, mdxComponents })
|
|
109
112
|
|
|
113
|
+
/**
|
|
114
|
+
* Module exporting the `pageSchemas` and/or `dataSchemas` the content config
|
|
115
|
+
* hands conlocaPages() and conlocaData(). A draft preview builds the same
|
|
116
|
+
* entry parsers from them and parses each draft as the build parses an
|
|
117
|
+
* entry, so it gets the stored values and fails where the build would. It
|
|
118
|
+
* loads in the preview server, so it may import schema code only (Typia
|
|
119
|
+
* host schemas, definePageSchema) — the module the content config imports
|
|
120
|
+
* them from, not the editor's `schemasPath` barrel.
|
|
121
|
+
*
|
|
122
|
+
* @example './src/page-schemas.ts'
|
|
123
|
+
*/
|
|
124
|
+
contentSchemasPath?: string;
|
|
125
|
+
|
|
110
126
|
/**
|
|
111
127
|
* Default layout for content pages.
|
|
112
128
|
* When provided without routing config, enables routing with catch-all pattern.
|
|
@@ -815,6 +831,9 @@ export function conlocaCMS(options: ConlocaCMSOptions): AstroIntegration {
|
|
|
815
831
|
if (id === VIRTUAL_PUCK_CONFIG) {
|
|
816
832
|
return RESOLVED_PUCK_CONFIG;
|
|
817
833
|
}
|
|
834
|
+
if (id === VIRTUAL_CONTENT_SCHEMAS) {
|
|
835
|
+
return RESOLVED_CONTENT_SCHEMAS;
|
|
836
|
+
}
|
|
818
837
|
// Hydration virtual modules
|
|
819
838
|
if (id === VIRTUAL_HYDRATION_REGISTRY) {
|
|
820
839
|
return RESOLVED_HYDRATION_REGISTRY;
|
|
@@ -839,6 +858,11 @@ export function conlocaCMS(options: ConlocaCMSOptions): AstroIntegration {
|
|
|
839
858
|
if (id === RESOLVED_PUCK_CONFIG) {
|
|
840
859
|
return generatePuckConfigModule(options.puckConfigPath);
|
|
841
860
|
}
|
|
861
|
+
if (id === RESOLVED_CONTENT_SCHEMAS) {
|
|
862
|
+
return generateContentSchemasModule(
|
|
863
|
+
options.contentSchemasPath ? resolve(options.contentSchemasPath) : undefined,
|
|
864
|
+
);
|
|
865
|
+
}
|
|
842
866
|
// Hydration virtual modules
|
|
843
867
|
if (id === RESOLVED_HYDRATION_REGISTRY) {
|
|
844
868
|
// Always scan - paths are auto-derived from puckConfigPath
|
|
@@ -960,7 +984,6 @@ initHydration(componentRegistry)
|
|
|
960
984
|
'clsx',
|
|
961
985
|
'tailwind-merge',
|
|
962
986
|
// Content processing
|
|
963
|
-
'zod',
|
|
964
987
|
'yaml',
|
|
965
988
|
'nanoid',
|
|
966
989
|
'sort-keys',
|
package/src/virtual-modules.d.ts
CHANGED
|
@@ -57,6 +57,20 @@ declare module 'virtual:conloca-puck-config' {
|
|
|
57
57
|
export default config;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Virtual module: virtual:conloca-content-schemas
|
|
62
|
+
*
|
|
63
|
+
* The site's `contentSchemasPath` module (the `pageSchemas` / `dataSchemas`
|
|
64
|
+
* its content config hands the collection factories), imported on the first
|
|
65
|
+
* call only; an empty object when the site names none. A preview render
|
|
66
|
+
* builds its collections' entry parsers from it.
|
|
67
|
+
*/
|
|
68
|
+
declare module 'virtual:conloca-content-schemas' {
|
|
69
|
+
import type { DataSchemas, PageSchemas } from '@conloca/cms-spa/page-schema-define';
|
|
70
|
+
|
|
71
|
+
export function loadContentSchemas(): Promise<{ pageSchemas?: PageSchemas; dataSchemas?: DataSchemas }>;
|
|
72
|
+
}
|
|
73
|
+
|
|
60
74
|
/**
|
|
61
75
|
* Virtual module: virtual:conloca-hydration-entry
|
|
62
76
|
*
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { type PageSchemas } from '@conloca/cms-spa/page-schema-define';
|
|
2
|
-
import type { ContentMeta } from '@conloca/content-api';
|
|
3
|
-
import { type PageMeta } from '@conloca/content-api/schemas';
|
|
4
|
-
import { z } from 'zod';
|
|
5
|
-
/**
|
|
6
|
-
* Fold every page-schema descriptor into one Zod object of host meta fields.
|
|
7
|
-
* Requiredness is not preserved — the caller always `.partial()`s this for the
|
|
8
|
-
* collection, because Astro validates the whole `pages` pile.
|
|
9
|
-
*
|
|
10
|
-
* The merged object has one slot per field name, so two descriptors may only
|
|
11
|
-
* declare the same name by sharing the same field schema instance — last-wins
|
|
12
|
-
* would silently rewrite what the earlier prefix's pages are validated
|
|
13
|
-
* against, failing them with a Zod error that names neither descriptor.
|
|
14
|
-
*/
|
|
15
|
-
export declare function hostFieldsFromPageSchemas(pageSchemas: PageSchemas): z.ZodObject<z.ZodRawShape>;
|
|
16
|
-
/**
|
|
17
|
-
* A page collection entry as the loader delivers it: the validated
|
|
18
|
-
* `pageMetaSchema` fields, the loader-written record fields (`created`,
|
|
19
|
-
* `modified`, `name`, `puckData` — admitted by the loose index at runtime),
|
|
20
|
-
* and the host's meta fields through `ContentMeta`/`HostPageMeta`.
|
|
21
|
-
*/
|
|
22
|
-
export type ConlocaPageEntry = ContentMeta & PageMeta & {
|
|
23
|
-
created?: string | Date;
|
|
24
|
-
modified?: string | Date;
|
|
25
|
-
name?: string;
|
|
26
|
-
puckData?: unknown;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* The schema Astro validates page entries against: the host's page-meta fields
|
|
30
|
-
* (all optional) with conloca's structural fields layered over them, loose so
|
|
31
|
-
* undeclared top-level fields still reach the rendered page.
|
|
32
|
-
*
|
|
33
|
-
* Looseness is TOP-LEVEL ONLY. A nested `z.object()` still strips undeclared
|
|
34
|
-
* nested keys; a host that wants them preserved has to write `z.looseObject()`.
|
|
35
|
-
*
|
|
36
|
-
* Rewrite bans (.default / .coerce / .transform / .catch) walk nested objects,
|
|
37
|
-
* arrays, and unions. `.partial()` is still shallow.
|
|
38
|
-
*
|
|
39
|
-
* Internal on purpose: `src/collections.ts` is a published entry (the
|
|
40
|
-
* `./collections` subpath), so anything exported from there is frozen under
|
|
41
|
-
* semver. Nothing outside this package consumes it.
|
|
42
|
-
*/
|
|
43
|
-
export declare function buildPageCollectionSchema(hostFields: z.ZodObject<z.ZodRawShape>): z.ZodType<ConlocaPageEntry>;
|
|
44
|
-
//# sourceMappingURL=page-collection-schema.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"page-collection-schema.d.ts","sourceRoot":"","sources":["../../../../src/internal/page-collection-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAA0B,MAAM,qCAAqC,CAAC;AAC/F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,KAAK,QAAQ,EAAkB,MAAM,8BAA8B,CAAC;AAC7E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAwGxB;;;;;;;;;GASG;AACH,wBAAgB,yBAAyB,CAAC,WAAW,EAAE,WAAW,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CA4B9F;AAED;;;;;GAKG;AACH,MAAM,MAAM,gBAAgB,GAAG,WAAW,GACxC,QAAQ,GAAG;IACT,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEJ;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,yBAAyB,CAAC,UAAU,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAc7G"}
|
|
@@ -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,C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,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
|
-
}
|