Define the sections/records/items schema with secret-flagged fields and a validator with tests. build.js bundles model.js. The PDF-derived seed lives in the gitignored data/ directory. Closes #3 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
114 lines
5.0 KiB
JavaScript
114 lines
5.0 KiB
JavaScript
// Data model for the document (plaintext JSON before encryption).
|
|
// Pure functions, no DOM; runs in browsers and Node. build.js strips `export`.
|
|
//
|
|
// Document:
|
|
// { schemaVersion: 1,
|
|
// meta: { owner, title, lastUpdated: 'YYYY-MM-DD' },
|
|
// sections: [ Section ] }
|
|
//
|
|
// Section (ordered). `kind` decides which body it has:
|
|
// 'text' -> { body: string } free narrative ("Start here")
|
|
// 'records' -> { records: [Record] } accounts, people, places, items
|
|
// 'steps' -> { items: [Item] } ordered instructions (wind-down)
|
|
// 'checklist' -> { items: [Item] } things still to add/do
|
|
// Every section has { id, title, kind } and may have { intro: string }.
|
|
//
|
|
// Record: { id, title, status: 'ok'|'tbd', updated: 'YYYY-MM-DD',
|
|
// fields: [{ label, value, secret?: boolean }], notes?: string }
|
|
// Item: { id, text, status: 'open'|'done'|'tbd', detail?: string }
|
|
//
|
|
// Ids are unique across the whole document. `secret` fields are masked
|
|
// in the UI and optionally in print output.
|
|
|
|
export const SCHEMA_VERSION = 1;
|
|
export const SECTION_KINDS = ['text', 'records', 'steps', 'checklist'];
|
|
export const RECORD_STATUSES = ['ok', 'tbd'];
|
|
export const ITEM_STATUSES = ['open', 'done', 'tbd'];
|
|
|
|
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
const isStr = (v) => typeof v === 'string';
|
|
const isObj = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
|
|
export function emptyDocument(owner = '', title = 'In the event of my death or incapacity') {
|
|
return {
|
|
schemaVersion: SCHEMA_VERSION,
|
|
meta: { owner, title, lastUpdated: new Date().toISOString().slice(0, 10) },
|
|
sections: [],
|
|
};
|
|
}
|
|
|
|
// Returns a list of human-readable problems; empty means valid.
|
|
export function validate(doc) {
|
|
const errs = [];
|
|
const seen = new Set();
|
|
const checkId = (id, where) => {
|
|
if (!isStr(id) || !id) errs.push(`${where}: missing id`);
|
|
else if (seen.has(id)) errs.push(`${where}: duplicate id "${id}"`);
|
|
else seen.add(id);
|
|
};
|
|
|
|
if (!isObj(doc)) return ['document must be an object'];
|
|
if (doc.schemaVersion !== SCHEMA_VERSION) errs.push(`schemaVersion must be ${SCHEMA_VERSION}`);
|
|
|
|
if (!isObj(doc.meta)) errs.push('meta must be an object');
|
|
else {
|
|
if (!isStr(doc.meta.owner)) errs.push('meta.owner must be a string');
|
|
if (!isStr(doc.meta.title)) errs.push('meta.title must be a string');
|
|
if (!DATE_RE.test(doc.meta.lastUpdated ?? '')) errs.push('meta.lastUpdated must be YYYY-MM-DD');
|
|
}
|
|
|
|
if (!Array.isArray(doc.sections)) return [...errs, 'sections must be an array'];
|
|
|
|
doc.sections.forEach((s, i) => {
|
|
const where = `sections[${i}]`;
|
|
if (!isObj(s)) return errs.push(`${where}: must be an object`);
|
|
checkId(s.id, where);
|
|
if (!isStr(s.title) || !s.title) errs.push(`${where}: missing title`);
|
|
if (s.intro !== undefined && !isStr(s.intro)) errs.push(`${where}: intro must be a string`);
|
|
if (!SECTION_KINDS.includes(s.kind)) return errs.push(`${where}: unknown kind "${s.kind}"`);
|
|
|
|
if (s.kind === 'text') {
|
|
if (!isStr(s.body)) errs.push(`${where}: text section needs a body string`);
|
|
} else if (s.kind === 'records') {
|
|
if (!Array.isArray(s.records)) return errs.push(`${where}: records must be an array`);
|
|
s.records.forEach((r, j) => checkRecord(r, `${where}.records[${j}]`));
|
|
} else {
|
|
if (!Array.isArray(s.items)) return errs.push(`${where}: items must be an array`);
|
|
s.items.forEach((it, j) => checkItem(it, `${where}.items[${j}]`));
|
|
}
|
|
});
|
|
|
|
function checkRecord(r, where) {
|
|
if (!isObj(r)) return errs.push(`${where}: must be an object`);
|
|
checkId(r.id, where);
|
|
if (!isStr(r.title) || !r.title) errs.push(`${where}: missing title`);
|
|
if (!RECORD_STATUSES.includes(r.status)) errs.push(`${where}: bad status "${r.status}"`);
|
|
if (!DATE_RE.test(r.updated ?? '')) errs.push(`${where}: updated must be YYYY-MM-DD`);
|
|
if (r.notes !== undefined && !isStr(r.notes)) errs.push(`${where}: notes must be a string`);
|
|
if (!Array.isArray(r.fields)) return errs.push(`${where}: fields must be an array`);
|
|
r.fields.forEach((f, k) => {
|
|
const fw = `${where}.fields[${k}]`;
|
|
if (!isObj(f)) return errs.push(`${fw}: must be an object`);
|
|
if (!isStr(f.label) || !f.label) errs.push(`${fw}: missing label`);
|
|
if (!isStr(f.value)) errs.push(`${fw}: value must be a string`);
|
|
if (f.secret !== undefined && typeof f.secret !== 'boolean') errs.push(`${fw}: secret must be a boolean`);
|
|
});
|
|
}
|
|
|
|
function checkItem(it, where) {
|
|
if (!isObj(it)) return errs.push(`${where}: must be an object`);
|
|
checkId(it.id, where);
|
|
if (!isStr(it.text) || !it.text) errs.push(`${where}: missing text`);
|
|
if (!ITEM_STATUSES.includes(it.status)) errs.push(`${where}: bad status "${it.status}"`);
|
|
if (it.detail !== undefined && !isStr(it.detail)) errs.push(`${where}: detail must be a string`);
|
|
}
|
|
|
|
return errs;
|
|
}
|
|
|
|
export function assertValid(doc) {
|
|
const errs = validate(doc);
|
|
if (errs.length) throw new Error('Invalid document:\n- ' + errs.join('\n- '));
|
|
return doc;
|
|
}
|