Files
softharbor/scripts/validate-data.mjs
T
poli0981andClaude Opus 5 c297c75e64 feat: M2 data layer — schema, registry, 5 seed apps, detail pages
The repository is now the database (docs/02 §1). 43 pages build from 5 JSON
files and a 12-entry category registry: grid, detail and category pages, each
in both locales.

Validation, proven in both directions:
- L1 (Zod, inside astro build) rejects shape errors. Negative-controlled: an
  entry missing `developer` and carrying an unknown key fails the build with
  both violations named, exit 1 - .strict() doing its job.
- L2 (scripts/validate-data.mjs) covers what Zod structurally cannot, because
  it never sees the filename or the filesystem: slug format and length,
  duplicate slug, duplicate name folded diacritic-insensitively, unknown
  category ref, non-https link, flagged-in-main-tree (hard rule 5), missing
  local: logo, updatedAt before addedAt, and quarantine invariants.
  Negative-controlled: one bad fixture tripped 5 rules at once, including
  catching "7-zip" as a duplicate of "7-Zip".
- L3: scripts/extract-urls.mjs feeds lychee; all 14 seed URLs return 200.

Two findings worth the space:

1. The T4 set:html ban had NEVER worked. docs/09 §2 says it is lint-banned,
   but the rule was a hand-rolled no-restricted-syntax selector matching
   JSXAttribute - a node astro-eslint-parser does not produce for template
   directives. Three components used set:html with a clean CI. Replaced with
   eslint-plugin-astro's purpose-built astro/no-set-html-directive, and
   negative-controlled: a fresh file with set:html={evil} now fails lint.

2. unplugin-icons cannot do data-driven icons. It resolves ~icons/set/name
   from a STATIC import specifier, but `logo` is a schema field, so the name
   is only known per entry. src/lib/icon.ts reads the same vendored Iconify
   JSON and inlines the SVG at build time - still zero runtime requests
   (hard rule 7), and still inheriting currentColor, which an <img> data URI
   would lose and which docs/04 §2 and docs/14 §3d both require.
   That needs set:html, so the ban carries exactly one carve-out, scoped to
   three named files rather than a loose disable comment: the only
   data-derived input is a schema-constrained lookup KEY, never interpolated
   markup, and an unknown key throws at build.

Seed entries are 7-Zip, VLC, Firefox, Krita and KeePassXC - all
security.status "unverified", the documented entry state (docs/04 §2). I did
not set "clean": that requires the docs/09 §6 vetting checklist, including a
reputation sweep and VirusTotal, and hard rule 4 forbids clean-by-default.
Licenses and developer names still need the L4 human pass.

Also: the header has linked to /categories since M1 and that route 404'd
until now.

Gate: lint, validate:data, i18n:check, 73 tests, astro check (0 errors),
build - all green.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-28 13:02:54 +07:00

135 lines
4.9 KiB
JavaScript

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 (SkullMute)
/**
* L2 data validation (docs/03 §2). Catches what the Zod schema structurally
* cannot: rules that span files, or that involve the filename, or the
* filesystem.
*
* Plain Node — runs OUTSIDE Astro, so it must never import astro:content.
* Prints `file → rule → detail` and exits non-zero.
*/
import { existsSync, readFileSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
const APPS_DIR = join(process.cwd(), 'src/data/apps');
const QUARANTINE_DIR = join(process.cwd(), 'data/quarantine');
const LOGOS_DIR = join(process.cwd(), 'src/assets/logos');
const CATEGORIES = join(process.cwd(), 'src/data/categories.json');
const SLUG_RE = /^[a-z0-9]+(-[a-z0-9]+)*$/;
const SLUG_MAX = 40;
/** Workers Static Assets caps a deploy; warn well before it (docs/08 A5). */
const FILE_WARN_THRESHOLD = 15_000;
const errors = [];
const warnings = [];
const fail = (file, rule, detail) => errors.push({ file, rule, detail });
/** Same fold as src/lib/normalize.ts — duplicated only because this script
* runs in plain Node and must not import from src/ (kept in sync by test). */
const normalizeViet = (s) =>
s
.normalize('NFD')
.replace(/[̀-ͯ]/g, '')
.replace(/đ/g, 'd')
.replace(/Đ/g, 'D')
.toLowerCase()
.replace(/\s+/g, ' ')
.trim();
const categoryIds = new Set(JSON.parse(readFileSync(CATEGORIES, 'utf8')).map((c) => c.id));
const files = existsSync(APPS_DIR) ? readdirSync(APPS_DIR).filter((f) => f.endsWith('.json')) : [];
const seenSlug = new Map();
const seenName = new Map();
for (const file of files) {
const slug = file.replace(/\.json$/, '');
let app;
try {
app = JSON.parse(readFileSync(join(APPS_DIR, file), 'utf8'));
} catch (e) {
fail(file, 'parse', e.message);
continue;
}
// Zod never sees the filename, but the filename IS the slug — it is the URL
// and the RSS guid (docs/04 §2).
if (!SLUG_RE.test(slug)) fail(file, 'slug-format', `"${slug}" is not kebab-case [a-z0-9-]`);
if (slug.length > SLUG_MAX) fail(file, 'slug-length', `${slug.length} chars, max ${SLUG_MAX}`);
if (seenSlug.has(slug)) fail(file, 'duplicate-slug', `also ${seenSlug.get(slug)}`);
seenSlug.set(slug, file);
// Two entries for the same product, differing only by accent or case, is a
// curation error a human reviewer reliably misses.
if (typeof app.name === 'string') {
const key = normalizeViet(app.name);
if (seenName.has(key))
fail(file, 'duplicate-name', `"${app.name}" collides with ${seenName.get(key)}`);
seenName.set(key, file);
}
for (const id of app.categories ?? []) {
if (!categoryIds.has(id)) fail(file, 'unknown-category', `"${id}" not in categories.json`);
}
for (const [k, v] of Object.entries(app.links ?? {})) {
if (v !== null && !String(v).startsWith('https://'))
fail(file, 'insecure-link', `links.${k} = ${v}`);
}
// Hard rule 5: a flagged entry must be in data/quarantine/, never here.
if (app.security?.status === 'flagged') {
fail(file, 'flagged-in-main-tree', 'move to data/quarantine/ (docs/03 §5)');
}
// A local: logo that does not exist fails the build far away from the cause.
const local = /^local:(.+)$/.exec(app.logo ?? '');
if (local && !existsSync(join(LOGOS_DIR, local[1]))) {
fail(file, 'missing-logo', `src/assets/logos/${local[1]} not found`);
}
if (app.updatedAt && app.addedAt && app.updatedAt < app.addedAt) {
fail(file, 'date-order', `updatedAt ${app.updatedAt} precedes addedAt ${app.addedAt}`);
}
}
// Quarantined entries must keep status flagged, or restoring them silently
// republishes something that was pulled.
if (existsSync(QUARANTINE_DIR)) {
for (const file of readdirSync(QUARANTINE_DIR).filter((f) => f.endsWith('.json'))) {
try {
const q = JSON.parse(readFileSync(join(QUARANTINE_DIR, file), 'utf8'));
if (q.security?.status !== 'flagged') {
fail(
`data/quarantine/${file}`,
'unflagged-in-quarantine',
`status is "${q.security?.status}"`,
);
}
if (seenSlug.has(file.replace(/\.json$/, ''))) {
fail(`data/quarantine/${file}`, 'slug-in-both-trees', 'same slug is live and quarantined');
}
} catch (e) {
fail(`data/quarantine/${file}`, 'parse', e.message);
}
}
}
if (files.length > FILE_WARN_THRESHOLD) {
warnings.push(`${files.length} app files — approaching the Workers per-deploy cap (docs/08 A5)`);
}
for (const w of warnings) console.warn(` ! ${w}`);
if (errors.length > 0) {
console.error(`validate:data — ${errors.length} problem(s):\n`);
const w = Math.max(...errors.map((e) => e.file.length));
for (const e of errors)
console.error(` ✗ ${e.file.padEnd(w)} ${e.rule.padEnd(22)} ${e.detail}`);
process.exitCode = 1;
} else {
console.log(`validate:data — OK (${files.length} apps, ${categoryIds.size} categories)`);
}