|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `os validate`'s author-time half of the boot registrar's divergent |
| 5 | + * view-container `name` refusal (#20331). |
| 6 | + * |
| 7 | + * ## The defect |
| 8 | + * |
| 9 | + * A `views:` container whose own `name` disagrees with the object key it binds |
| 10 | + * to — `{ name: 'order_line', object: 'my_app_order_line', list: {…} }` — is |
| 11 | + * refused at boot by `ObjectQL.registerMetadataCollections` (#7378 row 1). |
| 12 | + * `os validate` had no counterpart, so it exited 0 on that stack and |
| 13 | + * `os serve` then refused it. `os validate` is the author-time judge of what |
| 14 | + * the runtime will accept (NORTH-STAR road step ①), and a green validate |
| 15 | + * followed by a boot refusal is the silent-validator shape. |
| 16 | + * |
| 17 | + * ## One judge, not a second rule |
| 18 | + * |
| 19 | + * {@link viewContainerNameRefusal} is `@objectstack/objectql`'s, the SAME |
| 20 | + * function the boot registrar throws the answer of. ⛔ Not an `@objectstack/lint` |
| 21 | + * rule and not a re-spelling of the check here: a twin agrees with the |
| 22 | + * runtime only until one of the two is edited, which is the drift this card |
| 23 | + * exists to close. What this module owns is the WALK — which `views:` entries |
| 24 | + * boot judges, and under which package id — and nothing about the verdict. |
| 25 | + * |
| 26 | + * ## The walk is the boot path's |
| 27 | + * |
| 28 | + * `AppPlugin` hands the manifest service `{ ...stack.manifest, ...stack }`; |
| 29 | + * `resolveArtifactPackageOrder` returns that payload itself when it carries no |
| 30 | + * `packages` key and each `packages[i].manifest` body otherwise (ADR-0130 D4); |
| 31 | + * `ObjectQL.registerApp(body)` then runs `registerMetadataCollections(body, |
| 32 | + * body.id || body.name, 'manifest')`. So: |
| 33 | + * |
| 34 | + * - no `packages[]` → the top-level `views`, owned by the manifest's id; |
| 35 | + * - `packages[]` → each body's own `views`, owned by that package's id, |
| 36 | + * and ⛔ NOT the top level, which the load path does not register from. |
| 37 | + * |
| 38 | + * The package id is read through the owners that already exist for it: the |
| 39 | + * runtime's `artifactPackageId` (`@objectstack/core` — "every seam that has to |
| 40 | + * name a package reads the id through THIS function") for the one-package |
| 41 | + * payload, and this package's `artifactPackages` for `packages[]`, the reader |
| 42 | + * both `os build` and `os validate` already walk with. |
| 43 | + * |
| 44 | + * ⚠️ Bound, stated rather than hidden: a nested `plugins[]` entry's `views` is |
| 45 | + * registered by boot too (label `nested plugin`), and is NOT walked here. The |
| 46 | + * stack schema types `plugins` as `unknown[]` — in an authored config they are |
| 47 | + * runtime plugin instances, not metadata bundles — so this door has no parsed |
| 48 | + * shape to walk there. |
| 49 | + * |
| 50 | + * Reads the PARSED stack — what `defineStack()` hands the boot wrap, and what |
| 51 | + * `os build` serializes. |
| 52 | + */ |
| 53 | + |
| 54 | +import { artifactPackageId } from '@objectstack/core'; |
| 55 | +import { viewContainerNameRefusal } from '@objectstack/objectql'; |
| 56 | + |
| 57 | +import { artifactPackages } from './artifact-packages.js'; |
| 58 | + |
| 59 | +/** One refusal, located. `message` is the boot registrar's, verbatim. */ |
| 60 | +export interface ViewContainerNameRefusalRow { |
| 61 | + /** Where the entry sits in the parsed stack, e.g. `views[0]`. */ |
| 62 | + path: string; |
| 63 | + /** The ADR-0112 code the boot registrar throws with. */ |
| 64 | + code: string; |
| 65 | + /** The HTTP status the boot registrar throws with. */ |
| 66 | + httpStatus: number; |
| 67 | + /** The refusal, in the boot registrar's own words. */ |
| 68 | + message: string; |
| 69 | +} |
| 70 | + |
| 71 | +type AnyRec = Record<string, unknown>; |
| 72 | + |
| 73 | +const asRec = (v: unknown): AnyRec | undefined => |
| 74 | + v && typeof v === 'object' && !Array.isArray(v) ? (v as AnyRec) : undefined; |
| 75 | + |
| 76 | +/** |
| 77 | + * Every `views:` container in this stack that the boot registrar would refuse |
| 78 | + * for a divergent `name`, in the order boot meets them within each body. |
| 79 | + * |
| 80 | + * Returns `[]` for a stack the boot registrar accepts on this axis. |
| 81 | + */ |
| 82 | +export function findViewContainerNameRefusals(parsed: AnyRec): ViewContainerNameRefusalRow[] { |
| 83 | + const bodies: Array<{ at: string; ownerId: string | undefined; views: unknown }> = []; |
| 84 | + // The resolver's own branch test (`declared === undefined`). On the PARSED |
| 85 | + // stack a present `packages` is an array — `ArtifactPackageSchema[]`, |
| 86 | + // `.optional()`, so `null` and every non-array were refused at the parse. |
| 87 | + if (parsed.packages !== undefined) { |
| 88 | + for (const pkg of artifactPackages(parsed)) { |
| 89 | + bodies.push({ at: `packages[${pkg.index}].manifest.views`, ownerId: pkg.id, views: pkg.body.views }); |
| 90 | + } |
| 91 | + } else { |
| 92 | + const manifest = asRec(parsed.manifest); |
| 93 | + const payload = manifest ? { ...manifest, ...parsed } : parsed; |
| 94 | + bodies.push({ at: 'views', ownerId: artifactPackageId(payload), views: parsed.views }); |
| 95 | + } |
| 96 | + |
| 97 | + const rows: ViewContainerNameRefusalRow[] = []; |
| 98 | + for (const { at, ownerId, views } of bodies) { |
| 99 | + if (!Array.isArray(views)) continue; |
| 100 | + views.forEach((entry, index) => { |
| 101 | + const refusal = viewContainerNameRefusal(entry, 'manifest', ownerId); |
| 102 | + if (!refusal) return; |
| 103 | + rows.push({ |
| 104 | + path: `${at}[${index}]`, |
| 105 | + code: refusal.code, |
| 106 | + httpStatus: refusal.httpStatus, |
| 107 | + message: refusal.message, |
| 108 | + }); |
| 109 | + }); |
| 110 | + } |
| 111 | + return rows; |
| 112 | +} |
0 commit comments