ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/collections/checks/s01_relations.ts

checks/s01_relations.ts

46 líneas, 2.7 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // S1. Every value of a `kind: relation` field points to an existing record of the collection named in `collection`.
  2. // Kinds of findings: broken (no such file), wrong-collection (the file is not in that collection), not-multiple
  3. // (several values in a single-valued field), bad-value (the value is not a path).
  4. // A relation without `collection` (polymorphic, like enforcers of rules) is checked for existence only. A relation into
  5. // a collection the project does not have (a bridge into a layer not installed) sleeps and is not checked: the standard's
  6. // set had this as the no-collection finding; here it is not one, or any package with a bridge would fail in a project
  7. // without the second layer.
  8. import { addr, ctx, empty, hole, iterFields, scopeAll, short, values } from './lib/schemalib.ts';
  9. import { type Project, type Result, normPath, truthy } from './lib/speclib.ts';
  10. import { main } from './lib/runner.ts';
  11. export const RULE = { id: 's01', title: 'Schema: relations lead to existing records of the right collection', scope: 'all records of all collections, kind: relation fields' };
  12. export function check(p: Project, res: Result): void {
  13. const c = ctx(p);
  14. scopeAll(c, res, 'relations');
  15. if (!res.part('relations', null)) return;
  16. const files = c.files();
  17. const colls = new Set(c.collections());
  18. for (const [coll, name, spec] of iterFields(c, ['relation'])) {
  19. let target = String(truthy(spec.collection) ? spec.collection : '').trim().replace(/\/+$/, '');
  20. if (target.endsWith('.md')) target = target.slice(0, -3);
  21. if (target && !colls.has(target)) continue; // a bridge into a layer the project has not installed sleeps
  22. for (const r of c.records(coll)) {
  23. const v = r.fm[name];
  24. if (empty(v)) continue;
  25. const a = addr(r.id);
  26. if (!truthy(spec.multiple) && Array.isArray(v) && v.length > 1)
  27. hole(res, a, name, 'not-multiple', `${name}: ${v.length} values, but the field is single`, 'keep one value');
  28. for (const x of values(v)) {
  29. if (empty(x)) continue;
  30. if (typeof x === 'object' || typeof x === 'boolean' || typeof x === 'number') {
  31. hole(res, a, name, 'bad-value', `${name}: not a path, but ${short(x)}`, `replace with the path of a ${target} record: ${target}/…`);
  32. continue;
  33. }
  34. const path = normPath(x, name, r.id);
  35. if (!path || !files.has(path))
  36. hole(res, a, name, 'broken', `${name}: ${short(x)} — there is no such record`, 'fix the path or remove the reference');
  37. else if (target && !path.startsWith(target + '/'))
  38. hole(res, a, name, 'wrong-collection', `${name}: ${short(x)} is not from ${target}`, `refer to a record of ${target} or fix the schema`);
  39. }
  40. }
  41. }
  42. }
  43. if (import.meta.main) main({ RULE, check });