|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Metadata Type → minimal **valid create seed** (single source of truth). |
| 5 | + * |
| 6 | + * The recurring "designer creates a minimal item that fails spec validation on |
| 7 | + * save" family (a new dashboard lacked `layout`; a new `script` action lacks |
| 8 | + * `body`; a report carried stale `objectName`/`columns`) has one root cause: |
| 9 | + * the create-form's default shape was invented client-side (objectui's |
| 10 | + * `createDefaults`) and drifted from the spec's required fields, with nothing |
| 11 | + * validating the two against each other. |
| 12 | + * |
| 13 | + * This registry is the authoritative minimal shape a freshly-created item of |
| 14 | + * each type should carry — co-located with the schemas in `packages/spec` and |
| 15 | + * asserted valid by `metadata-create-seeds.test.ts`. When a schema tightens a |
| 16 | + * requirement, this seed (and the test) is right next to it, so the create |
| 17 | + * path can't silently break. Consumers (the Studio designer via `/meta/types`, |
| 18 | + * the CLI, API clients) should derive their create defaults from here instead |
| 19 | + * of re-inventing them. |
| 20 | + * |
| 21 | + * Each seed is a COMPLETE minimal valid object including placeholder identity |
| 22 | + * (`name`/`label`/object binding); a create flow overrides those from user |
| 23 | + * input. Structural defaults (empty collections, the required `type`/`body`, |
| 24 | + * …) are the part that matters and must not drift. |
| 25 | + */ |
| 26 | + |
| 27 | +import type { MetadataType } from './metadata-plugin.zod'; |
| 28 | + |
| 29 | +const PLACEHOLDER_OBJECT = 'example_object'; |
| 30 | + |
| 31 | +/** |
| 32 | + * Built-in minimal create seeds. Keyed by metadata type; every entry is |
| 33 | + * validated against its `metadata-type-schemas` schema by the test. |
| 34 | + * |
| 35 | + * Canvas-create types whose full shape is built INTERACTIVELY (e.g. `report` |
| 36 | + * picks its dataset/measures on the canvas, `object` adds fields on the canvas) |
| 37 | + * are intentionally absent — their minimal shape isn't a static literal. The |
| 38 | + * test documents these exclusions. |
| 39 | + */ |
| 40 | +const BUILTIN_METADATA_CREATE_SEEDS: Partial<Record<MetadataType, unknown>> = { |
| 41 | + dashboard: { |
| 42 | + name: 'new_dashboard', |
| 43 | + label: 'New Dashboard', |
| 44 | + widgets: [], |
| 45 | + }, |
| 46 | + action: { |
| 47 | + name: 'new_action', |
| 48 | + label: 'New Action', |
| 49 | + // `type` defaults to 'script', which the spec requires to carry an |
| 50 | + // executable body or target — seed a no-op L2 body so create round-trips. |
| 51 | + type: 'script', |
| 52 | + body: { language: 'js', source: 'return { success: true };' }, |
| 53 | + }, |
| 54 | + page: { |
| 55 | + name: 'new_page', |
| 56 | + label: 'New Page', |
| 57 | + object: PLACEHOLDER_OBJECT, |
| 58 | + type: 'list', |
| 59 | + kind: 'full', |
| 60 | + regions: [], |
| 61 | + }, |
| 62 | + view: { |
| 63 | + name: `${PLACEHOLDER_OBJECT}.new_view`, |
| 64 | + object: PLACEHOLDER_OBJECT, |
| 65 | + viewKind: 'list', |
| 66 | + label: 'New View', |
| 67 | + config: { type: 'grid', columns: [], data: { provider: 'object', object: PLACEHOLDER_OBJECT } }, |
| 68 | + }, |
| 69 | + flow: { |
| 70 | + name: 'new_flow', |
| 71 | + label: 'New Flow', |
| 72 | + type: 'autolaunched', |
| 73 | + nodes: [], |
| 74 | + edges: [], |
| 75 | + }, |
| 76 | + validation: { |
| 77 | + name: 'new_validation', |
| 78 | + label: 'New Validation', |
| 79 | + message: 'This record is invalid.', |
| 80 | + type: 'script', |
| 81 | + active: true, |
| 82 | + events: ['insert', 'update'], |
| 83 | + priority: 10, |
| 84 | + severity: 'error', |
| 85 | + condition: 'false', |
| 86 | + }, |
| 87 | + hook: { |
| 88 | + name: 'new_hook', |
| 89 | + label: 'New Hook', |
| 90 | + object: PLACEHOLDER_OBJECT, |
| 91 | + events: [], |
| 92 | + }, |
| 93 | + dataset: { |
| 94 | + name: 'new_dataset', |
| 95 | + label: 'New Dataset', |
| 96 | + object: PLACEHOLDER_OBJECT, |
| 97 | + dimensions: [], |
| 98 | + // A dataset needs at least one measure to be useful; seed a count. |
| 99 | + measures: [{ name: 'count', label: 'Count', aggregate: 'count' }], |
| 100 | + }, |
| 101 | + object: { |
| 102 | + name: 'new_object', |
| 103 | + label: 'New Object', |
| 104 | + pluralLabel: 'New Objects', |
| 105 | + fields: {}, |
| 106 | + }, |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * Return the authoritative minimal create seed for a metadata type, or |
| 111 | + * `undefined` when none is registered (caller falls back to `{}`). The |
| 112 | + * returned object is a fresh deep clone so callers may mutate it freely. |
| 113 | + */ |
| 114 | +export function getMetadataCreateSeed(type: string): unknown | undefined { |
| 115 | + const seed = BUILTIN_METADATA_CREATE_SEEDS[type as MetadataType]; |
| 116 | + return seed === undefined ? undefined : structuredClone(seed); |
| 117 | +} |
| 118 | + |
| 119 | +/** Snapshot of every type that has a built-in create seed. */ |
| 120 | +export function listMetadataCreateSeedTypes(): string[] { |
| 121 | + return Object.keys(BUILTIN_METADATA_CREATE_SEEDS).sort(); |
| 122 | +} |
0 commit comments