Add document data model and validation
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>
This commit is contained in:
53
test/model.test.js
Normal file
53
test/model.test.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import { validate, emptyDocument, assertValid } from '../src/model.js';
|
||||
|
||||
const good = () => ({
|
||||
schemaVersion: 1,
|
||||
meta: { owner: 'A', title: 'T', lastUpdated: '2022-04-14' },
|
||||
sections: [
|
||||
{ id: 's-text', title: 'Intro', kind: 'text', body: 'hi' },
|
||||
{ id: 's-rec', title: 'Bank', kind: 'records', records: [
|
||||
{ id: 'r1', title: 'Acct', status: 'ok', updated: '2022-04-14',
|
||||
fields: [{ label: 'PIN', value: '', secret: true }], notes: 'n' } ] },
|
||||
{ id: 's-steps', title: 'Steps', kind: 'steps', items: [
|
||||
{ id: 'i1', text: 'Do it', status: 'open', detail: 'how' } ] },
|
||||
{ id: 's-list', title: 'Add', kind: 'checklist', items: [
|
||||
{ id: 'i2', text: 'More', status: 'tbd' } ] },
|
||||
],
|
||||
});
|
||||
|
||||
test('valid document passes; emptyDocument is valid', () => {
|
||||
assert.deepEqual(validate(good()), []);
|
||||
assert.deepEqual(validate(emptyDocument('Me')), []);
|
||||
assert.equal(assertValid(good()).meta.owner, 'A');
|
||||
});
|
||||
|
||||
test('rejects structural problems', () => {
|
||||
const cases = [
|
||||
[(d) => { d.schemaVersion = 2; }, /schemaVersion/],
|
||||
[(d) => { d.meta.lastUpdated = 'yesterday'; }, /lastUpdated/],
|
||||
[(d) => { d.sections[0].kind = 'weird'; }, /unknown kind/],
|
||||
[(d) => { d.sections[1].records[0].status = 'maybe'; }, /bad status/],
|
||||
[(d) => { d.sections[1].records[0].fields[0].secret = 'yes'; }, /secret must be a boolean/],
|
||||
[(d) => { d.sections[1].records[0].fields[0].value = 5; }, /value must be a string/],
|
||||
[(d) => { d.sections[2].items[0].status = 'x'; }, /bad status/],
|
||||
[(d) => { d.sections[1].records[0].id = 's-text'; }, /duplicate id/],
|
||||
[(d) => { delete d.sections[3].items; }, /items must be an array/],
|
||||
[(d) => { delete d.sections[0].body; }, /body/],
|
||||
];
|
||||
for (const [mutate, re] of cases) {
|
||||
const d = good();
|
||||
mutate(d);
|
||||
assert.match(validate(d).join('\n'), re);
|
||||
}
|
||||
assert.throws(() => assertValid({}), /Invalid document/);
|
||||
assert.deepEqual(validate(null), ['document must be an object']);
|
||||
});
|
||||
|
||||
// data/ is gitignored (private), so this only runs on a machine that has the seed.
|
||||
test('data/seed.json, when present, is valid', { skip: !existsSync('data/seed.json') }, () => {
|
||||
const seed = JSON.parse(readFileSync('data/seed.json', 'utf8'));
|
||||
assert.deepEqual(validate(seed), []);
|
||||
});
|
||||
Reference in New Issue
Block a user