|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectui#11266 — one `ObjectFormSchema.subforms[].columns` entry is the spec's |
| 11 | + * `InlineGridColumnSchema`, by reference. |
| 12 | + * |
| 13 | + * `@objectstack/spec` 17.6.0 judges `FormViewSchema.subforms[].columns` with its |
| 14 | + * closed inline grid column schema (objectstack#20927). The mirror held |
| 15 | + * `z.array(z.any())` there, so `objectui validate` accepted a column with an |
| 16 | + * undeclared key, and a typed `currency` column carrying `scale`, both of which |
| 17 | + * `os validate` refuses. Two authoring doors gave two verdicts on one document. |
| 18 | + * |
| 19 | + * An authored document reaches `subforms` through the object-view `form` slot: |
| 20 | + * `subforms` is a form-VIEW member, which the `object-form` row refuses in its |
| 21 | + * `properties` bag (objectui#10859 batch 4), and the slot is this mirror minus |
| 22 | + * its identity keys. So the face rows below author it there. The mirror rows |
| 23 | + * parse the flat node as `ObjectForm` reads it. |
| 24 | + * |
| 25 | + * ## What is pinned |
| 26 | + * |
| 27 | + * - BY REFERENCE: a column's schema IS the spec schema as it crosses the |
| 28 | + * import boundary, so a faithful hand copy turns this red. The TypeScript |
| 29 | + * face IS the spec's `InlineGridColumn` (judged by `tsc -p tsconfig.test.json`). |
| 30 | + * - THE VERDICTS, on the tolerant face (`safeValidateSchema`, which |
| 31 | + * `objectui validate` runs) and on the strict authoring face. An undeclared |
| 32 | + * column key is refused at the column, with the key named. A `scale` on a |
| 33 | + * column that DECLARES `type: 'currency'` is refused at that `scale`. A bare |
| 34 | + * field-name string is refused at the column. A column `{ name }` is |
| 35 | + * accepted: the lit control. |
| 36 | + * - ONE VERDICT ACROSS THE TWO DOORS: each probe column gets the verdict the |
| 37 | + * spec's own `FormViewSchema` gives it, read live in the same run. |
| 38 | + * |
| 39 | + * ⛔ Not refused here, and pinned as ACCEPTED so that reaching for it is a |
| 40 | + * deliberate change: a `scale` on an identity-only column (`{ name, scale }`) |
| 41 | + * whose child field is a currency. Refusing it takes the child object's fields, |
| 42 | + * which are not in the document this mirror judges. The spec's column schema |
| 43 | + * accepts it too; `defineStack` refuses it at publish, by resolving `name` |
| 44 | + * through `childObject`, and `plugin-form`'s `hydrateColumns` reports it at |
| 45 | + * render (objectui#10783). |
| 46 | + */ |
| 47 | + |
| 48 | +import { describe, it, expect } from 'vitest'; |
| 49 | +import { |
| 50 | + InlineGridColumnSchema as SpecInlineGridColumnSchema, |
| 51 | + type InlineGridColumn as SpecInlineGridColumn, |
| 52 | +} from '@objectstack/spec/data'; |
| 53 | +import { FormViewSchema as SpecFormViewSchema } from '@objectstack/spec/ui'; |
| 54 | +import type { ObjectFormSchema as DeclaredObjectFormSchema } from '../objectql'; |
| 55 | +import { ObjectFormSchema as ObjectFormMirror } from '../zod/objectql.zod.js'; |
| 56 | +import { safeValidateSchema, StrictAnyComponentSchema } from '../zod/index.zod.js'; |
| 57 | +import { stripImportedDefaults } from '../zod/imported-defaults.js'; |
| 58 | + |
| 59 | +/* ── Type level ───────────────────────────────────────────────────────────── */ |
| 60 | + |
| 61 | +type Equal<A, B> = |
| 62 | + (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false; |
| 63 | +type Expect<T extends true> = T; |
| 64 | + |
| 65 | +type DeclaredSubform = NonNullable<DeclaredObjectFormSchema['subforms']>[number]; |
| 66 | +type DeclaredColumn = NonNullable<DeclaredSubform['columns']>[number]; |
| 67 | + |
| 68 | +// By reference: a declared column IS the spec's authoring type. A local |
| 69 | +// restatement, or the `any` this replaced, turns this red. |
| 70 | +export type _ColumnIsTheSpecInput = Expect<Equal<DeclaredColumn, SpecInlineGridColumn>>; |
| 71 | + |
| 72 | +export const identityOnlyColumnIsAuthorable: DeclaredSubform = { |
| 73 | + childObject: 'order_line', |
| 74 | + columns: [{ name: 'quantity' }, { name: 'amount', type: 'currency', label: 'Amount' }], |
| 75 | +}; |
| 76 | +export const stringColumnIsRefused: DeclaredSubform = { |
| 77 | + childObject: 'order_line', |
| 78 | + // @ts-expect-error a column is an object keyed by `name`, never a bare field-name string |
| 79 | + columns: ['quantity'], |
| 80 | +}; |
| 81 | + |
| 82 | +/* ── Runtime ──────────────────────────────────────────────────────────────── */ |
| 83 | + |
| 84 | +const CHILD = 'order_line'; |
| 85 | + |
| 86 | +/** An authored object-view whose form declares one subform with one column. */ |
| 87 | +const viewWith = (column: unknown) => ({ |
| 88 | + type: 'object-view', |
| 89 | + objectName: 'order', |
| 90 | + form: { subforms: [{ childObject: CHILD, columns: [column] }] }, |
| 91 | +}); |
| 92 | +/** The flat `object-form` node as `ObjectForm` reads it, with the same subform. */ |
| 93 | +const flatWith = (column: unknown) => ({ |
| 94 | + type: 'object-form', |
| 95 | + objectName: 'order', |
| 96 | + mode: 'create', |
| 97 | + subforms: [{ childObject: CHILD, columns: [column] }], |
| 98 | +}); |
| 99 | +/** The spec's form view with the same subform: the other door's verdict. */ |
| 100 | +const specFormViewWith = (column: unknown) => ({ |
| 101 | + type: 'simple', |
| 102 | + subforms: [{ childObject: CHILD, columns: [column] }], |
| 103 | +}); |
| 104 | + |
| 105 | +const VALID = { name: 'quantity' }; |
| 106 | +const BOGUS_KEY = { name: 'quantity', bogusKey: 1 }; |
| 107 | +const TYPED_CURRENCY_WITH_SCALE = { name: 'amount', type: 'currency', scale: 2 }; |
| 108 | +const IDENTITY_ONLY_WITH_SCALE = { name: 'amount', scale: 2 }; |
| 109 | +const BARE_STRING = 'quantity'; |
| 110 | + |
| 111 | +/** The column's own position on the object-view face. */ |
| 112 | +const VIEW_COLUMN = ['form', 'subforms', 0, 'columns', 0]; |
| 113 | +/** …and on the flat mirror. */ |
| 114 | +const FLAT_COLUMN = ['subforms', 0, 'columns', 0]; |
| 115 | + |
| 116 | +type Face = { name: string; parse: (doc: unknown) => ReturnType<typeof safeValidateSchema> }; |
| 117 | +const FACES: ReadonlyArray<Face> = [ |
| 118 | + { name: 'tolerant (`safeValidateSchema`, which `objectui validate` runs)', parse: (doc) => safeValidateSchema(doc) }, |
| 119 | + { name: 'strict authoring face', parse: (doc) => StrictAnyComponentSchema.safeParse(doc) as ReturnType<typeof safeValidateSchema> }, |
| 120 | +]; |
| 121 | + |
| 122 | +/** One column's schema on the mirror, unwrapped from `subforms`' and `columns`' `optional` / `array`. */ |
| 123 | +const mirrorColumnSchema = () => ObjectFormMirror.shape.subforms.unwrap().element.shape.columns.unwrap().element; |
| 124 | + |
| 125 | +describe('objectui#11266 — `subforms[].columns` is the spec\'s `InlineGridColumnSchema`, by reference', () => { |
| 126 | + it('a column\'s schema IS the spec schema as it crosses the import boundary', () => { |
| 127 | + expect(mirrorColumnSchema()).toBe(stripImportedDefaults(SpecInlineGridColumnSchema)); |
| 128 | + // The crossing is the identity here (the column schema carries no default), |
| 129 | + // so the member is the spec's own object. |
| 130 | + expect(mirrorColumnSchema()).toBe(SpecInlineGridColumnSchema); |
| 131 | + }); |
| 132 | + |
| 133 | + describe.each(FACES)('on the $name', ({ parse }) => { |
| 134 | + it('CONTROL: a column `{ name }` is accepted', () => { |
| 135 | + const parsed = parse(viewWith(VALID)); |
| 136 | + expect(parsed.success, JSON.stringify(parsed.error?.issues)).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it('an undeclared column key is refused at the column, with the key named', () => { |
| 140 | + const parsed = parse(viewWith(BOGUS_KEY)); |
| 141 | + expect(parsed.success).toBe(false); |
| 142 | + expect(parsed.error!.issues).toHaveLength(1); |
| 143 | + const [issue] = parsed.error!.issues; |
| 144 | + expect(issue.code).toBe('unrecognized_keys'); |
| 145 | + expect(issue.path).toEqual(VIEW_COLUMN); |
| 146 | + expect((issue as { keys?: string[] }).keys).toEqual(['bogusKey']); |
| 147 | + }); |
| 148 | + |
| 149 | + it('a `scale` on a column that declares `type: \'currency\'` is refused at that `scale`', () => { |
| 150 | + const parsed = parse(viewWith(TYPED_CURRENCY_WITH_SCALE)); |
| 151 | + expect(parsed.success).toBe(false); |
| 152 | + expect(parsed.error!.issues).toHaveLength(1); |
| 153 | + const [issue] = parsed.error!.issues; |
| 154 | + expect(issue.code).toBe('custom'); |
| 155 | + expect(issue.path).toEqual([...VIEW_COLUMN, 'scale']); |
| 156 | + // The refusal is the spec's own, sentence included: the same column |
| 157 | + // judged by the spec's schema in this run yields the same message. |
| 158 | + const spec = SpecInlineGridColumnSchema.safeParse(TYPED_CURRENCY_WITH_SCALE); |
| 159 | + expect(spec.success).toBe(false); |
| 160 | + expect(issue.message).toBe(spec.error!.issues[0].message); |
| 161 | + }); |
| 162 | + |
| 163 | + it('a bare field-name string is refused at the column', () => { |
| 164 | + const parsed = parse(viewWith(BARE_STRING)); |
| 165 | + expect(parsed.success).toBe(false); |
| 166 | + expect(parsed.error!.issues).toHaveLength(1); |
| 167 | + const [issue] = parsed.error!.issues; |
| 168 | + expect(issue.code).toBe('invalid_type'); |
| 169 | + expect(issue.path).toEqual(VIEW_COLUMN); |
| 170 | + }); |
| 171 | + |
| 172 | + it('⛔ an identity-only column with `scale` is accepted: the currency is the child field\'s, which this document does not carry', () => { |
| 173 | + const parsed = parse(viewWith(IDENTITY_ONLY_WITH_SCALE)); |
| 174 | + expect(parsed.success, JSON.stringify(parsed.error?.issues)).toBe(true); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + it('the flat mirror gives the same verdicts at the same column', () => { |
| 179 | + expect(ObjectFormMirror.safeParse(flatWith(VALID)).success).toBe(true); |
| 180 | + |
| 181 | + const bogus = ObjectFormMirror.safeParse(flatWith(BOGUS_KEY)); |
| 182 | + expect(bogus.success).toBe(false); |
| 183 | + expect(bogus.error!.issues.map((i) => [i.code, i.path])).toEqual([['unrecognized_keys', FLAT_COLUMN]]); |
| 184 | + |
| 185 | + const currency = ObjectFormMirror.safeParse(flatWith(TYPED_CURRENCY_WITH_SCALE)); |
| 186 | + expect(currency.success).toBe(false); |
| 187 | + expect(currency.error!.issues.map((i) => [i.code, i.path])).toEqual([['custom', [...FLAT_COLUMN, 'scale']]]); |
| 188 | + }); |
| 189 | + |
| 190 | + it.each([ |
| 191 | + ['a column `{ name }`', VALID, true], |
| 192 | + ['an undeclared column key', BOGUS_KEY, false], |
| 193 | + ['a typed currency column with `scale`', TYPED_CURRENCY_WITH_SCALE, false], |
| 194 | + ['an identity-only column with `scale`', IDENTITY_ONLY_WITH_SCALE, true], |
| 195 | + ['a bare field-name string', BARE_STRING, false], |
| 196 | + ] as const)('one verdict across the two doors: %s', (_label, column, expected) => { |
| 197 | + // The other door, read live: the spec's form view judging the same column. |
| 198 | + const spec = SpecFormViewSchema.safeParse(specFormViewWith(column)); |
| 199 | + expect(spec.success).toBe(expected); |
| 200 | + expect(safeValidateSchema(viewWith(column)).success).toBe(spec.success); |
| 201 | + }); |
| 202 | +}); |
0 commit comments