Files
Rosea/packages/content/src/validator.ts
T
mnovak 49c8def037 [repo] Workspace: Scaffold Phase 0 with schema, engine, content, ui and the PWA shell
pnpm + turbo workspace with the commands from CLAUDE.md §3. The contracts
from §6 as JSON Schemas with generated types; localDay as the one place a
day is computed; the forbidden-word list shared by lint:copy and the
library validator, with 13 negative fixtures; a Svelte 5 PWA shell built
under /Rosea/ with a meta CSP. E2E and offline suites run against the
built bundle; the offline one closes the server rather than emulating.

Analysis: .agents/features/phase0-workspace.md
2026-11-01 00:27:33 +01:00

149 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The hope-library validator (CLAUDE.md §5.5, §5.6 rule 2, §6.1).
*
* The schema says what shape an entry has. This says whether it may be shown
* to a person: sourced, public domain or the author's own, short enough to
* read at sunset, free of forbidden words, and not promising anything.
*
* Its negative fixtures in `content/fixtures/` ARE the validator: a rule here
* without a fixture proving it fires is a review blocker.
*/
import type { HopeSentence } from '@rosea/schema';
import { validateHopeSentence } from '@rosea/schema/validate';
import { findForbidden } from './forbidden.js';
export type Rule =
| 'schema'
| 'no-source'
| 'not-public-domain'
| 'translation-under-copyright'
| 'too-long'
| 'promises-outcome'
| 'forbidden-word'
| 'no-tradition'
| 'duplicate-id';
export interface Problem {
readonly id: string;
readonly rule: Rule;
readonly message: string;
}
/** Characters per language. A sentence opened at sunset is read on a phone, once. */
export const LENGTH_CAP = 240;
/**
* The tradition tag for a line the author wrote for Rosea. Those are not public
* domain, and need not be: the author owns them (CLAUDE.md §5.6 rule 2).
*/
export const AUTHOR_TRADITION = 'rosea';
/**
* Translations known to be in the public domain. Anything else is assumed to
* be under copyright until someone checks and adds it here, with the source of
* that check in the commit.
*/
export const PUBLIC_DOMAIN_TRANSLATIONS: ReadonlySet<string> = new Set([
'KJV', // King James Version, 1611/1769
'Bible kralická', // 1579–1613
'ASV', // American Standard Version, 1901
'Douay-Rheims', // 1582–1610, Challoner revision 1752
'YLT', // Young's Literal Translation, 1862
'WEB' // World English Bible, dedicated to the public domain
]);
/**
* Phrasings that promise an outcome. A heuristic, so a hit means "a human must
* read this", and the entry fails until one does. An honest `promisesOutcome:
* true` fails the same way.
*/
const OUTCOME_PHRASES: readonly RegExp[] = [
/\beverything will be\b/i,
/\b(it|you|things) will (all )?(be|get) (better|fine|okay|ok|alright|all right|well)\b/i,
/\byou will (find|never|always|soon)\b/i,
/\bi promise\b/i,
/\bguarantee/i
];
const idOf = (input: unknown): string =>
typeof input === 'object' && input !== null && typeof (input as { id?: unknown }).id === 'string'
? (input as { id: string }).id
: '(no id)';
/** Every reason `input` may not be shown to a player. Empty means it may. */
export function validateEntry(input: unknown): Problem[] {
const id = idOf(input);
const problem = (rule: Rule, message: string): Problem => ({ id, rule, message });
const problems: Problem[] = [];
const schema = validateHopeSentence(input);
if (!schema.ok) {
for (const error of schema.errors) {
if (/^\(root\) .*'source'/.test(error)) {
problems.push(problem('no-source', 'has no source: every sentence names where it comes from'));
} else if (/^\/source .*'tradition'|^\/source\/tradition /.test(error)) {
problems.push(problem('no-tradition', 'has no tradition tag: the library stays mixed, so each line says which one it is from'));
} else {
problems.push(problem('schema', error));
}
}
return problems;
}
const entry: HopeSentence = schema.value;
const { source } = entry;
if (!source.publicDomain && source.tradition !== AUTHOR_TRADITION) {
problems.push(
problem('not-public-domain', `source '${source.work}' is not verified public domain, and the author did not write it for Rosea`)
);
}
if (source.translation !== null && !PUBLIC_DOMAIN_TRANSLATIONS.has(source.translation)) {
problems.push(
problem(
'translation-under-copyright',
`translation '${source.translation}' is not on the public-domain list; a modern translation is usually still under copyright`
)
);
}
for (const [lang, text] of Object.entries(entry.text)) {
if (text === undefined) continue;
const length = [...text].length;
if (length > LENGTH_CAP) {
problems.push(problem('too-long', `${lang} text is ${length} characters, over the length cap of ${LENGTH_CAP}`));
}
for (const hit of findForbidden(text)) {
problems.push(problem('forbidden-word', `${lang} text contains '${hit.term}' (${hit.category}): ${hit.why}`));
}
const phrase = OUTCOME_PHRASES.find((p) => p.test(text));
if (phrase !== undefined) {
problems.push(problem('promises-outcome', `${lang} text may promise an outcome (${phrase.source}): needs human review`));
}
}
if (entry.promisesOutcome) {
problems.push(problem('promises-outcome', 'is marked promisesOutcome: comfort never promises what will happen'));
}
return problems;
}
/** Validates a whole library: every entry, plus ids unique across all of them. */
export function validateLibrary(entries: readonly unknown[]): Problem[] {
const problems = entries.flatMap(validateEntry);
const seen = new Set<string>();
for (const entry of entries) {
const id = idOf(entry);
if (seen.has(id)) {
problems.push({ id, rule: 'duplicate-id', message: `id '${id}' is used more than once; a message between players would be ambiguous` });
}
seen.add(id);
}
return problems;
}