ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/contours/checks/c01_states.ts

checks/c01_states.ts

39 líneas, 2.0 KB.

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

  1. // C1. The state of a record is a phase of its contour: the value of the `kind: phase` field is among the phases of the contour from the schema.
  2. // An empty value is not checked (being required is the required check of the package collections).
  3. // The code is the phase part of the check s02_values.ts of the package collections.
  4. import { addr, ctx, empty, hole, iterFields, scopeAll, short, values } from '../../collections/checks/lib/schemalib.ts';
  5. import { type Project, type Result, pystr } from '../../collections/checks/lib/speclib.ts';
  6. import { main } from './contourlib.ts';
  7. export const RULE = { id: 'c01', title: 'Contours: the state of a record is a phase of its contour', scope: 'all records of all collections, kind: phase fields' };
  8. export function check(p: Project, res: Result): void {
  9. const c = ctx(p);
  10. scopeAll(c, res, 'states');
  11. if (!res.part('states', null)) return;
  12. const seenSchema = new Set<string>();
  13. for (const [coll, name, spec] of iterFields(c, ['phase'])) {
  14. const allowed = c.states(spec.contour);
  15. if (allowed === null) {
  16. if (!seenSchema.has(coll + '\0' + name)) {
  17. seenSchema.add(coll + '\0' + name);
  18. hole(res, coll.slice(1) + '.md', name, 'no-contour', `schema ${coll}: field ${name} refers to the contour ${pystr(spec.contour)}, which does not exist`, 'fix contour in the schema');
  19. }
  20. continue;
  21. }
  22. for (const r of c.records(coll)) {
  23. const v = r.fm[name];
  24. if (empty(v)) continue;
  25. if (Array.isArray(v) && v.length !== 1) {
  26. hole(res, addr(r.id), name, 'bad-phase', `${name}: a list ${short(v)}, but the state is single`, 'keep one value');
  27. continue;
  28. }
  29. for (const x of values(v)) {
  30. if ((x && typeof x === 'object') || !allowed.has(pystr(x)))
  31. hole(res, addr(r.id), name, 'bad-phase', `${name}: "${short(x)}" is not a phase of the contour ${pystr(spec.contour)}`, 'one of: ' + [...allowed].sort().join(', '));
  32. }
  33. }
  34. }
  35. }
  36. if (import.meta.main) main({ RULE, check });