// 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' }, // header?: { fields: [HeaderField] }, // running print header; absent = defaults // 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 } // // HeaderField: { kind: 'text'|'lastUpdated', text, align?: 'left'|'right' } // kind 'text' prints `text`; 'lastUpdated' prints `text` (a label) + meta.lastUpdated. // At most one field is 'left' and one 'right'. Printed order: the left field, // then unaligned fields in list order, then the right field. // // 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']; export const HEADER_KINDS = ['text', 'lastUpdated']; export const HEADER_ALIGNS = ['left', 'right']; // What the header is when the document doesn't define one. export function defaultHeaderFields(doc) { return [ { kind: 'text', text: (doc.meta && doc.meta.title) || 'Final Wishes', align: 'left' }, { kind: 'lastUpdated', text: 'Last update: ', align: 'right' }, ]; } 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 (doc.header !== undefined) { if (!isObj(doc.header) || !Array.isArray(doc.header.fields)) errs.push('header.fields must be an array'); else { let left = 0; let right = 0; doc.header.fields.forEach((f, i) => { const w = `header.fields[${i}]`; if (!isObj(f)) return errs.push(`${w}: must be an object`); if (!HEADER_KINDS.includes(f.kind)) errs.push(`${w}: unknown kind "${f.kind}"`); if (!isStr(f.text)) errs.push(`${w}: text must be a string`); if (f.align !== undefined) { if (!HEADER_ALIGNS.includes(f.align)) errs.push(`${w}: bad align "${f.align}"`); else if (f.align === 'left') left++; else right++; } }); if (left > 1) errs.push('header: only one field can be left aligned'); if (right > 1) errs.push('header: only one field can be right aligned'); } } 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; }