Files
Rosea/packages/content/src/validator.ts
T

165 lines
6.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
// Added 2026-11-02 with the first candidates (day-sentence.md P8). Each is the edition
// on Project Gutenberg; translator dead before 1956 (EU life + 70) and the edition
// first published before 1931 (US). Death years from the Gutenberg catalogue,
// cross-checked by web search. The author still checks each sentence.
'George Long, 1862', // Marcus Aurelius; d. 1879
'George Long, 1877', // Epictetus; d. 1879
'Evelyn S. Shuckburgh, 1900', // Cicero; d. 1906
'Andrew P. Peabody, 1887', // Cicero, De Amicitia; d. 1893
'Aubrey Stewart, 1889', // Seneca, Minor Dialogues; d. 1918
'James Legge, 1891', // Tao Te Ching; d. 1897
'Herbert A. Giles, 1889', // Chuang Tzu; d. 1935
'F. Max Müller, 1881', // Dhammapada; d. 1900
'James Ross, 1823', // Sa'di, Gulistan; d. 1831
'William Benham, 1886', // Imitation of Christ; d. 1910
'Grace Warrack, 1901', // Julian of Norwich; d. 1932
'Rabindranath Tagore (his own English), 1912' // Gitanjali; d. 1941
]);
/**
* 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;
}