diff --git a/.changeset/34986-skills-seed-rows-obey-validation-rules.md b/.changeset/34986-skills-seed-rows-obey-validation-rules.md new file mode 100644 index 00000000000..338d6539403 --- /dev/null +++ b/.changeset/34986-skills-seed-rows-obey-validation-rules.md @@ -0,0 +1,8 @@ +--- +'@objectstack/skills': patch +--- + +docs(skills): `objectstack-data`'s seeds reference says seed rows obey the object's validation rules (all but `state_machine`) as they load: a rule reading a parent roll-up sees the total earlier rows left, a seeded roll-up value is stored as written, and `os verify`, not `os validate`, fails on a dropped row. One eval case covers it; the file pays in deleted restatement and its token ceiling does not move. + +Clause-②: no +Metadata-protocol: none diff --git a/content/docs/data-modeling/seed-data.mdx b/content/docs/data-modeling/seed-data.mdx index 8ee1bd8992a..7d6944ae8ec 100644 --- a/content/docs/data-modeling/seed-data.mdx +++ b/content/docs/data-modeling/seed-data.mdx @@ -841,7 +841,7 @@ export a single aggregate array. ``` src/ data/ - index.ts ← exports SeedData array in dependency order + index.ts ← exports the SeedData array (the loader orders it) accounts.seed.ts contacts.seed.ts leads.seed.ts @@ -920,7 +920,8 @@ company names, email addresses, and values — not `foo`, `bar`, or `test123`. ### One file per object Split large seed payloads into `{object}.seed.ts` files. A single `index.ts` that -re-exports and orders them keeps the entry point clean. +re-exports them keeps the entry point clean; lookups and `dependsOn` order the +load, and array position only breaks ties. --- diff --git a/skills/objectstack-data/evals/hooks-security-seeds-datasources.json b/skills/objectstack-data/evals/hooks-security-seeds-datasources.json index b2d81a7d09e..7c726d4e75d 100644 --- a/skills/objectstack-data/evals/hooks-security-seeds-datasources.json +++ b/skills/objectstack-data/evals/hooks-security-seeds-datasources.json @@ -310,6 +310,22 @@ "must_contain": ["CrossDatasourceTransactionWriteError", "default datasource"], "must_not_contain": [] } + }, + { + "id": 32, + "prompt": "`payment` has a validation rule that refuses a payment above the invoice's open balance (`record.amount > record.invoice.amount - record.invoice.amount_paid`); `invoice.amount_paid` is a `sum` roll-up of its payments. Seed demo data: invoice `INV-1` for 4800, fully paid in two payments, and `INV-2` for 5000 with 2000 paid.", + "expected_output": "Two `defineSeed` sets. The invoice rows carry `number` and `amount` and no roll-up value: a seed write stores a supplied `amount_paid` as written, the rule then reads a balance already spent, and both payments on the fully paid `INV-1` are refused (the 2000 on `INV-2` still fits its 3000 open balance). The payments name their invoice by natural key (`invoice: 'INV-1'`) and each fits what the rows before it left: two that sum to 4800 on `INV-1`, one of 2000 on `INV-2`; the roll-up then computes the paid totals. Does not say seed rows skip validation rules (only `state_machine` is skipped) and does not weaken the rule to make the seed load; `os verify` fails on a dropped seed row.", + "files": [], + "assertions": { + "must_contain": [ + "defineSeed", + "invoice: 'INV-1'" + ], + "must_not_contain": [ + "amount_paid: 4800", + "amount_paid: 2000" + ] + } } ] } diff --git a/skills/objectstack-data/references/seeds.md b/skills/objectstack-data/references/seeds.md index 34d4b7c316a..3c9597261e0 100644 --- a/skills/objectstack-data/references/seeds.md +++ b/skills/objectstack-data/references/seeds.md @@ -1,7 +1,7 @@ ## Seed Data & Fixtures (`defineSeed()`) Object definition and seed data live together — a `*.object.ts` usually -pairs with a `*.seed.ts` (fixtures, reference rows, bootstrap data). +pairs with a `*.seed.ts`. > The factory is `defineSeed` — **not** `defineDataset`, which is the > unrelated ADR-0021 analytics semantic layer (`@objectstack/spec/ui`). @@ -85,14 +85,14 @@ mapping entry's `applyOn` (objectstack-api). defineSeed(Invoice, { externalId: 'number', mode: 'upsert', - columns: { status: { applyOn: 'create' } }, // the payment job owns it after the first seed + columns: { status: { applyOn: 'create' } }, records: [{ number: 'INV-001', status: 'draft', amount: 1200 }], }); ``` ### `externalId` selection -Pick a stable natural key. **Never use `id`** for generated UUIDs — they +**Never use `id`** for generated UUIDs — they differ across environments. | Scenario | Key | @@ -100,19 +100,17 @@ differ across environments. | Named entities (country, currency) | `'code'` / `'slug'` | | Users / contacts | `'email'` | | Externally sourced | `'external_id'` | -| Generic | `'name'` (default) | ### Relationship references -For `lookup` fields, supply the **natural key** of the target record (not -its UUID). The loader resolves it at load time, and lookups order the load: +For `lookup` fields, supply the target record's **natural key**. The +loader resolves it, and lookups order the load: a child loads after its parent however the array lists them (array order only breaks ties). > If a lookup value matches no natural key, the loader falls back to -> resolving it as the target's `id` — so a reference to an existing record -> by internal id resolves instead of dangling to null. Rely on it only for -> records you didn't seed (e.g. a system user). +> resolving it as the target's `id`. Rely on it only for records you didn't +> seed (e.g. a system user). ```typescript const contacts = defineSeed(Contact, { @@ -120,7 +118,7 @@ const contacts = defineSeed(Contact, { records: [{ email: 'john@acme.example.com', first_name: 'John', - account: 'Acme Corporation', // natural key of an Account record + account: 'Acme Corporation', }], }); ``` @@ -130,15 +128,21 @@ A `beforeInsert` hook reading a seeded object no lookup links to needs object, one no dataset of the load seeds (misspelt, or `env`/`locale`-scoped out), or a cycle refuses the whole load; nothing is written. +**Seed rows obey the object's validation rules** (all but `state_machine`); a +refused row never lands. `os validate` does not run them on seed rows; +`os verify` fails on each dropped row. A rule reading a parent roll-up +(`record.invoice.amount_paid`) sees the total earlier rows left, so children +must fit in record order. ⛔ Never seed the roll-up: a seeded `amount_paid` is +stored as written, and a cap on payments refuses those it counts. + ### Dynamic values (CEL) Any field value may be a CEL expression evaluated at install time against a single per-load pinned `now`. This is the **only** correct way to author time-based or identity-derived seed values — `new Date()` ships the package -author's clock to every customer and breaks build determinism. +author's clock to every customer. ```typescript -import { defineSeed } from '@objectstack/spec/data'; import { cel } from '@objectstack/spec'; defineSeed(Opportunity, { @@ -165,10 +169,10 @@ produce byte-identical `dist/objectstack.json`. |:---------|:----| | TypeScript: `defineSeed()`, never `SeedSchema.parse()` | Lose compile-time field checks otherwise | -**Fictional is fine; malformed is not.** A seed row is written through the same -field validation as a user's insert, and a value the field type refuses never -lands — the table loads short or empty while sibling rows point at records that -do not exist. `os validate` / `os build` refuse such a value first, at the +**Fictional is fine; malformed is not.** A seed row passes the same field +validation as a user's insert; a refused value leaves the table short or empty +while sibling rows point at records that do not exist. +`os validate` / `os build` refuse such a value first, at the record's field — run them before trusting the load. So never mask or placeholder a sample value: no `138****4207`, `***`, `XXX`, `N/A`, `—`, `TBD`. Invent a plausible well-formed one — `+86 138