@conloca/content-api 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 (76) hide show
  1. package/dist/.build-inputs.json +71 -0
  2. package/dist/address-site.d.ts +6 -0
  3. package/dist/address-site.d.ts.map +1 -0
  4. package/dist/address-site.js +15 -0
  5. package/dist/address-site.js.map +1 -0
  6. package/dist/asset-operations.d.ts +15 -2
  7. package/dist/asset-operations.d.ts.map +1 -1
  8. package/dist/asset-operations.js +216 -78
  9. package/dist/asset-operations.js.map +1 -1
  10. package/dist/cf-access.d.ts +4 -2
  11. package/dist/cf-access.d.ts.map +1 -1
  12. package/dist/cf-access.js +29 -13
  13. package/dist/cf-access.js.map +1 -1
  14. package/dist/content-file-id.d.ts +9 -0
  15. package/dist/content-file-id.d.ts.map +1 -0
  16. package/dist/content-file-id.js +33 -0
  17. package/dist/content-file-id.js.map +1 -0
  18. package/dist/content-utils.d.ts +4 -1
  19. package/dist/content-utils.d.ts.map +1 -1
  20. package/dist/content-utils.js +14 -1
  21. package/dist/content-utils.js.map +1 -1
  22. package/dist/editor-host.d.ts +121 -0
  23. package/dist/editor-host.d.ts.map +1 -0
  24. package/dist/editor-host.js +212 -0
  25. package/dist/editor-host.js.map +1 -0
  26. package/dist/filesystem-content-api.d.ts.map +1 -1
  27. package/dist/filesystem-content-api.js +3 -2
  28. package/dist/filesystem-content-api.js.map +1 -1
  29. package/dist/git-operations.d.ts.map +1 -1
  30. package/dist/git-operations.js +162 -34
  31. package/dist/git-operations.js.map +1 -1
  32. package/dist/index.d.ts +2 -1
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +2 -1
  35. package/dist/index.js.map +1 -1
  36. package/dist/middleware.d.ts.map +1 -1
  37. package/dist/middleware.js +5 -0
  38. package/dist/middleware.js.map +1 -1
  39. package/dist/schemas/index.d.ts +9 -97
  40. package/dist/schemas/index.d.ts.map +1 -1
  41. package/dist/schemas/index.js +8 -83
  42. package/dist/schemas/index.js.map +1 -1
  43. package/dist/schemas/normalize.d.ts +101 -0
  44. package/dist/schemas/normalize.d.ts.map +1 -0
  45. package/dist/schemas/normalize.js +168 -0
  46. package/dist/schemas/normalize.js.map +1 -0
  47. package/dist/schemas/types.d.ts +135 -0
  48. package/dist/schemas/types.d.ts.map +1 -0
  49. package/dist/schemas/types.js +17 -0
  50. package/dist/schemas/types.js.map +1 -0
  51. package/dist/schemas/validators.d.ts +46 -0
  52. package/dist/schemas/validators.d.ts.map +1 -0
  53. package/dist/schemas/validators.js +938 -0
  54. package/dist/schemas/validators.js.map +1 -0
  55. package/dist/site.d.ts.map +1 -1
  56. package/dist/site.js +3 -1
  57. package/dist/site.js.map +1 -1
  58. package/dist/types.d.ts +9 -0
  59. package/dist/types.d.ts.map +1 -1
  60. package/package.json +17 -5
  61. package/src/address-site.ts +19 -0
  62. package/src/asset-operations.ts +256 -83
  63. package/src/cf-access.ts +30 -14
  64. package/src/content-file-id.ts +31 -0
  65. package/src/content-utils.ts +18 -2
  66. package/src/editor-host.ts +268 -0
  67. package/src/filesystem-content-api.ts +3 -2
  68. package/src/git-operations.ts +188 -41
  69. package/src/index.ts +19 -1
  70. package/src/middleware.ts +5 -0
  71. package/src/schemas/index.ts +34 -108
  72. package/src/schemas/normalize.ts +194 -0
  73. package/src/schemas/types.ts +154 -0
  74. package/src/schemas/validators.ts +176 -0
  75. package/src/site.ts +3 -1
  76. package/src/types.ts +9 -0
@@ -1,113 +1,39 @@
1
- import { z } from 'zod';
2
-
3
- // ===== Page Schemas =====
4
-
5
- /**
6
- * Base schema for page metadata - editable fields only.
7
- * Used by CMS forms for dynamic form generation.
8
- * Add .describe() for form field labels/hints.
9
- */
10
- export const pageEditableSchema = z.object({
11
- title: z.string().describe('Page title for SEO and browser tab'),
12
- description: z.string().optional().describe('Meta description for search engines'),
13
- pathname: z.string().describe('URL path (e.g., /about)'),
14
- publishAt: z.coerce.date().nullable().optional().describe('Schedule publish date/time'),
15
- unpublishAt: z.coerce.date().nullable().optional().describe('Schedule unpublish date/time'),
16
- robots: z.string().optional().describe('Robots meta tag (e.g., index, follow)'),
17
- canonical: z.string().url().optional().describe('Canonical URL for duplicate content'),
18
- });
19
-
20
- export type PageEditable = z.infer<typeof pageEditableSchema>;
21
-
22
- /**
23
- * Full page schema with system fields.
24
- * Used by Astro Content Collections for validation.
25
- */
26
- export const pageMetaSchema = pageEditableSchema.extend({
27
- id: z.string(),
28
- locale: z.string(),
29
- });
30
-
31
- export type PageMeta = z.infer<typeof pageMetaSchema>;
32
-
33
- // ===== Fragment Schemas =====
34
-
35
- /**
36
- * Base schema for fragment metadata - editable fields only.
37
- * Used by CMS forms for dynamic form generation.
38
- * Add .describe() for form field labels/hints.
39
- */
40
- export const fragmentEditableSchema = z.object({
41
- title: z.string().describe('Display name for the fragment'),
42
- description: z.string().optional().describe('Brief description of this fragment'),
43
- category: z.string().optional().describe('Category for organizing fragments (e.g., headers, cta, content)'),
44
- tags: z.array(z.string()).optional().describe('Tags for filtering and organization'),
45
- });
46
-
47
- export type FragmentEditable = z.infer<typeof fragmentEditableSchema>;
48
-
49
1
  /**
50
- * Full fragment schema with system fields.
51
- * Used by Astro Content Collections for validation.
52
- */
53
- export const fragmentMetaSchema = fragmentEditableSchema.extend({
54
- id: z.string(),
55
- locale: z.string(),
56
- name: z.string(),
57
- });
58
-
59
- export type FragmentMeta = z.infer<typeof fragmentMetaSchema>;
60
-
61
- // ===== MDX Page Schemas =====
62
-
63
- /**
64
- * Base schema for mdx-type page frontmatter — renderer-neutral.
2
+ * Content metadata shapes and their generated validators.
65
3
  *
66
- * Default for the CMS form when editing an mdx page; projects override via
67
- * `mdxPages.schema` when they want a renderer-specific shape (e.g. an
68
- * Astro-collection schema with extra fields).
4
+ * Zod is gone from this package: the shapes are plain TypeScript types
5
+ * (`./types`), the validators are Typia-generated (`./validators`), and the
6
+ * coercion/strip behaviour the Zod schemas performed implicitly is explicit and
7
+ * separately tested (`./normalize`).
69
8
  *
70
- * Permissive on purpose: `.passthrough()` lets unknown frontmatter keys flow
71
- * through unchanged so the file's existing fields are never lost on save.
9
+ * The `parseLoose*` variants are what `@conloca/astro-cms`'s loader validates
10
+ * collection entries with before they reach Astro's data store.
72
11
  */
73
- export const mdxPageFrontmatterSchema = z
74
- .object({
75
- title: z.string().describe('Page title for SEO and browser tab'),
76
- description: z.string().optional().describe('Meta description for search engines'),
77
- draft: z.boolean().optional().describe('Hide the page from production builds'),
78
- })
79
- .passthrough();
80
-
81
- export type MdxPageFrontmatter = z.infer<typeof mdxPageFrontmatterSchema>;
82
-
83
- // ===== Data Schemas =====
84
-
85
- /**
86
- * Base schema for data entry metadata - editable fields only.
87
- * Used by CMS forms for dynamic form generation.
88
- * Add .describe() for form field labels/hints.
89
- */
90
- export const dataEditableSchema = z.object({
91
- title: z.string().describe('Display name for the entry'),
92
- description: z.string().optional().describe('Brief description of this entry'),
93
- category: z.string().optional().describe('Category for organizing entries'),
94
- tags: z.array(z.string()).optional().describe('Tags for filtering and organization'),
95
- });
96
-
97
- export type DataEditable = z.infer<typeof dataEditableSchema>;
98
-
99
- /**
100
- * Zod schema for data entry metadata.
101
- * Covers common organizational fields for structured data collections.
102
- */
103
- export const dataMetaSchema = dataEditableSchema.extend({
104
- // System fields (added by loader)
105
- id: z.string(),
106
- locale: z.string(),
107
- name: z.string(),
108
-
109
- // Actual data content (arbitrary JSON structure)
110
- data: z.record(z.string(), z.unknown()).optional(),
111
- });
112
12
 
113
- export type DataMeta = z.infer<typeof dataMetaSchema>;
13
+ export type {
14
+ DataEditable,
15
+ DataMeta,
16
+ FragmentEditable,
17
+ FragmentMeta,
18
+ LooseDataMeta,
19
+ LooseFragmentMeta,
20
+ LoosePageMeta,
21
+ MdxPageFrontmatter,
22
+ PageEditable,
23
+ PageMeta,
24
+ SchemaIssue,
25
+ SchemaParseResult,
26
+ } from './types.js';
27
+ export {
28
+ PAGE_META_KEYS,
29
+ parseDataEditable,
30
+ parseDataMeta,
31
+ parseFragmentEditable,
32
+ parseFragmentMeta,
33
+ parseLooseDataMeta,
34
+ parseLooseFragmentMeta,
35
+ parseLoosePageMeta,
36
+ parseMdxPageFrontmatter,
37
+ parsePageEditable,
38
+ parsePageMeta,
39
+ } from './validators.js';
@@ -0,0 +1,194 @@
1
+ /**
2
+ * The value rewriting the replaced Zod schemas performed before validating that
3
+ * Typia does not do, spelled out as pure functions.
4
+ *
5
+ * Typia validates the value it is given — it never coerces and never copies —
6
+ * so these Zod steps are explicit here, each unit-tested against the recorded
7
+ * Zod behaviour: the fresh object every Zod parse returned, holding the
8
+ * declared fields (and, for `.loose()`/`.passthrough()`, the undeclared ones)
9
+ * with arrays and records copied; `z.coerce.date()`; and `z.string().url()`
10
+ * (Zod's check is "the URL parser accepts it" and it rewrites the value, which
11
+ * Typia's `tags.Format<'url'>` regex neither matches nor does).
12
+ */
13
+
14
+ import type { SchemaIssue } from './types.js';
15
+
16
+ /** The page fields that were declared `z.coerce.date().nullable().optional()`. */
17
+ export const PAGE_DATE_KEYS = ['publishAt', 'unpublishAt'] as const;
18
+
19
+ /** The page fields that were declared `z.string().url().optional()`. */
20
+ export const PAGE_URL_KEYS = ['canonical'] as const;
21
+
22
+ /** The `expected` a rejected URL field reports, in Typia's notation. */
23
+ const URL_EXPECTED = 'string & Format<"url">';
24
+
25
+ /** The `expected` a rejected record field reports, in Typia's notation. */
26
+ const RECORD_EXPECTED = 'Record<string, unknown>';
27
+
28
+ /** The URL parser deletes every ASCII tab, LF and CR before parsing; so did Zod's output. */
29
+ const ASCII_TAB_OR_NEWLINE = /[\t\n\r]/g;
30
+
31
+ /** One shape's fields, by what normalization does with them. */
32
+ export interface ShapeFields {
33
+ /**
34
+ * Every field the shape declares. Zod read each by property lookup, so an
35
+ * inherited or non-enumerable declared field counts; the copy carries it as
36
+ * an own field.
37
+ */
38
+ readonly declared: readonly string[];
39
+ /**
40
+ * Whether undeclared fields survive the parse (`.loose()`/`.passthrough()`).
41
+ * A strict shape never reads them, as `z.object()` never did, so a throwing
42
+ * getter on a field the shape strips cannot fail the parse.
43
+ */
44
+ readonly keepsUndeclared: boolean;
45
+ /** Fields declared `z.coerce.date().nullable().optional()`. */
46
+ readonly dates?: readonly string[];
47
+ /** Fields declared `z.string().url()`. */
48
+ readonly urls?: readonly string[];
49
+ /** Fields declared `z.array()`: an array is copied so the parsed one never aliases the caller's. */
50
+ readonly arrays?: readonly string[];
51
+ /**
52
+ * Fields declared `z.record(z.string(), …)`: Zod accepted only a plain object
53
+ * and returned a fresh one holding its own enumerable string keys.
54
+ */
55
+ readonly records?: readonly string[];
56
+ }
57
+
58
+ /** A normalized value plus the failures a generated validator cannot see in it. */
59
+ export interface Normalized {
60
+ readonly value: unknown;
61
+ readonly issues: readonly SchemaIssue[];
62
+ }
63
+
64
+ /** A non-null, non-array object — the only input shape normalization can rewrite. */
65
+ function isRecord(value: unknown): value is Record<string, unknown> {
66
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
67
+ }
68
+
69
+ /**
70
+ * `z.coerce.date()` under a `.nullable().optional()` wrapper: `undefined` and
71
+ * `null` short-circuit before coercion, anything else goes through `new Date()`.
72
+ *
73
+ * A `Date` input is cloned rather than passed through, which is what Zod did:
74
+ * the parsed value never aliases the caller's Date, so mutating one cannot
75
+ * reach through to the other.
76
+ *
77
+ * An uncoercible value yields an Invalid Date; that is not an error here.
78
+ * `normalize()` reports it, because a `Date` carrying `NaN` still satisfies
79
+ * every `instanceof Date` check a generated validator can make. That includes
80
+ * values `new Date()` throws on (a BigInt, a Symbol, an object whose
81
+ * `toString`/`valueOf` are not callable), so a parse reports them as an issue
82
+ * instead of throwing.
83
+ */
84
+ export function coerceNullableDate(value: unknown): Date | null | undefined {
85
+ if (value === undefined) return undefined;
86
+ if (value === null) return null;
87
+ try {
88
+ return new Date(value as string | number);
89
+ } catch {
90
+ return new Date(Number.NaN);
91
+ }
92
+ }
93
+
94
+ /**
95
+ * `z.string().url()`: a string passes iff the WHATWG URL parser accepts it once
96
+ * trimmed, and the parsed value is that trimmed string with ASCII tabs and
97
+ * newlines removed (not the normalized `href`). `undefined` means rejected.
98
+ */
99
+ export function coerceUrl(value: string): string | undefined {
100
+ const trimmed = value.trim();
101
+ try {
102
+ new URL(trimmed);
103
+ } catch {
104
+ return undefined;
105
+ }
106
+ return trimmed.replace(ASCII_TAB_OR_NEWLINE, '');
107
+ }
108
+
109
+ /**
110
+ * The value a validator may rewrite, and the issues only normalization can see.
111
+ *
112
+ * Every object input becomes a fresh top-level copy — as every Zod parse
113
+ * returned one — so the parsed value never is the caller's. Like Zod's, the
114
+ * copy reads each field once: first every `declared` field the input has
115
+ * however it holds it (`key in input`: inherited, non-enumerable, a getter),
116
+ * then, for a `keepsUndeclared` shape (`.loose()`), every other enumerable
117
+ * field, own or inherited, by reference — except a `__proto__` key, which Zod
118
+ * dropped. Then:
119
+ * - `dates` are coerced, and each that ends up an Invalid Date is an issue
120
+ * (`expected: Date`), the way Zod reported it;
121
+ * - `urls` holding a string are checked and rewritten by `coerceUrl()`, a
122
+ * rejected one is an issue; a non-string is left for the generated type check;
123
+ * - `arrays` holding an array are copied;
124
+ * - `records` holding a plain object are rebuilt by `copyRecord()`; any other
125
+ * object (an array, a `Date`, a class instance) is an issue, which the
126
+ * generated `Record<string, unknown>` check cannot be relied on to raise.
127
+ *
128
+ * A non-object passes through untouched for the validator to report.
129
+ */
130
+ export function normalize(input: unknown, fields: ShapeFields): Normalized {
131
+ if (!isRecord(input)) return { value: input, issues: [] };
132
+ const value: Record<string, unknown> = {};
133
+ for (const key of fields.declared) {
134
+ if (key in input) value[key] = input[key];
135
+ }
136
+ if (fields.keepsUndeclared) {
137
+ for (const key in input) {
138
+ // Skipping `__proto__` also keeps the assignment below from setting the copy's prototype.
139
+ if (key !== '__proto__' && !fields.declared.includes(key)) value[key] = input[key];
140
+ }
141
+ }
142
+ const issues: SchemaIssue[] = [];
143
+ for (const key of fields.dates ?? []) {
144
+ if (!Object.hasOwn(value, key)) continue;
145
+ const coerced = coerceNullableDate(value[key]);
146
+ value[key] = coerced;
147
+ if (coerced instanceof Date && Number.isNaN(coerced.getTime())) {
148
+ issues.push({ path: `$input.${key}`, expected: 'Date', value: coerced });
149
+ }
150
+ }
151
+ for (const key of fields.urls ?? []) {
152
+ const raw = value[key];
153
+ if (typeof raw !== 'string') continue;
154
+ const coerced = coerceUrl(raw);
155
+ if (coerced === undefined) issues.push({ path: `$input.${key}`, expected: URL_EXPECTED, value: raw });
156
+ else value[key] = coerced;
157
+ }
158
+ for (const key of fields.arrays ?? []) {
159
+ const raw = value[key];
160
+ if (Array.isArray(raw)) value[key] = [...raw];
161
+ }
162
+ for (const key of fields.records ?? []) {
163
+ const raw = value[key];
164
+ if (typeof raw !== 'object' || raw === null) continue;
165
+ const copy = Array.isArray(raw) ? undefined : copyRecord(raw);
166
+ if (copy === undefined) issues.push({ path: `$input.${key}`, expected: RECORD_EXPECTED, value: raw });
167
+ else value[key] = copy;
168
+ }
169
+ return { value, issues };
170
+ }
171
+
172
+ /**
173
+ * `z.record(z.string(), z.unknown())`: `undefined` when Zod rejected the value,
174
+ * else a fresh object with its own enumerable string keys. Zod's plain-object
175
+ * test is constructor-based (`Object.create(null)` and objects inheriting from
176
+ * a plain object pass; a `Date` or a class instance does not), it skipped a
177
+ * `__proto__` key, and its string key schema rejected an enumerable symbol key.
178
+ */
179
+ export function copyRecord(value: object): Record<string, unknown> | undefined {
180
+ const valueConstructor: unknown = value.constructor;
181
+ if (typeof valueConstructor === 'function') {
182
+ const prototype: unknown = valueConstructor.prototype;
183
+ if (typeof prototype !== 'object' || prototype === null) return undefined;
184
+ if (!Object.hasOwn(prototype, 'isPrototypeOf')) return undefined;
185
+ }
186
+ if (Object.getOwnPropertySymbols(value).some((symbol) => Object.prototype.propertyIsEnumerable.call(value, symbol))) {
187
+ return undefined;
188
+ }
189
+ const copy: Record<string, unknown> = {};
190
+ for (const [key, entry] of Object.entries(value)) {
191
+ if (key !== '__proto__') copy[key] = entry;
192
+ }
193
+ return copy;
194
+ }
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Content metadata shapes the CMS reads and writes.
3
+ *
4
+ * These were Zod schema objects (`pageMetaSchema`, `dataMetaSchema`, …) until
5
+ * the Typia migration. They are now plain TypeScript types: `./validators`
6
+ * carries the generated validators, `./normalize` carries the coercion and
7
+ * key-stripping the Zod schemas used to perform. `@conloca/astro-cms` validates
8
+ * collection entries with the same generated validators inside its loader.
9
+ *
10
+ * Optional fields also admit an explicit `undefined`, as the `z.infer` types
11
+ * they replaced did, so consumers compiling with `exactOptionalPropertyTypes`
12
+ * keep accepting `{ description: undefined }`. The closed shapes are type
13
+ * aliases, not interfaces, for the same reason: like the `z.infer` aliases,
14
+ * they stay assignable to `Record<string, unknown>`.
15
+ */
16
+
17
+ // ===== Page =====
18
+
19
+ /**
20
+ * Page metadata — editable fields only. Used by CMS forms for dynamic form
21
+ * generation; the form-field descriptors live in the CMS package.
22
+ */
23
+ export type PageEditable = {
24
+ /** Page title for SEO and browser tab. */
25
+ title: string;
26
+ /** Meta description for search engines. */
27
+ description?: string | undefined;
28
+ /** URL path (e.g. `/about`). */
29
+ pathname: string;
30
+ /** Schedule publish date/time. */
31
+ publishAt?: Date | null | undefined;
32
+ /** Schedule unpublish date/time. */
33
+ unpublishAt?: Date | null | undefined;
34
+ /** Robots meta tag (e.g. `index, follow`). */
35
+ robots?: string | undefined;
36
+ /** Canonical URL for duplicate content. Validated as a URL. */
37
+ canonical?: string | undefined;
38
+ };
39
+
40
+ /** Full page metadata with the system fields the loader adds. */
41
+ export type PageMeta = PageEditable & {
42
+ id: string;
43
+ locale: string;
44
+ };
45
+
46
+ // ===== Fragment =====
47
+
48
+ /** Fragment metadata — editable fields only. */
49
+ export type FragmentEditable = {
50
+ /** Display name for the fragment. */
51
+ title: string;
52
+ /** Brief description of this fragment. */
53
+ description?: string | undefined;
54
+ /** Category for organizing fragments (e.g. headers, cta, content). */
55
+ category?: string | undefined;
56
+ /** Tags for filtering and organization. */
57
+ tags?: string[] | undefined;
58
+ };
59
+
60
+ /** Full fragment metadata with the system fields the loader adds. */
61
+ export type FragmentMeta = FragmentEditable & {
62
+ id: string;
63
+ locale: string;
64
+ name: string;
65
+ };
66
+
67
+ // ===== MDX page frontmatter =====
68
+
69
+ /**
70
+ * Frontmatter of an `mdx`-type page — renderer-neutral.
71
+ *
72
+ * Default for the CMS form when editing an mdx page; projects override via
73
+ * `mdxPages.schema` when they want a renderer-specific shape.
74
+ *
75
+ * Permissive on purpose: the index signature is the Zod `.passthrough()` this
76
+ * replaced — unknown frontmatter keys flow through unchanged so the file's
77
+ * existing fields are never lost on save.
78
+ */
79
+ export interface MdxPageFrontmatter {
80
+ /** Page title for SEO and browser tab. */
81
+ title: string;
82
+ /** Meta description for search engines. */
83
+ description?: string | undefined;
84
+ /** Hide the page from production builds. */
85
+ draft?: boolean | undefined;
86
+ [key: string]: unknown;
87
+ }
88
+
89
+ // ===== Data =====
90
+
91
+ /** Data-entry metadata — editable fields only. */
92
+ export type DataEditable = {
93
+ /** Display name for the entry. */
94
+ title: string;
95
+ /** Brief description of this entry. */
96
+ description?: string | undefined;
97
+ /** Category for organizing entries. */
98
+ category?: string | undefined;
99
+ /** Tags for filtering and organization. */
100
+ tags?: string[] | undefined;
101
+ };
102
+
103
+ /**
104
+ * Full data-entry metadata: the editable fields, the system fields the loader
105
+ * adds, and the arbitrary JSON payload under `data`.
106
+ */
107
+ export type DataMeta = DataEditable & {
108
+ id: string;
109
+ locale: string;
110
+ name: string;
111
+ data?: Record<string, unknown> | undefined;
112
+ };
113
+
114
+ // ===== Collection entries =====
115
+
116
+ /**
117
+ * Page metadata as a collection entry carries it: the declared fields validated,
118
+ * every other top-level field (loader-written `created`/`puckData`, host meta)
119
+ * kept as-is. This is what `pageMetaSchema.loose()` accepted.
120
+ */
121
+ export interface LoosePageMeta extends PageMeta {
122
+ [key: string]: unknown;
123
+ }
124
+
125
+ /** Fragment metadata with undeclared top-level fields kept (`fragmentMetaSchema.loose()`). */
126
+ export interface LooseFragmentMeta extends FragmentMeta {
127
+ [key: string]: unknown;
128
+ }
129
+
130
+ /** Data-entry metadata with undeclared top-level fields kept (`dataMetaSchema.loose()`). */
131
+ export interface LooseDataMeta extends DataMeta {
132
+ [key: string]: unknown;
133
+ }
134
+
135
+ // ===== Validation results =====
136
+
137
+ /**
138
+ * One validation failure, in the shape Typia reports it: the `$input`-rooted
139
+ * path, the type that was expected there, and the value that was found.
140
+ *
141
+ * Declared here rather than re-exported from Typia so this package's published
142
+ * declarations stay free of a `typia` type import — the transform runs at build
143
+ * time and consumers get generated code, not a Typia dependency.
144
+ */
145
+ export interface SchemaIssue {
146
+ readonly path: string;
147
+ readonly expected: string;
148
+ readonly value: unknown;
149
+ }
150
+
151
+ /** Outcome of a `parse*` call: the Zod `safeParse` contract, without Zod. */
152
+ export type SchemaParseResult<T> =
153
+ | { readonly success: true; readonly data: T }
154
+ | { readonly success: false; readonly issues: readonly SchemaIssue[] };
@@ -0,0 +1,176 @@
1
+ /**
2
+ * Generated validators for the content metadata shapes.
3
+ *
4
+ * Every `typia.createValidate<T>()` below is rewritten into concrete generated
5
+ * validation code by the `ttsc` transform (spec 12 §The build chain) — in
6
+ * tsdown's Rolldown pipeline for the published build, and by the Bun test
7
+ * preload for `bun test`. Nothing here hand-rolls a check.
8
+ *
9
+ * `parse*` is the replacement for the Zod schemas' `safeParse`: normalize first
10
+ * (`./normalize` reproduces what Zod did implicitly: the fresh copy, the
11
+ * unknown-key strip of the strict shapes, the date coercion and the `.url()`
12
+ * check), then validate with `typia.createValidate<T>()`.
13
+ *
14
+ * `canonical` deliberately carries no `tags.Format<'url'>`: Typia's URL regex
15
+ * rejects URLs Zod accepted (`https://example.com?lang=en`, `#top`,
16
+ * `http://localhost:4321/`) and accepts ones it rejected (port 99999), so the
17
+ * page parsers keep Zod's parser-based check in `normalize()` instead.
18
+ */
19
+
20
+ import typia, { type IValidation } from 'typia';
21
+ import { normalize, PAGE_DATE_KEYS, PAGE_URL_KEYS, type ShapeFields } from './normalize.js';
22
+ import type {
23
+ DataEditable,
24
+ DataMeta,
25
+ FragmentEditable,
26
+ FragmentMeta,
27
+ LooseDataMeta,
28
+ LooseFragmentMeta,
29
+ LoosePageMeta,
30
+ MdxPageFrontmatter,
31
+ PageEditable,
32
+ PageMeta,
33
+ SchemaIssue,
34
+ SchemaParseResult,
35
+ } from './types.js';
36
+
37
+ /** Field names `PageMeta` declares — the fields a host page schema may not redeclare. */
38
+ export const PAGE_META_KEYS: readonly (keyof PageMeta)[] = typia.reflect.literals<keyof PageMeta>();
39
+
40
+ const validatePageEditable = typia.createValidate<PageEditable>();
41
+ const validatePageMeta = typia.createValidate<PageMeta>();
42
+ const validateFragmentEditable = typia.createValidate<FragmentEditable>();
43
+ const validateFragmentMeta = typia.createValidate<FragmentMeta>();
44
+ const validateMdxPageFrontmatter = typia.createValidate<MdxPageFrontmatter>();
45
+ const validateDataEditable = typia.createValidate<DataEditable>();
46
+ const validateDataMeta = typia.createValidate<DataMeta>();
47
+ const validateLoosePageMeta = typia.createValidate<LoosePageMeta>();
48
+ const validateLooseFragmentMeta = typia.createValidate<LooseFragmentMeta>();
49
+ const validateLooseDataMeta = typia.createValidate<LooseDataMeta>();
50
+
51
+ /** Typia's `IError` already carries path/expected/value; copy it so no Typia type reaches the public API. */
52
+ function toIssues(errors: readonly IValidation.IError[]): SchemaIssue[] {
53
+ return errors.map((error) => ({ path: error.path, expected: error.expected, value: error.value }));
54
+ }
55
+
56
+ /** The field names `T` declares, without an index signature's `string`/`number`. */
57
+ type DeclaredKey<T> = keyof {
58
+ [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
59
+ };
60
+
61
+ /** Page shapes: the former `z.coerce.date()` and `z.string().url()` fields. */
62
+ const PAGE_EDITABLE_FIELDS: ShapeFields = {
63
+ declared: typia.reflect.literals<keyof PageEditable>(),
64
+ keepsUndeclared: false,
65
+ dates: PAGE_DATE_KEYS,
66
+ urls: PAGE_URL_KEYS,
67
+ };
68
+ const PAGE_META_FIELDS: ShapeFields = { ...PAGE_EDITABLE_FIELDS, declared: PAGE_META_KEYS };
69
+ const LOOSE_PAGE_META_FIELDS: ShapeFields = { ...PAGE_META_FIELDS, keepsUndeclared: true };
70
+ /** Fragment shapes: the `z.array()` tags. */
71
+ const FRAGMENT_EDITABLE_FIELDS: ShapeFields = {
72
+ declared: typia.reflect.literals<keyof FragmentEditable>(),
73
+ keepsUndeclared: false,
74
+ arrays: ['tags'],
75
+ };
76
+ const FRAGMENT_META_FIELDS: ShapeFields = {
77
+ ...FRAGMENT_EDITABLE_FIELDS,
78
+ declared: typia.reflect.literals<keyof FragmentMeta>(),
79
+ };
80
+ const LOOSE_FRAGMENT_META_FIELDS: ShapeFields = { ...FRAGMENT_META_FIELDS, keepsUndeclared: true };
81
+ const MDX_PAGE_FRONTMATTER_FIELDS: ShapeFields = {
82
+ declared: typia.reflect.literals<DeclaredKey<MdxPageFrontmatter>>(),
83
+ keepsUndeclared: true,
84
+ };
85
+ /** Data shapes: the tags, plus the `z.record()` payload on the full one. */
86
+ const DATA_EDITABLE_FIELDS: ShapeFields = {
87
+ declared: typia.reflect.literals<keyof DataEditable>(),
88
+ keepsUndeclared: false,
89
+ arrays: ['tags'],
90
+ };
91
+ const DATA_META_FIELDS: ShapeFields = {
92
+ declared: typia.reflect.literals<keyof DataMeta>(),
93
+ keepsUndeclared: false,
94
+ arrays: ['tags'],
95
+ records: ['data'],
96
+ };
97
+ const LOOSE_DATA_META_FIELDS: ShapeFields = { ...DATA_META_FIELDS, keepsUndeclared: true };
98
+
99
+ /**
100
+ * Normalize, then validate, folding in the failures a generated validator
101
+ * cannot see: an Invalid Date (`new Date('nonsense')` is a `Date`, so every
102
+ * structural check passes while Zod rejected it as `expected: date`), a URL
103
+ * field the URL parser rejects, and a record field Zod's plain-object check
104
+ * rejected. A field the generated validator already reported is reported once.
105
+ */
106
+ function parseWith<T>(
107
+ input: unknown,
108
+ fields: ShapeFields,
109
+ validate: (input: unknown) => IValidation<T>,
110
+ ): SchemaParseResult<T> {
111
+ const { value, issues } = normalize(input, fields);
112
+ const result = validate(value);
113
+ if (!result.success) {
114
+ const reported = new Set(result.errors.map((error) => error.path));
115
+ return {
116
+ success: false,
117
+ issues: [...toIssues(result.errors), ...issues.filter((issue) => !reported.has(issue.path))],
118
+ };
119
+ }
120
+ if (issues.length > 0) return { success: false, issues };
121
+ return { success: true, data: result.data };
122
+ }
123
+
124
+ /** Editable page fields: unknown keys stripped, `publishAt`/`unpublishAt` coerced to `Date`, `canonical` checked. */
125
+ export function parsePageEditable(input: unknown): SchemaParseResult<PageEditable> {
126
+ return parseWith(input, PAGE_EDITABLE_FIELDS, validatePageEditable);
127
+ }
128
+
129
+ /** Full page metadata: editable fields plus `id`/`locale`, same strip, date coercion and URL check. */
130
+ export function parsePageMeta(input: unknown): SchemaParseResult<PageMeta> {
131
+ return parseWith(input, PAGE_META_FIELDS, validatePageMeta);
132
+ }
133
+
134
+ /** Editable fragment fields; unknown keys stripped. */
135
+ export function parseFragmentEditable(input: unknown): SchemaParseResult<FragmentEditable> {
136
+ return parseWith(input, FRAGMENT_EDITABLE_FIELDS, validateFragmentEditable);
137
+ }
138
+
139
+ /** Full fragment metadata: editable fields plus `id`/`locale`/`name`. */
140
+ export function parseFragmentMeta(input: unknown): SchemaParseResult<FragmentMeta> {
141
+ return parseWith(input, FRAGMENT_META_FIELDS, validateFragmentMeta);
142
+ }
143
+
144
+ /** MDX page frontmatter; undeclared keys are preserved (the former `.passthrough()`). */
145
+ export function parseMdxPageFrontmatter(input: unknown): SchemaParseResult<MdxPageFrontmatter> {
146
+ return parseWith(input, MDX_PAGE_FRONTMATTER_FIELDS, validateMdxPageFrontmatter);
147
+ }
148
+
149
+ /** Editable data-entry fields; unknown keys stripped. */
150
+ export function parseDataEditable(input: unknown): SchemaParseResult<DataEditable> {
151
+ return parseWith(input, DATA_EDITABLE_FIELDS, validateDataEditable);
152
+ }
153
+
154
+ /** Full data-entry metadata: editable fields plus `id`/`locale`/`name` and the `data` payload. */
155
+ export function parseDataMeta(input: unknown): SchemaParseResult<DataMeta> {
156
+ return parseWith(input, DATA_META_FIELDS, validateDataMeta);
157
+ }
158
+
159
+ /**
160
+ * A page collection entry: the declared page fields validated, dates coerced and
161
+ * `canonical` URL-checked, every undeclared top-level field kept (the former
162
+ * `pageMetaSchema.loose()`).
163
+ */
164
+ export function parseLoosePageMeta(input: unknown): SchemaParseResult<LoosePageMeta> {
165
+ return parseWith(input, LOOSE_PAGE_META_FIELDS, validateLoosePageMeta);
166
+ }
167
+
168
+ /** A fragment collection entry; undeclared top-level fields kept (the former `fragmentMetaSchema.loose()`). */
169
+ export function parseLooseFragmentMeta(input: unknown): SchemaParseResult<LooseFragmentMeta> {
170
+ return parseWith(input, LOOSE_FRAGMENT_META_FIELDS, validateLooseFragmentMeta);
171
+ }
172
+
173
+ /** A data collection entry; undeclared top-level fields kept (the former `dataMetaSchema.loose()`). */
174
+ export function parseLooseDataMeta(input: unknown): SchemaParseResult<LooseDataMeta> {
175
+ return parseWith(input, LOOSE_DATA_META_FIELDS, validateLooseDataMeta);
176
+ }
package/src/site.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ContentAPI } from './content-api.interface.js';
2
- import { localesOf } from './content-utils.js';
2
+ import { isReservedPathname, localesOf } from './content-utils.js';
3
3
  import type { SiteIndex } from './site-index.js';
4
4
  import type { ContentManifest, CreatePageInput, CreateResult, MoveResult, SiteFilters } from './types.js';
5
5
 
@@ -134,6 +134,8 @@ export class Site {
134
134
 
135
135
  // Validate pathname is available (not taken or in redirects)
136
136
  isPathnameAvailable(pathname: string, locale: string, excludeId?: string): boolean {
137
+ if (isReservedPathname(pathname)) return false;
138
+
137
139
  // Check if pathname is already taken
138
140
  const existing = this.getByPathname(pathname, locale);
139
141
  if (existing && existing.id !== excludeId) {