@nuasite/cms-core 0.64.2 → 0.65.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/asset-paths.d.ts.map +1 -0
- package/dist/asset-paths.js +55 -0
- package/dist/astro-source-analysis.d.ts.map +1 -0
- package/dist/astro-source-analysis.js +503 -0
- package/dist/byte-range.d.ts.map +1 -0
- package/dist/byte-range.js +31 -0
- package/dist/{types/check-entries.d.ts → check-entries.d.ts} +3 -3
- package/dist/check-entries.d.ts.map +1 -0
- package/dist/check-entries.js +98 -0
- package/dist/{types/check-live.d.ts → check-live.d.ts} +4 -4
- package/dist/check-live.d.ts.map +1 -0
- package/dist/check-live.js +49 -0
- package/dist/{types/check-shape.d.ts → check-shape.d.ts} +4 -4
- package/dist/check-shape.d.ts.map +1 -0
- package/dist/check-shape.js +119 -0
- package/dist/{types/check-write.d.ts → check-write.d.ts} +4 -4
- package/dist/check-write.d.ts.map +1 -0
- package/dist/check-write.js +273 -0
- package/dist/{types/check.d.ts → check.d.ts} +2 -2
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +366 -0
- package/dist/{types/collection-scanner.d.ts → collection-scanner.d.ts} +2 -2
- package/dist/collection-scanner.d.ts.map +1 -0
- package/dist/collection-scanner.js +1483 -0
- package/dist/{types/component-registry.d.ts → component-registry.d.ts} +1 -1
- package/dist/component-registry.d.ts.map +1 -0
- package/dist/component-registry.js +271 -0
- package/dist/{types/content-config-ast.d.ts → content-config-ast.d.ts} +1 -1
- package/dist/content-config-ast.d.ts.map +1 -0
- package/dist/content-config-ast.js +1614 -0
- package/dist/{types/core.d.ts → core.d.ts} +3 -3
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +94 -0
- package/dist/editor-write-model.d.ts.map +1 -0
- package/dist/editor-write-model.js +101 -0
- package/dist/fs/glob.d.ts.map +1 -0
- package/dist/fs/glob.js +79 -0
- package/dist/{types/fs → fs}/node-fs.d.ts +1 -1
- package/dist/fs/node-fs.d.ts.map +1 -0
- package/dist/fs/node-fs.js +199 -0
- package/dist/{types/fs → fs}/staged.d.ts +1 -1
- package/dist/fs/staged.d.ts.map +1 -0
- package/dist/fs/staged.js +203 -0
- package/dist/fs/types.d.ts.map +1 -0
- package/dist/fs/types.js +0 -0
- package/dist/{types/handlers → handlers}/entry-ops.d.ts +2 -2
- package/dist/handlers/entry-ops.d.ts.map +1 -0
- package/dist/handlers/entry-ops.js +846 -0
- package/dist/{types/handlers → handlers}/page-ops.d.ts +1 -1
- package/dist/handlers/page-ops.d.ts.map +1 -0
- package/dist/handlers/page-ops.js +165 -0
- package/dist/{types/handlers → handlers}/redirect-ops.d.ts +1 -1
- package/dist/handlers/redirect-ops.d.ts.map +1 -0
- package/dist/handlers/redirect-ops.js +111 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/media/contember.d.ts.map +1 -0
- package/dist/media/contember.js +79 -0
- package/dist/media/errors.d.ts.map +1 -0
- package/dist/media/errors.js +22 -0
- package/dist/{types/media → media}/file-system.d.ts +1 -1
- package/dist/media/file-system.d.ts.map +1 -0
- package/dist/media/file-system.js +124 -0
- package/dist/{types/media → media}/index.d.ts +8 -8
- package/dist/media/index.d.ts.map +1 -0
- package/dist/media/index.js +8 -0
- package/dist/media/local.d.ts.map +1 -0
- package/dist/media/local.js +209 -0
- package/dist/media/mime.d.ts.map +1 -0
- package/dist/media/mime.js +21 -0
- package/dist/media/names.d.ts.map +1 -0
- package/dist/media/names.js +23 -0
- package/dist/{types/media → media}/project-images.d.ts +1 -1
- package/dist/media/project-images.d.ts.map +1 -0
- package/dist/media/project-images.js +99 -0
- package/dist/media/s3.d.ts.map +1 -0
- package/dist/media/s3.js +160 -0
- package/dist/media/type-filter.d.ts.map +1 -0
- package/dist/media/type-filter.js +164 -0
- package/dist/{types/portable.d.ts → portable.d.ts} +25 -25
- package/dist/portable.d.ts.map +1 -0
- package/dist/portable.js +27 -0
- package/dist/{types/project-config-ast.d.ts → project-config-ast.d.ts} +1 -1
- package/dist/project-config-ast.d.ts.map +1 -0
- package/dist/project-config-ast.js +324 -0
- package/dist/public-static-file.d.ts.map +1 -0
- package/dist/public-static-file.js +40 -0
- package/dist/redirects.d.ts.map +1 -0
- package/dist/redirects.js +185 -0
- package/dist/route-scan.d.ts.map +1 -0
- package/dist/route-scan.js +23 -0
- package/dist/schema-port.d.ts.map +1 -0
- package/dist/schema-port.js +30 -0
- package/dist/{types/schema-probe.d.ts → schema-probe.d.ts} +2 -2
- package/dist/schema-probe.d.ts.map +1 -0
- package/dist/schema-probe.js +119 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +163 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +12 -12
- package/src/check-entries.ts +4 -4
- package/src/check-live.ts +4 -4
- package/src/check-shape.ts +5 -5
- package/src/check-write.ts +6 -6
- package/src/check.ts +15 -9
- package/src/collection-scanner.ts +3 -3
- package/src/component-registry.ts +1 -1
- package/src/content-config-ast.ts +1 -1
- package/src/core.ts +8 -8
- package/src/editor-write-model.ts +1 -1
- package/src/fs/node-fs.ts +2 -2
- package/src/fs/staged.ts +6 -3
- package/src/handlers/entry-ops.ts +7 -7
- package/src/handlers/page-ops.ts +2 -2
- package/src/handlers/redirect-ops.ts +2 -2
- package/src/index.ts +4 -4
- package/src/media/contember.ts +1 -1
- package/src/media/file-system.ts +4 -4
- package/src/media/index.ts +8 -8
- package/src/media/local.ts +3 -3
- package/src/media/project-images.ts +2 -2
- package/src/media/s3.ts +3 -3
- package/src/portable.ts +25 -25
- package/src/project-config-ast.ts +1 -1
- package/src/route-scan.ts +1 -1
- package/src/schema-probe.ts +2 -2
- package/src/tsconfig.json +5 -1
- package/dist/types/asset-paths.d.ts.map +0 -1
- package/dist/types/astro-source-analysis.d.ts.map +0 -1
- package/dist/types/byte-range.d.ts.map +0 -1
- package/dist/types/check-entries.d.ts.map +0 -1
- package/dist/types/check-live.d.ts.map +0 -1
- package/dist/types/check-shape.d.ts.map +0 -1
- package/dist/types/check-write.d.ts.map +0 -1
- package/dist/types/check.d.ts.map +0 -1
- package/dist/types/collection-scanner.d.ts.map +0 -1
- package/dist/types/component-registry.d.ts.map +0 -1
- package/dist/types/content-config-ast.d.ts.map +0 -1
- package/dist/types/core.d.ts.map +0 -1
- package/dist/types/editor-write-model.d.ts.map +0 -1
- package/dist/types/fs/glob.d.ts.map +0 -1
- package/dist/types/fs/node-fs.d.ts.map +0 -1
- package/dist/types/fs/staged.d.ts.map +0 -1
- package/dist/types/fs/types.d.ts.map +0 -1
- package/dist/types/handlers/entry-ops.d.ts.map +0 -1
- package/dist/types/handlers/page-ops.d.ts.map +0 -1
- package/dist/types/handlers/redirect-ops.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -5
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/media/contember.d.ts.map +0 -1
- package/dist/types/media/errors.d.ts.map +0 -1
- package/dist/types/media/file-system.d.ts.map +0 -1
- package/dist/types/media/index.d.ts.map +0 -1
- package/dist/types/media/local.d.ts.map +0 -1
- package/dist/types/media/mime.d.ts.map +0 -1
- package/dist/types/media/names.d.ts.map +0 -1
- package/dist/types/media/project-images.d.ts.map +0 -1
- package/dist/types/media/s3.d.ts.map +0 -1
- package/dist/types/media/type-filter.d.ts.map +0 -1
- package/dist/types/portable.d.ts.map +0 -1
- package/dist/types/project-config-ast.d.ts.map +0 -1
- package/dist/types/public-static-file.d.ts.map +0 -1
- package/dist/types/redirects.d.ts.map +0 -1
- package/dist/types/route-scan.d.ts.map +0 -1
- package/dist/types/schema-port.d.ts.map +0 -1
- package/dist/types/schema-probe.d.ts.map +0 -1
- package/dist/types/shared.d.ts.map +0 -1
- package/dist/types/tsconfig.tsbuildinfo +0 -1
- /package/dist/{types/asset-paths.d.ts → asset-paths.d.ts} +0 -0
- /package/dist/{types/astro-source-analysis.d.ts → astro-source-analysis.d.ts} +0 -0
- /package/dist/{types/byte-range.d.ts → byte-range.d.ts} +0 -0
- /package/dist/{types/editor-write-model.d.ts → editor-write-model.d.ts} +0 -0
- /package/dist/{types/fs → fs}/glob.d.ts +0 -0
- /package/dist/{types/fs → fs}/types.d.ts +0 -0
- /package/dist/{types/media → media}/contember.d.ts +0 -0
- /package/dist/{types/media → media}/errors.d.ts +0 -0
- /package/dist/{types/media → media}/local.d.ts +0 -0
- /package/dist/{types/media → media}/mime.d.ts +0 -0
- /package/dist/{types/media → media}/names.d.ts +0 -0
- /package/dist/{types/media → media}/s3.d.ts +0 -0
- /package/dist/{types/media → media}/type-filter.d.ts +0 -0
- /package/dist/{types/public-static-file.d.ts → public-static-file.d.ts} +0 -0
- /package/dist/{types/redirects.d.ts → redirects.d.ts} +0 -0
- /package/dist/{types/route-scan.d.ts → route-scan.d.ts} +0 -0
- /package/dist/{types/schema-port.d.ts → schema-port.d.ts} +0 -0
- /package/dist/{types/shared.d.ts → shared.d.ts} +0 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read every declared collection off disk once.
|
|
3
|
+
*
|
|
4
|
+
* Split out of `check.ts` because more than one checker needs the same material: the
|
|
5
|
+
* type/reference rules there, and the live-schema rules that re-parse each entry against
|
|
6
|
+
* the project's real Zod schema. Loading twice would double the I/O on a 1.5k-entry site
|
|
7
|
+
* and let the two disagree about which files even belong to a collection.
|
|
8
|
+
*/
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import yaml from 'yaml';
|
|
11
|
+
import { UnreadableDirectoryError } from "./fs/glob.js";
|
|
12
|
+
const DATA_EXTENSIONS = new Set(['.json', '.yaml', '.yml']);
|
|
13
|
+
/** `./content/articles` and `content/articles/` both mean the same directory. */
|
|
14
|
+
export const normalizeBase = (base) => path.normalize(base).replace(/^\.\//, '').replace(/\/+$/, '');
|
|
15
|
+
/** The id Astro's `glob()` loader derives from a file, and therefore what a `reference()` must hold. */
|
|
16
|
+
export const entryStem = (file) => path.basename(file).replace(/\.(md|mdx|json|ya?ml)$/, '');
|
|
17
|
+
export function isPlainObject(value) {
|
|
18
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) && !(value instanceof Date);
|
|
19
|
+
}
|
|
20
|
+
async function collectEntryFiles(fs, collection, base) {
|
|
21
|
+
const pattern = collection.loaderPattern ?? '**/*.{md,mdx,json,yaml,yml}';
|
|
22
|
+
const files = await fs.glob(`${base}/${pattern}`);
|
|
23
|
+
return files.filter(file => !path.basename(file).startsWith('_')).sort();
|
|
24
|
+
}
|
|
25
|
+
export function parseEntry(file, raw) {
|
|
26
|
+
const extension = path.extname(file);
|
|
27
|
+
if (DATA_EXTENSIONS.has(extension)) {
|
|
28
|
+
try {
|
|
29
|
+
const parsed = extension === '.json' ? JSON.parse(raw) : yaml.parse(raw);
|
|
30
|
+
// A data collection file may legitimately hold an array of entries; nothing to field-check then.
|
|
31
|
+
return { frontmatter: isPlainObject(parsed) ? parsed : {} };
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
return { error: error instanceof Error ? error.message.split('\n')[0] : String(error) };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const trimmed = raw.trimStart();
|
|
38
|
+
if (!trimmed.startsWith('---'))
|
|
39
|
+
return { frontmatter: {} };
|
|
40
|
+
const lines = trimmed.split('\n');
|
|
41
|
+
const end = lines.findIndex((line, index) => index > 0 && line.trimEnd() === '---');
|
|
42
|
+
if (end === -1)
|
|
43
|
+
return { error: 'frontmatter is never closed (missing the second `---`)' };
|
|
44
|
+
try {
|
|
45
|
+
const parsed = yaml.parse(lines.slice(1, end).join('\n').trim());
|
|
46
|
+
return { frontmatter: isPlainObject(parsed) ? parsed : {} };
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
return { error: error instanceof Error ? error.message.split('\n')[0] : String(error) };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Whether a collection holds markdown entries or plain data files.
|
|
54
|
+
*
|
|
55
|
+
* Decides what a create writes, so it reads the entries on disk first and only falls back to
|
|
56
|
+
* the loader pattern for a collection that has none yet.
|
|
57
|
+
*/
|
|
58
|
+
export function collectionKind(collection, loaderPattern) {
|
|
59
|
+
const extensions = collection.entries.map(entry => path.extname(entry.file));
|
|
60
|
+
if (extensions.length > 0)
|
|
61
|
+
return extensions.some(extension => extension === '.md' || extension === '.mdx') ? 'markdown' : 'data';
|
|
62
|
+
return loaderPattern === undefined || /\bmdx?\b/.test(loaderPattern) ? 'markdown' : 'data';
|
|
63
|
+
}
|
|
64
|
+
/** Every declared collection's files, read and parsed. Reports nothing — the callers judge. */
|
|
65
|
+
export async function loadCollections(fs, config) {
|
|
66
|
+
const loaded = new Map();
|
|
67
|
+
for (const [name, collection] of config) {
|
|
68
|
+
const base = normalizeBase(collection.loaderBase ?? `src/content/${name}`);
|
|
69
|
+
if (!(await fs.exists(base))) {
|
|
70
|
+
loaded.set(name, { name, base, exists: false, entries: [] });
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
let files;
|
|
74
|
+
try {
|
|
75
|
+
files = await collectEntryFiles(fs, collection, base);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
// Anything else (a path traversal, a bug) is not a fact about the content: let it surface.
|
|
79
|
+
if (!(error instanceof UnreadableDirectoryError))
|
|
80
|
+
throw error;
|
|
81
|
+
// A directory under the base that cannot be read. Which entries it holds is unknown,
|
|
82
|
+
// so the collection is handed on as unreadable instead of as whatever was listed so far.
|
|
83
|
+
loaded.set(name, { name, base, exists: true, unreadable: error.message, entries: [] });
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const entries = [];
|
|
87
|
+
for (const file of files) {
|
|
88
|
+
const parsed = parseEntry(file, await fs.readFile(file));
|
|
89
|
+
entries.push({
|
|
90
|
+
file,
|
|
91
|
+
stem: entryStem(file),
|
|
92
|
+
...('error' in parsed ? { parseError: parsed.error } : { frontmatter: parsed.frontmatter }),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
loaded.set(name, { name, base, exists: true, entries });
|
|
96
|
+
}
|
|
97
|
+
return loaded;
|
|
98
|
+
}
|
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
* spot: it carries refinements, unions, `.min()`, coercions — everything the build will
|
|
7
7
|
* actually apply. This is the same verdict `astro sync` would give, without the build.
|
|
8
8
|
*/
|
|
9
|
-
import type {
|
|
10
|
-
import type {
|
|
11
|
-
import type { ParsedConfig } from './content-config-ast';
|
|
12
|
-
import { type LiveSchemas } from './schema-port';
|
|
9
|
+
import type { LoadedCollections } from './check-entries.ts';
|
|
10
|
+
import type { CheckFinding } from './check.ts';
|
|
11
|
+
import type { ParsedConfig } from './content-config-ast.ts';
|
|
12
|
+
import { type LiveSchemas } from './schema-port.ts';
|
|
13
13
|
export interface LiveCheckInput {
|
|
14
14
|
config: ParsedConfig;
|
|
15
15
|
collections: LoadedCollections;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check-live.d.ts","sourceRoot":"","sources":["../src/check-live.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAC3D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAC3D,OAAO,EAAuC,KAAK,WAAW,EAAa,MAAM,kBAAkB,CAAA;AAEnG,MAAM,WAAW,cAAc;IAC9B,MAAM,EAAE,YAAY,CAAA;IACpB,WAAW,EAAE,iBAAiB,CAAA;IAC9B,OAAO,EAAE,WAAW,CAAA;CACpB;AAED;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,CAkCxF"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validate what is on disk against the project's real collection schemas.
|
|
3
|
+
*
|
|
4
|
+
* The AST rules in `check.ts` only type-check fields the content config types explicitly,
|
|
5
|
+
* and deliberately stay silent on a plain `z.string()`. A live schema has no such blind
|
|
6
|
+
* spot: it carries refinements, unions, `.min()`, coercions — everything the build will
|
|
7
|
+
* actually apply. This is the same verdict `astro sync` would give, without the build.
|
|
8
|
+
*/
|
|
9
|
+
import { describeIssue, schemaFor } from "./schema-port.js";
|
|
10
|
+
/**
|
|
11
|
+
* One finding per rejected value, with the field path zod reported.
|
|
12
|
+
*
|
|
13
|
+
* Entries whose frontmatter did not parse are skipped — `check.ts` already reports those,
|
|
14
|
+
* and there is nothing to hand a schema.
|
|
15
|
+
*/
|
|
16
|
+
export async function checkAgainstSchemas(input) {
|
|
17
|
+
const findings = [];
|
|
18
|
+
for (const collection of input.collections.values()) {
|
|
19
|
+
const schema = schemaFor(input.schemas, collection.name);
|
|
20
|
+
// No live schema for this collection means no opinion, not a pass.
|
|
21
|
+
if (!schema)
|
|
22
|
+
continue;
|
|
23
|
+
for (const entry of collection.entries) {
|
|
24
|
+
if (entry.frontmatter === undefined)
|
|
25
|
+
continue;
|
|
26
|
+
let result;
|
|
27
|
+
try {
|
|
28
|
+
result = await schema.safeParse(entry.frontmatter);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
// An injected schema that throws must not take down a report of real problems.
|
|
32
|
+
findings.push({
|
|
33
|
+
severity: 'error',
|
|
34
|
+
code: 'entry/schema-rejected',
|
|
35
|
+
file: entry.file,
|
|
36
|
+
message: `The collection schema threw while validating this entry: ${error instanceof Error ? error.message : String(error)}`,
|
|
37
|
+
});
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (result.success)
|
|
41
|
+
continue;
|
|
42
|
+
// Every issue, not just the first — reporting the whole entry in one pass is the point.
|
|
43
|
+
for (const issue of result.issues) {
|
|
44
|
+
findings.push({ severity: 'error', code: 'entry/schema-rejected', file: entry.file, ...describeIssue(issue) });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return findings;
|
|
49
|
+
}
|
|
@@ -15,10 +15,10 @@
|
|
|
15
15
|
* type fallback is decided per top-level field, so a nested finding would name a consequence
|
|
16
16
|
* that does not follow.
|
|
17
17
|
*/
|
|
18
|
-
import type {
|
|
19
|
-
import type {
|
|
20
|
-
import type { ParsedConfig } from './content-config-ast';
|
|
21
|
-
import { type LiveSchemas } from './schema-port';
|
|
18
|
+
import type { LoadedCollections } from './check-entries.ts';
|
|
19
|
+
import type { CheckFinding } from './check.ts';
|
|
20
|
+
import type { ParsedConfig } from './content-config-ast.ts';
|
|
21
|
+
import { type LiveSchemas } from './schema-port.ts';
|
|
22
22
|
export interface ShapeCheckInput {
|
|
23
23
|
config: ParsedConfig;
|
|
24
24
|
collections: LoadedCollections;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check-shape.d.ts","sourceRoot":"","sources":["../src/check-shape.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAe,MAAM,oBAAoB,CAAA;AACxE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,KAAK,EAAE,YAAY,EAAe,MAAM,yBAAyB,CAAA;AACxE,OAAO,EAAE,KAAK,WAAW,EAAa,MAAM,kBAAkB,CAAA;AAG9D,MAAM,WAAW,eAAe;IAC/B,MAAM,EAAE,YAAY,CAAA;IACpB,WAAW,EAAE,iBAAiB,CAAA;IAC9B,OAAO,EAAE,WAAW,CAAA;CACpB;AAsFD,wBAAsB,gBAAgB,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,CAsBtF"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the config parser's picture of a field and the live schema's picture disagree.
|
|
3
|
+
*
|
|
4
|
+
* The editor is built from the parser's picture: what it renders, and what it refuses to save,
|
|
5
|
+
* both come from `parseContentConfig`. The build judges by the live schema. Wherever those two
|
|
6
|
+
* disagree the editor is wrong about the project in a way neither side can notice on its own —
|
|
7
|
+
* the config looks fine, the content parses, and the editor still does the wrong thing.
|
|
8
|
+
*
|
|
9
|
+
* Two disagreements are reported here. Both are held to the same standard as the rest of
|
|
10
|
+
* `check.ts`: the evidence comes from asking the schema (see `schema-probe.ts`), never from
|
|
11
|
+
* assuming the parser's silence means anything.
|
|
12
|
+
*
|
|
13
|
+
* Both are limited to top-level fields, because that is the limit of what they can honestly
|
|
14
|
+
* claim. The write guard in `handlers/entry-ops.ts` stops at the top level and the editor's
|
|
15
|
+
* type fallback is decided per top-level field, so a nested finding would name a consequence
|
|
16
|
+
* that does not follow.
|
|
17
|
+
*/
|
|
18
|
+
import { schemaFor } from "./schema-port.js";
|
|
19
|
+
import { acceptsMissing, firstAcceptedEntry, governsPath, pointAt, rejectsValueAt } from "./schema-probe.js";
|
|
20
|
+
/** These findings are about the schema, not about any one entry — `check.ts` names the same file for its config findings. */
|
|
21
|
+
const CONFIG_FILE = 'src/content.config.ts';
|
|
22
|
+
/**
|
|
23
|
+
* Strings diverse enough that anything still refusing all of them refuses text as a kind.
|
|
24
|
+
*
|
|
25
|
+
* One string proves nothing: `z.string().url()` refuses `'nua'` and is perfectly usable through
|
|
26
|
+
* a text input, so reporting on a single rejection would fire on every constrained string field
|
|
27
|
+
* in the project. A number, a date, a boolean and an enum refuse the whole set.
|
|
28
|
+
*/
|
|
29
|
+
const TEXT_PROBES = ['', 'nua', 'https://nua.test/probe', '2024-01-02', '123'];
|
|
30
|
+
/** Whether any entry gives the collection scanner a value to infer this field's type from. */
|
|
31
|
+
const anyEntryHasValue = (entries, field) => entries.some(entry => entry.frontmatter !== undefined && entry.frontmatter[field] !== undefined);
|
|
32
|
+
/**
|
|
33
|
+
* The editor forces a field the build does not need.
|
|
34
|
+
*
|
|
35
|
+
* `blankRequiredFields` refuses to save an entry whose required field is blank, and it takes
|
|
36
|
+
* `required` from the parser. When the schema accepts the record without that key, the editor is
|
|
37
|
+
* blocking an edit the build would have taken.
|
|
38
|
+
*
|
|
39
|
+
* Only this direction is reported. The opposite one — parser says optional, schema insists — has
|
|
40
|
+
* a consequence that `cms/empty-write` states precisely (the create it breaks), and saying it
|
|
41
|
+
* twice under two codes makes one defect look like two.
|
|
42
|
+
*/
|
|
43
|
+
async function requiredDrift(name, field, seed, schemas) {
|
|
44
|
+
const schema = schemaFor(schemas, name);
|
|
45
|
+
// `blankRequiredFields` skips hidden fields, so an over-strict parser costs nothing there.
|
|
46
|
+
if (!schema || !field.required || field.layout?.hidden)
|
|
47
|
+
return null;
|
|
48
|
+
const point = pointAt(seed, field.name);
|
|
49
|
+
if (!await acceptsMissing(schema, point))
|
|
50
|
+
return null;
|
|
51
|
+
// Without this, a `passthrough()` object — which accepts the record with any key removed —
|
|
52
|
+
// would put every field it never declared on the report.
|
|
53
|
+
if (!await governsPath(schema, point))
|
|
54
|
+
return null;
|
|
55
|
+
return {
|
|
56
|
+
severity: 'warning',
|
|
57
|
+
code: 'cms/required-drift',
|
|
58
|
+
file: CONFIG_FILE,
|
|
59
|
+
field: `${name}.${field.name}`,
|
|
60
|
+
message: `The editor treats "${name}.${field.name}" as required and refuses to save it blank, but the schema accepts an entry without it.`,
|
|
61
|
+
hint: 'Either the schema should require it, or the config should show the parser that it is optional. Wrapping a field schema in a helper call (`field.image(z.string().optional())`) or reaching it through an import hides the `.optional()` from the parser, which then reads the field as required — put the wrapper outside, as `field.image(z.string()).optional()`.',
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The editor will render a plain text input for a field that cannot hold text.
|
|
66
|
+
*
|
|
67
|
+
* When the parser pins no type, the collection scanner falls back to whatever it can infer from
|
|
68
|
+
* existing values — and to `'text'` when there are none (`parsedFieldToFieldDefinition`). A text
|
|
69
|
+
* input on a number, a date or an enum writes a string the build refuses, so the first editor to
|
|
70
|
+
* fill that field breaks it.
|
|
71
|
+
*
|
|
72
|
+
* The three guards are what keep this off projects that are fine: a hidden field is never
|
|
73
|
+
* rendered at all (`field-utils.ts`, `entry-create.tsx`, `entry-editor.tsx` all drop it), a field
|
|
74
|
+
* some entry already carries is typed by inference and never falls back, and a schema that takes
|
|
75
|
+
* any of `TEXT_PROBES` is one a text input can serve.
|
|
76
|
+
*/
|
|
77
|
+
async function degradedField(name, field, seed, input) {
|
|
78
|
+
const schema = schemaFor(input.schemas, name);
|
|
79
|
+
if (!schema || field.type !== undefined || field.layout?.hidden)
|
|
80
|
+
return null;
|
|
81
|
+
if (anyEntryHasValue(input.collections.get(name)?.entries ?? [], field.name))
|
|
82
|
+
return null;
|
|
83
|
+
const point = pointAt(seed, field.name);
|
|
84
|
+
for (const text of TEXT_PROBES) {
|
|
85
|
+
if (!await rejectsValueAt(schema, point, text))
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
severity: 'error',
|
|
90
|
+
code: 'cms/field-degraded',
|
|
91
|
+
file: CONFIG_FILE,
|
|
92
|
+
field: `${name}.${field.name}`,
|
|
93
|
+
message: `The editor will render "${name}.${field.name}" as a plain text input — the parser pinned no type and no entry supplies one — and the schema rejects text there.`,
|
|
94
|
+
hint: "Write the field's type chain inline in the content config (`n.number()`, `n.date()`, `n.enum([…])`). A type reached through an import, a helper with arguments, or a modifier applied after the object is built is invisible to the parser, which then leaves the editor guessing.",
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
export async function checkFieldShapes(input) {
|
|
98
|
+
const findings = [];
|
|
99
|
+
// Sequential on purpose: probes resolve at different speeds and the report has to read the same every run.
|
|
100
|
+
for (const [name, collection] of input.config) {
|
|
101
|
+
const schema = schemaFor(input.schemas, name);
|
|
102
|
+
if (!schema || collection.fields.length === 0)
|
|
103
|
+
continue;
|
|
104
|
+
// No accepted entry, no evidence. `cms/empty-write-unchecked` already says so for this
|
|
105
|
+
// collection, and repeating it once per field would bury the findings that mean something.
|
|
106
|
+
const seed = await firstAcceptedEntry(schema, input.collections.get(name)?.entries ?? []);
|
|
107
|
+
if (!seed)
|
|
108
|
+
continue;
|
|
109
|
+
for (const field of collection.fields) {
|
|
110
|
+
const drift = await requiredDrift(name, field, seed, input.schemas);
|
|
111
|
+
if (drift)
|
|
112
|
+
findings.push(drift);
|
|
113
|
+
const degraded = await degradedField(name, field, seed, input);
|
|
114
|
+
if (degraded)
|
|
115
|
+
findings.push(degraded);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return findings;
|
|
119
|
+
}
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
* `handlers/entry-ops.ts` does before serializing. Predicting the pre-write record would
|
|
28
28
|
* report a failure the server no longer allows to happen.
|
|
29
29
|
*/
|
|
30
|
-
import type
|
|
31
|
-
import {
|
|
32
|
-
import type { ParsedConfig } from './content-config-ast';
|
|
33
|
-
import { type LiveSchemas } from './schema-port';
|
|
30
|
+
import { type LoadedCollections } from './check-entries.ts';
|
|
31
|
+
import type { CheckFinding } from './check.ts';
|
|
32
|
+
import type { ParsedConfig } from './content-config-ast.ts';
|
|
33
|
+
import { type LiveSchemas } from './schema-port.ts';
|
|
34
34
|
export interface WriteCheckInput {
|
|
35
35
|
config: ParsedConfig;
|
|
36
36
|
collections: LoadedCollections;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check-write.d.ts","sourceRoot":"","sources":["../src/check-write.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAGH,OAAO,EAAwD,KAAK,iBAAiB,EAAoB,MAAM,oBAAoB,CAAA;AACnI,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,KAAK,EAAE,YAAY,EAAe,MAAM,yBAAyB,CAAA;AAYxE,OAAO,EAAkD,KAAK,WAAW,EAAa,MAAM,kBAAkB,CAAA;AAG9G,MAAM,WAAW,eAAe;IAC/B,MAAM,EAAE,YAAY,CAAA;IACpB,WAAW,EAAE,iBAAiB,CAAA;IAC9B,OAAO,EAAE,WAAW,CAAA;IACpB,4FAA4F;IAC5F,KAAK,CAAC,EAAE,MAAM,IAAI,CAAA;CAClB;AA2JD,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,CAmHvF"}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Predict the writes the editor is about to make, and whether the schema will take them.
|
|
3
|
+
*
|
|
4
|
+
* Everything else here validates content that already exists. This runs before the mistake:
|
|
5
|
+
* it builds the exact frontmatter the CMS would write for an editorial action and parses it
|
|
6
|
+
* against the live schema. A rejection is a build that will break the next time someone
|
|
7
|
+
* performs that action — not a guess, since the write shape comes from
|
|
8
|
+
* `editor-write-model.ts`, which the editor itself uses.
|
|
9
|
+
*
|
|
10
|
+
* Two actions are simulated:
|
|
11
|
+
*
|
|
12
|
+
* - **new entry** — `newEntryFrontmatter` then `omitEmptyOnCreate`, then the keys the create
|
|
13
|
+
* route itself injects (`applyCreateRouteFields`), then what `createEntry` normalizes away
|
|
14
|
+
* (`normalizeCreateWrite`). Note what this means: a field left empty
|
|
15
|
+
* arrives *absent*, not as `''`, so the failure to look for is a missing required value.
|
|
16
|
+
* Where `blankRequiredFields` says the write guard in `handlers/entry-ops.ts` would reject
|
|
17
|
+
* the create before it reaches disk, there is no finding to make — with the exception of
|
|
18
|
+
* `hidden` fields, which that guard skips.
|
|
19
|
+
* - **"+ Add" in a list** — an object-array field gains an item built by `newRepeaterItem`,
|
|
20
|
+
* which seeds the keys the config declares required and omits the rest. What survives is a
|
|
21
|
+
* required key the config does not know about: `addArrayItem` seeds from the same config the
|
|
22
|
+
* editor renders from, so neither can supply a key neither can see. A list of scalars —
|
|
23
|
+
* `z.array(reference('tags'))` and friends — is simulated too: it appends `blankFieldValue`
|
|
24
|
+
* for the item type, which is what every editor's "+ Add" does.
|
|
25
|
+
*
|
|
26
|
+
* Both simulated writes end with `withoutBlankArrayItems`, because that is the last thing
|
|
27
|
+
* `handlers/entry-ops.ts` does before serializing. Predicting the pre-write record would
|
|
28
|
+
* report a failure the server no longer allows to happen.
|
|
29
|
+
*/
|
|
30
|
+
import { collectionKind, isPlainObject } from "./check-entries.js";
|
|
31
|
+
import { applyCreateRouteFields, blankFieldValue, blankRequiredFields, newEntryFrontmatter, newRepeaterItem, normalizeCreateWrite, omitEmptyOnCreate, withoutBlankArrayItems, } from "./editor-write-model.js";
|
|
32
|
+
import { describeIssue, schemaFor } from "./schema-port.js";
|
|
33
|
+
import { firstAcceptedEntry } from "./schema-probe.js";
|
|
34
|
+
/** These findings are about the schema, not about any one entry — `check.ts` names the same file for its config findings. */
|
|
35
|
+
const CONFIG_FILE = 'src/content.config.ts';
|
|
36
|
+
/** `parseContentConfig` reports `hidden` under `layout`; the write model wants it at the top level. */
|
|
37
|
+
const toWriteModelField = (field) => ({ name: field.name, type: field.type, hidden: field.layout?.hidden });
|
|
38
|
+
const errorText = (error) => (error instanceof Error ? error.message : String(error));
|
|
39
|
+
async function parseWrite(schema, value) {
|
|
40
|
+
try {
|
|
41
|
+
const result = await schema.safeParse(value);
|
|
42
|
+
return { issues: result.success ? [] : result.issues };
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
// A refinement that dereferences a field this simulation deliberately omits throws
|
|
46
|
+
// rather than returning issues. One project doing that must not cost the whole report.
|
|
47
|
+
return { threw: errorText(error) };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The value the simulated write carries at `path`, if it carries one.
|
|
52
|
+
*
|
|
53
|
+
* The remedy hinges entirely on this. A key the write omits is fixed by making the schema
|
|
54
|
+
* accept its absence; a key the write fills with a value the schema refuses cannot be, and
|
|
55
|
+
* suggesting `.optional()` there would send the reader down a dead end. Reading the record we
|
|
56
|
+
* actually parsed is what tells the two apart — the issue's own code cannot, since a rejected
|
|
57
|
+
* `''` and a missing key both arrive as `invalid_type`.
|
|
58
|
+
*/
|
|
59
|
+
function valueAt(record, path) {
|
|
60
|
+
let current = record;
|
|
61
|
+
for (const segment of path) {
|
|
62
|
+
if (Array.isArray(current)) {
|
|
63
|
+
const index = typeof segment === 'number' ? segment : Number(segment);
|
|
64
|
+
if (!Number.isInteger(index) || index < 0 || index >= current.length)
|
|
65
|
+
return { present: false };
|
|
66
|
+
current = current[index];
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (!isPlainObject(current) || !Object.hasOwn(current, String(segment)))
|
|
70
|
+
return { present: false };
|
|
71
|
+
current = current[String(segment)];
|
|
72
|
+
}
|
|
73
|
+
return current === undefined ? { present: false } : { present: true, value: current };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* What to change, given which action wrote the value and whether it wrote one at all.
|
|
77
|
+
*
|
|
78
|
+
* `.nullable()` is called out by name in both absent cases because it is the wrong answer that
|
|
79
|
+
* looks right: it accepts `null`, the editor writes no key at all, and the check keeps failing.
|
|
80
|
+
*/
|
|
81
|
+
function hintFor(action, found) {
|
|
82
|
+
if (action === 'create') {
|
|
83
|
+
if (!found.present) {
|
|
84
|
+
return 'The create form leaves an untouched field out of the write entirely. `.optional()` or `.default(…)` accepts a missing key; `.nullable()` does not — it accepts `null`, not absence.';
|
|
85
|
+
}
|
|
86
|
+
return `The create form starts this field at ${JSON.stringify(found.value)} and nothing makes the editor change it before saving. Either the schema accepts that value, or mark the field \`hidden\` so the write omits it and let \`.default(…)\` supply one.`;
|
|
87
|
+
}
|
|
88
|
+
if (!found.present) {
|
|
89
|
+
return 'The appended item does not carry this key — "+ Add" seeds only the item fields the config declares required. Declaring it required there is what makes the editor seed it; if it is not required, `.optional()` or `.default(…)` says so to the schema, and `.nullable()` does not — it accepts `null`, not absence.';
|
|
90
|
+
}
|
|
91
|
+
return `The appended item carries ${JSON.stringify(found.value)} for this key, and it is written the moment "+ Add" is clicked. \`.default(…)\` on the inner field is the way out: the parser then reads the field as optional, so the item leaves the key out and the schema supplies the value itself.`;
|
|
92
|
+
}
|
|
93
|
+
/** One rejected write, phrased as the editorial action that causes it. `action` reads as the subject of the sentence. */
|
|
94
|
+
function rejectionFinding(action, issue, kind, written) {
|
|
95
|
+
const { field, message } = describeIssue(issue);
|
|
96
|
+
const finding = {
|
|
97
|
+
severity: 'error',
|
|
98
|
+
code: 'cms/empty-write',
|
|
99
|
+
file: CONFIG_FILE,
|
|
100
|
+
message: `${action} produces a write the schema rejects. ${message}`,
|
|
101
|
+
};
|
|
102
|
+
if (field !== undefined)
|
|
103
|
+
finding.field = field;
|
|
104
|
+
finding.hint = hintFor(kind, valueAt(written, issue.path));
|
|
105
|
+
return finding;
|
|
106
|
+
}
|
|
107
|
+
/** A throw is a verdict too: `astro sync` runs the same schema over the same record and dies the same way. */
|
|
108
|
+
const throwFinding = (action, threw) => ({
|
|
109
|
+
severity: 'error',
|
|
110
|
+
code: 'cms/empty-write',
|
|
111
|
+
file: CONFIG_FILE,
|
|
112
|
+
message: `${action} produces a write the schema throws on: ${threw}`,
|
|
113
|
+
hint: 'A schema that throws instead of returning an issue is usually a refinement reading a field this write does not carry. Guard it against a missing value — `astro sync` runs the same schema over the same record.',
|
|
114
|
+
});
|
|
115
|
+
/**
|
|
116
|
+
* Whether "+ Add" can be clicked on this field at all.
|
|
117
|
+
*
|
|
118
|
+
* `field.type` is not enough on its own: `z.array(reference('tags'))` parses to a `reference`
|
|
119
|
+
* with `isArray`, no `type` and no `itemType`, which is exactly the shape that went unchecked.
|
|
120
|
+
*/
|
|
121
|
+
const isListField = (field) => field.type === 'array' || field.reference?.isArray === true;
|
|
122
|
+
/**
|
|
123
|
+
* The item type of a list of scalars, or `undefined` for a list of objects.
|
|
124
|
+
*
|
|
125
|
+
* A list whose `itemType` the config never pinned is treated as a list of objects, which is
|
|
126
|
+
* what `blankItemsFor`'s keyless fallback is for — guessing `text` there would predict a write
|
|
127
|
+
* nobody makes.
|
|
128
|
+
*/
|
|
129
|
+
function scalarItemType(field) {
|
|
130
|
+
if (field.reference?.isArray)
|
|
131
|
+
return 'reference';
|
|
132
|
+
if (field.type !== 'array')
|
|
133
|
+
return undefined;
|
|
134
|
+
return field.itemType === undefined || field.itemType === 'object' ? undefined : field.itemType;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The item "+ Add" appends to a repeater.
|
|
138
|
+
*
|
|
139
|
+
* Where the schema declares the item's fields, both the editor and `addArrayItem` build it with
|
|
140
|
+
* `newRepeaterItem` — required keys seeded, optional ones omitted — so there is one shape to
|
|
141
|
+
* predict and it comes from the same function the editors call.
|
|
142
|
+
*
|
|
143
|
+
* Where the schema declares nothing, the editors have nothing to build from and fall back to
|
|
144
|
+
* what they can see: `collections-admin` appends an object with no keys, and the in-page editor
|
|
145
|
+
* copies the first item's keys with every value set to `''`, inventing `{ name: '' }` when there
|
|
146
|
+
* is no item to copy (`ArrayOfObjectsField.handleAddItem` in `@nuasite/cms`). "+ Add" is an
|
|
147
|
+
* update, not a create, so `omitEmptyOnCreate` never runs and those `''` really reach disk —
|
|
148
|
+
* which is the failure `{}` alone cannot predict.
|
|
149
|
+
*/
|
|
150
|
+
function blankItemsFor(field, current, today) {
|
|
151
|
+
// A list of scalars has one shape, and it is the one every editor's item widget produces.
|
|
152
|
+
const scalar = scalarItemType(field);
|
|
153
|
+
if (scalar !== undefined)
|
|
154
|
+
return [blankFieldValue({ name: field.name, type: scalar })];
|
|
155
|
+
const itemFields = field.fields ?? [];
|
|
156
|
+
if (itemFields.length > 0) {
|
|
157
|
+
return [newRepeaterItem(itemFields.map(item => ({ ...toWriteModelField(item), required: item.required })), today)];
|
|
158
|
+
}
|
|
159
|
+
// What is already on disk is the only evidence left. `z.array(z.string())` parses with no item
|
|
160
|
+
// type either, and it renders as a row of text inputs whose blank the server drops — so an
|
|
161
|
+
// object is invented only where something says the items are objects. Predicting one anywhere
|
|
162
|
+
// else reports a write no editor makes.
|
|
163
|
+
const first = current[0];
|
|
164
|
+
if (isPlainObject(first))
|
|
165
|
+
return [{}, Object.fromEntries(Object.keys(first).map(key => [key, '']))];
|
|
166
|
+
if (field.itemType !== 'object')
|
|
167
|
+
return [];
|
|
168
|
+
// Declared a repeater, with nothing to copy: the in-page editor invents `{ name: '' }` there.
|
|
169
|
+
// With a scalar sitting in it, it is not what renders this field, and `{}` — what the other
|
|
170
|
+
// editor appends — is the honest stand-in.
|
|
171
|
+
return current.length === 0 ? [{}, { name: '' }] : [{}];
|
|
172
|
+
}
|
|
173
|
+
export async function checkEditorWrites(input) {
|
|
174
|
+
const findings = [];
|
|
175
|
+
const seen = new Set();
|
|
176
|
+
/** `key` identifies the collection and the action; a union schema can report one path twice. */
|
|
177
|
+
const report = (key, finding) => {
|
|
178
|
+
const identity = `${key} ${finding.field ?? ''}`;
|
|
179
|
+
if (seen.has(identity))
|
|
180
|
+
return;
|
|
181
|
+
seen.add(identity);
|
|
182
|
+
findings.push(finding);
|
|
183
|
+
};
|
|
184
|
+
// Sequential on purpose: schemas resolve at different speeds and the report has to read the same every run.
|
|
185
|
+
for (const [name, collection] of input.config) {
|
|
186
|
+
const schema = schemaFor(input.schemas, name);
|
|
187
|
+
if (!schema)
|
|
188
|
+
continue;
|
|
189
|
+
const createKey = `${name} create`;
|
|
190
|
+
// A schema the config AST could not read leaves the collection with no fields, so the
|
|
191
|
+
// simulated create would pre-fill nothing and suppress nothing — every required key the
|
|
192
|
+
// live schema declares would light up on a project that is fine. Say so instead.
|
|
193
|
+
if (collection.fields.length === 0) {
|
|
194
|
+
report(createKey, {
|
|
195
|
+
severity: 'warning',
|
|
196
|
+
code: 'cms/empty-write-unchecked',
|
|
197
|
+
file: CONFIG_FILE,
|
|
198
|
+
field: name,
|
|
199
|
+
message: `Could not check what creating a new entry in "${name}" writes: its schema is not readable from the content config, so there are no fields to predict the write from.`,
|
|
200
|
+
hint: 'Write the field types inline in the content config rather than importing the shape from another module — the prediction reads the config source, and a schema assembled elsewhere leaves it nothing to read.',
|
|
201
|
+
});
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const loaded = input.collections.get(name);
|
|
205
|
+
const entries = loaded?.entries ?? [];
|
|
206
|
+
const kind = loaded ? collectionKind(loaded, collection.loaderPattern) : 'markdown';
|
|
207
|
+
// The route's own keys go on last, because it spreads the form's frontmatter over them.
|
|
208
|
+
// `createEntry` then normalizes what it received, so the prediction does too.
|
|
209
|
+
const created = withoutBlankArrayItems(normalizeCreateWrite(collection.fields, applyCreateRouteFields(omitEmptyOnCreate(newEntryFrontmatter(collection.fields.map(toWriteModelField))), kind, input.today)));
|
|
210
|
+
// What the write guard in `handlers/entry-ops.ts` rejects never reaches disk, so it is
|
|
211
|
+
// nothing to report. The guard runs inside `createEntry`, i.e. on this same record — and
|
|
212
|
+
// it skips `hidden` fields, which is why `blankRequiredFields` never lists one and why a
|
|
213
|
+
// hidden required field survives to below.
|
|
214
|
+
const guarded = new Set(blankRequiredFields(collection.fields, created));
|
|
215
|
+
const createAction = `Creating a new entry in "${name}"`;
|
|
216
|
+
const createVerdict = await parseWrite(schema, created);
|
|
217
|
+
if ('threw' in createVerdict) {
|
|
218
|
+
report(createKey, throwFinding(createAction, createVerdict.threw));
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
for (const issue of createVerdict.issues) {
|
|
222
|
+
const top = issue.path[0];
|
|
223
|
+
if (typeof top === 'string' && guarded.has(top))
|
|
224
|
+
continue;
|
|
225
|
+
report(createKey, rejectionFinding(createAction, issue, 'create', created));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const lists = collection.fields.filter(isListField);
|
|
229
|
+
if (lists.length === 0)
|
|
230
|
+
continue;
|
|
231
|
+
// The record the editor is really sitting on when "+ Add" is clicked. Shared with the shape
|
|
232
|
+
// rules so both mean the same thing by it.
|
|
233
|
+
const seed = await firstAcceptedEntry(schema, entries);
|
|
234
|
+
for (const field of lists) {
|
|
235
|
+
const key = `${name} add:${field.name}`;
|
|
236
|
+
if (!seed) {
|
|
237
|
+
// A simulation that could not run must never read as a pass.
|
|
238
|
+
const hasEntries = entries.some(entry => entry.frontmatter !== undefined);
|
|
239
|
+
const reason = hasEntries
|
|
240
|
+
? 'no existing entry passes the schema, so there is no valid record to add an item to'
|
|
241
|
+
: 'the collection has no entry to add an item to';
|
|
242
|
+
report(key, {
|
|
243
|
+
severity: 'warning',
|
|
244
|
+
code: 'cms/empty-write-unchecked',
|
|
245
|
+
file: CONFIG_FILE,
|
|
246
|
+
field: field.name,
|
|
247
|
+
message: `Could not check what "+ Add" on "${name}.${field.name}" writes: ${reason}.`,
|
|
248
|
+
hint: hasEntries
|
|
249
|
+
? `Fix the \`entry/schema-rejected\` findings in "${name}" and run this again — the prediction needs one entry the schema accepts.`
|
|
250
|
+
: `Add one entry to "${name}" and run this again.`,
|
|
251
|
+
});
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const current = seed[field.name];
|
|
255
|
+
const existing = Array.isArray(current) ? current : [];
|
|
256
|
+
const action = `Clicking "+ Add" on "${name}.${field.name}"`;
|
|
257
|
+
// Both shapes are the same user action, so the dedup key is too: where they fail
|
|
258
|
+
// identically the report says it once — in the words of the keyless item, which is
|
|
259
|
+
// simulated first — and a failure only one of them causes still names "+ Add".
|
|
260
|
+
for (const item of blankItemsFor(field, existing, input.today)) {
|
|
261
|
+
const written = withoutBlankArrayItems({ ...seed, [field.name]: [...existing, item] });
|
|
262
|
+
const verdict = await parseWrite(schema, written);
|
|
263
|
+
if ('threw' in verdict) {
|
|
264
|
+
report(key, throwFinding(action, verdict.threw));
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
for (const issue of verdict.issues)
|
|
268
|
+
report(key, rejectionFinding(action, issue, 'add', written));
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return findings;
|
|
273
|
+
}
|
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
* would trade the one thing this has to get right — no false positives — for coverage that
|
|
12
12
|
* `astro sync` already provides.
|
|
13
13
|
*/
|
|
14
|
-
import type { CmsFileSystem } from './fs/types';
|
|
15
|
-
import { type LiveSchemas } from './schema-port';
|
|
14
|
+
import type { CmsFileSystem } from './fs/types.ts';
|
|
15
|
+
import { type LiveSchemas } from './schema-port.ts';
|
|
16
16
|
export type CheckSeverity = 'error' | 'warning';
|
|
17
17
|
export interface CheckFinding {
|
|
18
18
|
severity: CheckSeverity;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check.d.ts","sourceRoot":"","sources":["../src/check.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAcH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAClD,OAAO,EAAE,KAAK,WAAW,EAAa,MAAM,kBAAkB,CAAA;AAG9D,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,SAAS,CAAA;AAE/C,MAAM,WAAW,YAAY;IAC5B,QAAQ,EAAE,aAAa,CAAA;IACvB,mFAAmF;IACnF,IAAI,EAAE,MAAM,CAAA;IACZ,2DAA2D;IAC3D,IAAI,EAAE,MAAM,CAAA;IACZ,6EAA6E;IAC7E,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;CACb;AAED,MAAM,WAAW,WAAW;IAC3B,QAAQ,EAAE,YAAY,EAAE,CAAA;IACxB,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,EAAE,MAAM,CAAA;CACf;AAgKD,MAAM,WAAW,mBAAmB;IACnC;;;;OAIG;IACH,OAAO,CAAC,EAAE,WAAW,CAAA;CACrB;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,EAAE,EAAE,aAAa,EAAE,OAAO,GAAE,mBAAwB,GAAG,OAAO,CAAC,WAAW,CAAC,CAkK7G;AAED,sEAAsE;AACtE,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CA0B7D"}
|