Skip to content

Commit 52311b2

Browse files
TMHSDigitalclaude
andcommitted
build: optional entry fields from one table, checked against validate's keys
The build's optional-field spread is now OPTIONAL_FIELDS in site/lib/entry-fields.mjs, beside CORE_FIELDS and NOT_EXPORTED (preview, with its reason). A test fails when a key validate knows is neither exported nor excluded on purpose, when a core field drifts from REQUIRED_KEYS, or when an exported field is missing from the data.json schema. data.json is byte-identical before and after. Closes #235 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LpkeXQPaUhxjgqdqnk6BrJ
1 parent 823a6b6 commit 52311b2

3 files changed

Lines changed: 68 additions & 10 deletions

File tree

‎site/build.mjs‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { categoryPageHtml } from "./lib/category-page.mjs";
2222
import { filmstripPick, isAttributionLicence, previewCredit, thumbRelPath } from "./lib/thumbs.mjs";
2323
import { summaryFromBody } from "./lib/frontmatter.mjs";
2424
import { readCatalog, idMatchesFile } from "./lib/entry-files.mjs";
25+
import { optionalFields } from "./lib/entry-fields.mjs";
2526
import { entryPageHtml, scriptJson } from "./lib/entry-page.mjs";
2627
import { LinkError, makeLinkResolver, caseMismatch } from "./lib/links.mjs";
2728
import { atomFeed, catalogCsv, commitDatesFromLog, DATED_PATHS, homeJsonLd, sitemapXml } from "./lib/exports.mjs";
@@ -144,16 +145,7 @@ function loadEntries(vocab) {
144145
...(isAttributionLicence(meta.license) ? { credit: previewCredit(meta) } : {}),
145146
}
146147
: null,
147-
...(meta.grid_dimensions ? { grid_dimensions: String(meta.grid_dimensions) } : {}),
148-
...(meta.camera_perspective ? { camera_perspective: String(meta.camera_perspective) } : {}),
149-
...(Array.isArray(meta.hardware_tags) ? { hardware_tags: meta.hardware_tags } : {}),
150-
...(meta.attribution_string ? { attribution_string: String(meta.attribution_string) } : {}),
151-
...(meta.publisher ? { publisher: String(meta.publisher) } : {}),
152-
...(meta.license_spdx ? { license_spdx: String(meta.license_spdx) } : {}),
153-
...(meta.maintenance ? { maintenance: String(meta.maintenance) } : {}),
154-
...(meta.engine_lock ? { engine_lock: String(meta.engine_lock) } : {}),
155-
...(meta.ue_versions ? { ue_versions: String(meta.ue_versions) } : {}),
156-
...(meta.limits ? { limits: String(meta.limits) } : {}),
148+
...optionalFields(meta),
157149
licenseFamily: licenseFamily(vocab, String(meta.license)),
158150
attributionClass: vocab.licenses[String(meta.license)]?.attribution || "any",
159151
licenseRank:

‎site/lib/entry-fields.mjs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Which frontmatter fields reach data.json, and how. build.mjs writes the
3+
* core fields one by one (they are always present, with fallbacks) and the
4+
* optional ones from OPTIONAL_FIELDS; lib.test.mjs checks that every key
5+
* validate knows (checks.mjs KNOWN_KEYS) is one or the other, or listed in
6+
* NOT_EXPORTED with a reason, so a new field cannot pass validate and then
7+
* silently never reach the site.
8+
*/
9+
10+
/** Always in data.json, normalised by build.mjs loadEntries. */
11+
export const CORE_FIELDS = [
12+
"id", "name", "url", "category", "subcategories", "license", "commercial",
13+
"attribution_required", "formats", "tags", "added", "verified", "status",
14+
];
15+
16+
/** In data.json only when set: `string` fields are stringified, `list` fields kept as arrays. */
17+
export const OPTIONAL_FIELDS = [
18+
{ key: "grid_dimensions", type: "string" },
19+
{ key: "camera_perspective", type: "string" },
20+
{ key: "hardware_tags", type: "list" },
21+
{ key: "attribution_string", type: "string" },
22+
{ key: "publisher", type: "string" },
23+
{ key: "license_spdx", type: "string" },
24+
{ key: "maintenance", type: "string" },
25+
{ key: "engine_lock", type: "string" },
26+
{ key: "ue_versions", type: "string" },
27+
{ key: "limits", type: "string" },
28+
];
29+
30+
/** Known keys deliberately left out of data.json, with why. */
31+
export const NOT_EXPORTED = {
32+
// An instruction to the thumbnail script; the result is exported as `thumb`.
33+
preview: "used by site/thumbs.mjs; its result is the entry's `thumb`",
34+
};
35+
36+
/** The optional fields of one entry's frontmatter, as data.json carries them. */
37+
export function optionalFields(meta) {
38+
const out = {};
39+
for (const { key, type } of OPTIONAL_FIELDS) {
40+
const v = meta[key];
41+
if (type === "list") {
42+
if (Array.isArray(v)) out[key] = v;
43+
} else if (v) {
44+
out[key] = String(v);
45+
}
46+
}
47+
return out;
48+
}

‎site/lib/lib.test.mjs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,24 @@ eq("search: q from the URL is capped", readState(`?q=${"a".repeat(5000)}`, { q:
19431943
has("opensearch: homepage link", opensearchLinkHtml({ siteUrl: "https://x.test/s", title: "T" }), 'href="https://x.test/s/opensearch.xml"');
19441944
}
19451945

1946+
/* every known field reaches data.json or is excluded on purpose (#235) ------- */
1947+
{
1948+
const { CORE_FIELDS, OPTIONAL_FIELDS, NOT_EXPORTED, optionalFields } = await import("./entry-fields.mjs");
1949+
const { KNOWN_KEYS, REQUIRED_KEYS } = await import("../checks.mjs");
1950+
const optional = OPTIONAL_FIELDS.map((f) => f.key);
1951+
const covered = [...CORE_FIELDS, ...optional, ...Object.keys(NOT_EXPORTED)];
1952+
for (const key of KNOWN_KEYS) eq(`fields: ${key} is exported or excluded on purpose`, covered.includes(key), true);
1953+
eq("fields: no field is listed twice", new Set(covered).size, covered.length);
1954+
eq("fields: core fields are validate's required keys", [...CORE_FIELDS].sort().join(), [...REQUIRED_KEYS].sort().join());
1955+
const schema = JSON.parse(nodeFs.readFileSync(new URL("../data.schema.json", import.meta.url), "utf8"));
1956+
const props = Object.keys(schema.$defs.entryBase.properties);
1957+
for (const key of [...CORE_FIELDS, ...optional]) eq(`fields: ${key} is in the data.json schema`, props.includes(key), true);
1958+
const all = Object.fromEntries(optional.map((k) => [k, OPTIONAL_FIELDS.find((f) => f.key === k).type === "list" ? ["a"] : "v"]));
1959+
eq("fields: optionalFields copies every optional field", Object.keys(optionalFields(all)).sort().join(), [...optional].sort().join());
1960+
eq("fields: unset optional fields are left out", Object.keys(optionalFields({ publisher: "", hardware_tags: "x" })).length, 0);
1961+
eq("fields: preview is not exported", "preview" in optionalFields({ preview: "none" }), false);
1962+
}
1963+
19461964
/* lint: unused imports (#230) --------------------------------------------- */
19471965
{
19481966
const { unusedImports, scriptFiles } = await import("../lint.mjs");

0 commit comments

Comments
 (0)