包文件→ .shtukbase/packages/contours/checks/c01_states.ts
checks/c01_states.ts
39 行, 2.0 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // 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.
- // An empty value is not checked (being required is the required check of the package collections).
- // The code is the phase part of the check s02_values.ts of the package collections.
- import { addr, ctx, empty, hole, iterFields, scopeAll, short, values } from '../../collections/checks/lib/schemalib.ts';
- import { type Project, type Result, pystr } from '../../collections/checks/lib/speclib.ts';
- import { main } from './contourlib.ts';
- 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' };
- export function check(p: Project, res: Result): void {
- const c = ctx(p);
- scopeAll(c, res, 'states');
- if (!res.part('states', null)) return;
- const seenSchema = new Set<string>();
- for (const [coll, name, spec] of iterFields(c, ['phase'])) {
- const allowed = c.states(spec.contour);
- if (allowed === null) {
- if (!seenSchema.has(coll + '\0' + name)) {
- seenSchema.add(coll + '\0' + name);
- 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');
- }
- continue;
- }
- for (const r of c.records(coll)) {
- const v = r.fm[name];
- if (empty(v)) continue;
- if (Array.isArray(v) && v.length !== 1) {
- hole(res, addr(r.id), name, 'bad-phase', `${name}: a list ${short(v)}, but the state is single`, 'keep one value');
- continue;
- }
- for (const x of values(v)) {
- if ((x && typeof x === 'object') || !allowed.has(pystr(x)))
- 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(', '));
- }
- }
- }
- }
- if (import.meta.main) main({ RULE, check });