-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathview.zod.ts
More file actions
1266 lines (1155 loc) · 54.7 KB
/
Copy pathview.zod.ts
File metadata and controls
1266 lines (1155 loc) · 54.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import { ProtectionSchema } from '../shared/protection.zod';
import { MetadataProtectionFields } from '../kernel/metadata-protection.zod';
import { SnakeCaseIdentifierSchema } from '../shared/identifiers.zod';
import { ExpressionInputSchema } from '../shared/expression.zod';
import { I18nLabelSchema, AriaPropsSchema } from './i18n.zod';
import { SharingConfigSchema } from './sharing.zod';
import { ResponsiveConfigSchema, PerformanceConfigSchema } from './responsive.zod';
import { FieldType, SelectOptionSchema } from '../data/field.zod';
/**
* HTTP Method Enum & HTTP Request Schema
* Migrated to shared/http.zod.ts. Re-exported here for backward compatibility.
*/
import { HttpMethodSchema, HttpRequestSchema } from '../shared/http.zod';
import { lazySchema } from '../shared/lazy-schema';
export { HttpMethodSchema, HttpRequestSchema };
/**
* View Data Source Configuration
* Supports three modes:
* 1. 'object': Standard Protocol - Auto-connects to ObjectStack Metadata and Data APIs
* 2. 'api': Custom API - Explicitly provided API URLs
* 3. 'value': Static Data - Hardcoded data array
*/
export const ViewDataSchema = lazySchema(() => z.discriminatedUnion('provider', [
z.object({
provider: z.literal('object'),
object: z.string().describe('Target object name'),
}),
z.object({
provider: z.literal('api'),
read: HttpRequestSchema.optional().describe('Configuration for fetching data'),
write: HttpRequestSchema.optional().describe('Configuration for submitting data (for forms/editable tables)'),
}),
z.object({
provider: z.literal('value'),
items: z.array(z.unknown()).describe('Static data array'),
}),
/**
* Schema-bound data source — used by standalone forms whose data is
* shaped by a JSON Schema (or Zod-derived schema) rather than by an
* ObjectQL object. Powers the metadata editor, action input dialogs,
* and any Form that is not bound to a CRUD object.
*/
z.object({
provider: z.literal('schema'),
/** Schema identifier (e.g. metadata type name "report"). Resolved at runtime against /meta entries. */
schemaId: z.string().describe('Schema identifier — typically the metadata type name'),
/** Optional inline JSON Schema; when omitted the runtime resolves schemaId from the server. */
schema: z.record(z.string(), z.unknown()).optional().describe('Inline JSON Schema (Draft 2020-12). Optional when schemaId is resolvable.'),
}),
]));
/**
* View Filter Rule Schema
* Standardized filter condition used in list views, tabs, and page-level filters.
* Uses a declarative array-of-objects format: [{ field, operator, value }].
*
* @example
* ```ts
* filter: [
* { field: 'status', operator: 'equals', value: 'active' },
* { field: 'close_date', operator: 'this_quarter' },
* ]
* ```
*/
export const ViewFilterRuleSchema = lazySchema(() => z.object({
/** Field name to filter on */
field: z.string().describe('Field name to filter on'),
/** Filter operator */
operator: z.string().describe('Filter operator (e.g. equals, not_equals, contains, this_quarter)'),
/** Filter value (optional for unary operators like is_null, this_quarter) */
value: z.union([z.string(), z.number(), z.boolean(), z.null(), z.array(z.union([z.string(), z.number()]))])
.optional().describe('Filter value'),
}).describe('View filter rule'));
export type ViewFilterRule = z.infer<typeof ViewFilterRuleSchema>;
/**
* Column Summary Function Schema
* Aggregation function for column footer (Airtable-style column summaries)
*/
export const ColumnSummarySchema = lazySchema(() => z.enum([
'none',
'count',
'count_empty',
'count_filled',
'count_unique',
'percent_empty',
'percent_filled',
'sum',
'avg',
'min',
'max',
]).describe('Aggregation function for column footer summary'));
/**
* List Column Configuration Schema
* Detailed configuration for individual list view columns
*/
export const ListColumnSchema = lazySchema(() => z.object({
field: z.string().describe('Field name (snake_case)'),
label: I18nLabelSchema.optional().describe('Display label override'),
width: z.number().positive().optional().describe('Column width in pixels'),
align: z.enum(['left', 'center', 'right']).optional().describe('Text alignment'),
hidden: z.boolean().optional().describe('Hide column by default'),
sortable: z.boolean().optional().describe('Allow sorting by this column'),
resizable: z.boolean().optional().describe('Allow resizing this column'),
wrap: z.boolean().optional().describe('Allow text wrapping'),
type: z.string().optional().describe('Renderer type override (e.g., "currency", "date")'),
/** Pinning (Airtable-style frozen columns) */
pinned: z.enum(['left', 'right']).optional().describe('Pin/freeze column to left or right side'),
/** Column Footer Summary (Airtable-style aggregation) */
summary: ColumnSummarySchema.optional().describe('Footer aggregation function for this column'),
/** Interaction */
link: z.boolean().optional().describe('Functions as the primary navigation link (triggers View navigation)'),
action: z.string().optional().describe('Registered Action ID to execute when clicked'),
}));
/**
* List View Selection Configuration
*/
export const SelectionConfigSchema = lazySchema(() => z.object({
type: z.enum(['none', 'single', 'multiple']).default('none').describe('Selection mode'),
}));
/**
* List View Pagination Configuration
*/
export const PaginationConfigSchema = lazySchema(() => z.object({
pageSize: z.number().int().positive().default(25).describe('Number of records per page'),
pageSizeOptions: z.array(z.number().int().positive()).optional().describe('Available page size options'),
}));
/**
* Row Height / Density Schema (Airtable-style)
* Controls the visual density of rows in a list view.
*/
export const RowHeightSchema = lazySchema(() => z.enum([
'compact', // Minimal padding, single line
'short', // Reduced padding
'medium', // Default padding
'tall', // Extra padding, multi-line preview
'extra_tall', // Maximum padding, rich content preview
]).describe('Row height / density setting for list view'));
/**
* Grouping Field Configuration
* Defines a single grouping level for record grouping.
*/
export const GroupingFieldSchema = lazySchema(() => z.object({
field: z.string().describe('Field name to group by'),
order: z.enum(['asc', 'desc']).default('asc').describe('Group sort order'),
collapsed: z.boolean().default(false).describe('Collapse groups by default'),
}));
/**
* Grouping Configuration Schema (Airtable-style)
* Supports multi-level grouping for grid/gallery views.
*/
export const GroupingConfigSchema = lazySchema(() => z.object({
fields: z.array(GroupingFieldSchema).min(1).describe('Fields to group by (supports up to 3 levels)'),
}).describe('Record grouping configuration'));
/**
* Gallery View Configuration (Airtable-style)
* Configures card layout for gallery/card views.
*/
export const GalleryConfigSchema = lazySchema(() => z.object({
coverField: z.string().optional().describe('Attachment/image field to display as card cover'),
coverFit: z.enum(['cover', 'contain']).default('cover').describe('Image fit mode for card cover'),
cardSize: z.enum(['small', 'medium', 'large']).default('medium').describe('Card size in gallery view'),
titleField: z.string().optional().describe('Field to display as card title'),
visibleFields: z.array(z.string()).optional().describe('Fields to display on card body'),
}).describe('Gallery/card view configuration'));
/**
* Timeline View Configuration (Airtable-style)
* Configures timeline/chronological views.
*/
export const TimelineConfigSchema = lazySchema(() => z.object({
startDateField: z.string().describe('Field for timeline item start date'),
endDateField: z.string().optional().describe('Field for timeline item end date'),
titleField: z.string().describe('Field to display as timeline item title'),
groupByField: z.string().optional().describe('Field to group timeline rows'),
colorField: z.string().optional().describe('Field to determine item color'),
scale: z.enum(['hour', 'day', 'week', 'month', 'quarter', 'year']).default('week').describe('Default timeline scale'),
}).describe('Timeline view configuration'));
/**
* View Sharing Configuration (Airtable-style)
* Defines who can see and modify a view.
*/
export const ViewSharingSchema = lazySchema(() => z.object({
type: z.enum(['personal', 'collaborative']).default('collaborative').describe('View ownership type'),
lockedBy: z.string().optional().describe('User who locked the view configuration'),
}).describe('View sharing and access configuration'));
/**
* Row Color Configuration (Airtable-style)
* Defines how rows are colored based on field values.
*/
export const RowColorConfigSchema = lazySchema(() => z.object({
field: z.string().describe('Field to derive color from (typically a select/status field)'),
colors: z.record(z.string(), z.string()).optional().describe('Map of field value to color (hex/token)'),
}).describe('Row color configuration based on field values'));
/**
* Visualization Type Schema
* Whitelist of visualization types the user can switch between.
* Maps to Airtable's "Visualizations" setting in Appearance panel.
*/
export const VisualizationTypeSchema = lazySchema(() => z.enum([
'grid',
'kanban',
'gallery',
'calendar',
'timeline',
'gantt',
'map',
'chart',
]).describe('Visualization type that users can switch to'));
/**
* User Actions Configuration Schema (Airtable Interface parity)
* Controls which interactive actions are available to users in the view toolbar.
* Each boolean toggles the corresponding toolbar element on/off.
*
* @see Airtable Interface → "User actions" panel
*/
export const UserActionsConfigSchema = lazySchema(() => z.object({
sort: z.boolean().default(true).describe('Allow users to sort records'),
search: z.boolean().default(true).describe('Allow users to search records'),
filter: z.boolean().default(true).describe('Allow users to filter records'),
rowHeight: z.boolean().default(true).describe('Allow users to toggle row height/density'),
addRecordForm: z.boolean().default(false).describe('Add records through a form instead of inline'),
buttons: z.array(z.string()).optional().describe('Custom action button IDs to show in the toolbar'),
}).describe('User action toggles for the view toolbar'));
/**
* Appearance Configuration Schema (Airtable Interface parity)
* Controls visual presentation options for the view.
*
* @see Airtable Interface → "Appearance" panel
*/
export const AppearanceConfigSchema = lazySchema(() => z.object({
showDescription: z.boolean().default(true).describe('Show the view description text'),
allowedVisualizations: z.array(VisualizationTypeSchema).optional()
.describe('Whitelist of visualization types users can switch between (e.g. ["grid", "gallery", "kanban"])'),
}).describe('Appearance and visualization configuration'));
/**
* View Tab Schema (Airtable Interface parity)
* Defines a tab in a multi-tab view interface.
* Each tab references a named list view and can be ordered, pinned, or set as default.
*
* @see Airtable Interface → "Tabs" panel
*/
export const ViewTabSchema = lazySchema(() => z.object({
name: SnakeCaseIdentifierSchema.describe('Tab identifier (snake_case)'),
label: I18nLabelSchema.optional().describe('Display label'),
icon: z.string().optional().describe('Tab icon name'),
view: z.string().optional().describe('Referenced list view name from listViews'),
filter: z.array(ViewFilterRuleSchema).optional().describe('Tab-specific filter criteria'),
order: z.number().int().min(0).optional().describe('Tab display order'),
pinned: z.boolean().default(false).describe('Pin tab (cannot be removed by users)'),
isDefault: z.boolean().default(false).describe('Set as the default active tab'),
visible: z.boolean().default(true).describe('Tab visibility'),
}).describe('Tab configuration for multi-tab view interface'));
/**
* User Filter Field Schema (ADR-0047)
* One field exposed as a quick-filter control in the end-user filter bar.
* Rendering details (widget, options) default to inference from the field
* definition on the source object — authors only override when needed.
*
* @see Airtable Interface → "User filters" panel (Dropdowns element)
*/
export const UserFilterFieldSchema = lazySchema(() => z.object({
field: z.string().describe('Field name on the source object (must exist — checked by reference diagnostics)'),
label: I18nLabelSchema.optional().describe('Display label override (defaults to the field label)'),
type: z.enum(['select', 'multi-select', 'boolean', 'date-range', 'text']).optional()
.describe('Filter control type. Omit to infer from the field definition'),
options: z.array(z.object({
value: z.union([z.string(), z.number(), z.boolean()]).describe('Option value'),
label: I18nLabelSchema.describe('Option label'),
color: z.string().optional().describe('Option color token/hex'),
})).optional().describe('Static options. Omit to derive from the field definition (select options / lookup records)'),
showCount: z.boolean().optional().describe('Show per-option record counts'),
defaultValues: z.array(z.union([z.string(), z.number(), z.boolean()])).optional()
.describe('Pre-selected values when the view loads'),
}).describe('Quick-filter field configuration'));
/**
* User Filters Schema (ADR-0047, Airtable Interface parity)
* The end-user-facing quick-filter surface above a list. The author picks the
* element style and which fields/presets are exposed; end users combine them
* at runtime (session-scoped — selections never persist as metadata).
*
* Distinct from `ListView.filter` (the always-on base criteria) and from the
* advanced filter builder (`userActions.filter` toggle).
*
* @see Airtable Interface → "User filters" panel (Elements: tabs / dropdowns)
*/
export const UserFiltersSchema = lazySchema(() => z.object({
// `toggle` is DEPRECATED (ADR-0047 §3.4a): it overlaps `tabs` (presets) and
// `dropdown` (per-field values) without adding expressive power, needs
// per-field defaultValues to be useful, and authoring tooling no longer
// offers it (None / Tabs / Dropdown only). Kept in the enum so existing
// configs keep rendering; do not author new `toggle` filters.
element: z.enum(['dropdown', 'tabs', 'toggle']).default('dropdown')
.describe('Filter control style: "dropdown" (per-field value selectors) or "tabs" (named presets). "toggle" is deprecated.'),
fields: z.array(UserFilterFieldSchema).optional()
.describe('Fields exposed as quick filters (dropdown/toggle elements)'),
tabs: z.array(ViewTabSchema).optional()
.describe('Named filter presets rendered as tabs (tabs element). Reuses ViewTabSchema'),
showAllRecords: z.boolean().optional()
.describe('Show an "All records" tab before the presets (tabs element)'),
}).describe('End-user quick-filter configuration (Airtable "User filters" parity)'));
/**
* Add Record Configuration Schema (Airtable Interface parity)
* Configures the "Add Record" entry point for a list view.
*
* @see Airtable Interface → "+ Add record" button
*/
export const AddRecordConfigSchema = lazySchema(() => z.object({
enabled: z.boolean().default(true).describe('Show the add record entry point'),
position: z.enum(['top', 'bottom', 'both']).default('bottom').describe('Position of the add record button'),
mode: z.enum(['inline', 'form', 'modal']).default('inline').describe('How to add a new record'),
formView: z.string().optional().describe('Named form view to use when mode is "form" or "modal"'),
}).describe('Add record entry point configuration'));
/**
* Kanban Settings
*/
export const KanbanConfigSchema = lazySchema(() => z.object({
groupByField: z.string().describe('Field to group columns by (usually status/select)'),
summarizeField: z.string().optional().describe('Field to sum at top of column (e.g. amount)'),
columns: z.array(z.string()).describe('Fields to show on cards'),
}));
/**
* List Chart View Configuration (Airtable-style)
* Configures aggregate chart visualizations (bar/line/pie/area/scatter)
* when used as a `type: 'chart'` ListView. Distinct from the full-featured
* `ChartConfigSchema` in `chart.zod.ts` (which is for embedded reports).
*/
export const ListChartConfigSchema = lazySchema(() => z.object({
chartType: z.enum(['bar', 'line', 'pie', 'area', 'scatter']).default('bar').describe('Chart visualisation type'),
/**
* ADR-0021 — the semantic-layer `dataset` this chart binds to. Selects
* dimensions/measures BY NAME so the chart's numbers stay consistent with
* every other surface using the same dataset. This is the single author-facing
* shape (the legacy inline `xAxisField` + `yAxisFields` + `aggregation` query
* was removed in the cutover).
*/
dataset: SnakeCaseIdentifierSchema.describe('Dataset name to bind (ADR-0021)'),
/** Dimension names (from the dataset) for X / group / split. */
dimensions: z.array(z.string()).optional().describe('Dimension names — X/group/split'),
/** Measure names (from the dataset) for the value axis. */
values: z.array(z.string()).min(1).describe('Measure names — Y (at least one)'),
}).describe('List chart view configuration'));
/**
* Calendar Settings
*/
export const CalendarConfigSchema = lazySchema(() => z.object({
startDateField: z.string(),
endDateField: z.string().optional(),
titleField: z.string(),
colorField: z.string().optional(),
}));
/**
* Quick filter dimension for the Gantt toolbar.
*/
export const GanttQuickFilterSchema = lazySchema(() => z.object({
field: z.string().describe('Record field / dot-path the dimension filters on'),
label: z.string().optional().describe('Trigger label (falls back to the field label)'),
options: z.array(z.union([
z.string(),
z.object({
value: z.union([z.string(), z.number()]),
label: z.string().optional(),
}),
])).optional().describe('Explicit option override for fixed enums'),
}));
/**
* Gantt Settings
*
* Beyond the core timeline fields, the renderer supports a two-level
* parent/child hierarchy (parentField/typeField), planned-vs-actual baselines,
* dynamic grouping, a resource/workload view, hover tooltips and quick filters.
*/
export const GanttConfigSchema = lazySchema(() => z.object({
startDateField: z.string(),
endDateField: z.string(),
titleField: z.string(),
progressField: z.string().optional(),
dependenciesField: z.string().optional(),
colorField: z.string().optional().describe('Field that drives the bar color'),
// Two-level hierarchy: a parent task id (summary bar) and a row type.
parentField: z.string().optional().describe('Field holding the parent task id (builds the summary → step tree)'),
typeField: z.string().optional().describe('Field whose value maps to task/summary/milestone'),
// Planned-vs-actual reference bars.
baselineStartField: z.string().optional().describe('Baseline (planned) start field'),
baselineEndField: z.string().optional().describe('Baseline (planned) end field'),
// Dynamic grouping: bucket leaf tasks under one synthesized summary per value.
groupByField: z.string().optional().describe('Field to group leaf tasks by (synthesized summary rows)'),
// Resource / workload view.
resourceView: z.boolean().optional().describe('Render a per-resource workload histogram instead of the timeline'),
assigneeField: z.string().optional().describe('Resource field to bucket load by (resource view)'),
effortField: z.string().optional().describe('Per-task load units (resource view; default 1)'),
capacity: z.number().optional().describe('Per-resource capacity ceiling; loads above this flag overload'),
// Hover tooltip + quick filters.
tooltipFields: z.array(z.union([
z.string(),
z.object({ field: z.string(), label: z.string().optional() }),
])).optional().describe('Fields to surface in the hover tooltip, in display order'),
quickFilters: z.array(GanttQuickFilterSchema).optional().describe('Multi-select filter dropdowns rendered above the chart'),
autoZoomToFilter: z.boolean().optional().describe('When true (default), filtering zooms the range to the filtered tasks'),
}));
/**
* Navigation Mode Enum
* Defines how to navigate to the detail view from a list item.
*/
export const NavigationModeSchema = lazySchema(() => z.enum([
'page', // Navigate to a new route (default)
'drawer', // Open details in a side drawer/panel
'modal', // Open details in a modal dialog
'split', // Show details side-by-side with the list (master-detail)
'popover', // Show details in a popover (lightweight)
'new_window', // Open in new browser tab/window
'none' // No navigation (read-only list)
]));
/**
* Navigation Configuration Schema
*/
export const NavigationConfigSchema = lazySchema(() => z.object({
mode: NavigationModeSchema.default('page'),
/** Target View Config */
view: z.string().optional().describe('Name of the form view to use for details (e.g. "summary_view", "edit_form")'),
/** Interaction Triggers */
preventNavigation: z.boolean().default(false).describe('Disable standard navigation entirely'),
openNewTab: z.boolean().default(false).describe('Force open in new tab (applies to page mode)'),
/** Dimensions (for modal/drawer) */
width: z.union([z.string(), z.number()]).optional().describe('Width of the drawer/modal (e.g. "600px", "50%")'),
}));
/**
* List View Schema (Expanded)
* Defines how a collection of records is displayed to the user.
*
* **NAMING CONVENTION:**
* View names (when provided) are machine identifiers and must be lowercase snake_case.
*
* @example Standard Grid
* {
* name: "all_active",
* label: "All Active",
* type: "grid",
* columns: ["name", "status", "created_at"],
* filter: [["status", "=", "active"]]
* }
*
* @example Kanban Board
* {
* type: "kanban",
* columns: ["name", "amount"],
* kanban: {
* groupByField: "stage",
* summarizeField: "amount",
* columns: ["name", "close_date"]
* }
* }
*/
export const ListViewSchema = lazySchema(() => z.object({
name: SnakeCaseIdentifierSchema.optional().describe('Internal view name (lowercase snake_case)'),
label: I18nLabelSchema.optional(), // Display label override (supports i18n)
type: z.enum([
'grid', // Standard Data Table
'kanban', // Board / Columns
'gallery', // Card Deck / Masonry
'calendar', // Monthly/Weekly/Daily
'timeline', // Chronological Stream (Feed)
'gantt', // Project Timeline
'map', // Geospatial
'chart' // Aggregate visualisation
]).default('grid'),
/** Data Source Configuration */
data: ViewDataSchema.optional().describe('Data source configuration (defaults to "object" provider)'),
/** Shared Query Config */
columns: z.union([
z.array(z.string()), // Legacy: simple field names
z.array(ListColumnSchema), // Enhanced: detailed column config
]).describe('Fields to display as columns'),
filter: z.array(ViewFilterRuleSchema).optional().describe('Filter criteria (JSON Rules)'),
sort: z.union([
z.string(), //Legacy "field desc"
z.array(z.object({
field: z.string(),
order: z.enum(['asc', 'desc'])
}))
]).optional(),
/** Search & Filter */
searchableFields: z.array(z.string()).optional().describe('Fields enabled for search'),
filterableFields: z.array(z.string()).optional().describe('Legacy shorthand for userFilters.fields — bare field names enabled for end-user filtering. Prefer userFilters'),
/** User Filters (ADR-0047, Airtable Interface parity) */
userFilters: UserFiltersSchema.optional()
.describe('End-user quick-filter bar: dropdown/toggle fields or tab presets. Omit to let the renderer derive filters from select/boolean fields'),
/** Grid Features */
resizable: z.boolean().optional().describe('Enable column resizing'),
striped: z.boolean().optional().describe('Striped row styling'),
bordered: z.boolean().optional().describe('Show borders'),
compactToolbar: z.boolean().optional().describe('Collapse Group/Color/Density/Hide-fields into a single View settings popover'),
/** Selection */
selection: SelectionConfigSchema.optional().describe('Row selection configuration'),
/** Navigation / Interaction */
navigation: NavigationConfigSchema.optional().describe('Configuration for item click navigation (page, drawer, modal, etc.)'),
/** Pagination */
pagination: PaginationConfigSchema.optional().describe('Pagination configuration'),
/** Type Specific Config */
kanban: KanbanConfigSchema.optional(),
calendar: CalendarConfigSchema.optional(),
gantt: GanttConfigSchema.optional(),
gallery: GalleryConfigSchema.optional(),
timeline: TimelineConfigSchema.optional(),
chart: ListChartConfigSchema.optional(),
/** View Metadata (Airtable-style view management) */
description: I18nLabelSchema.optional().describe('View description for documentation/tooltips'),
sharing: ViewSharingSchema.optional().describe('View sharing and access configuration'),
/** Row Height / Density (Airtable-style) */
rowHeight: RowHeightSchema.optional().describe('Row height / density setting'),
/** Record Grouping (Airtable-style) */
grouping: GroupingConfigSchema.optional().describe('Group records by one or more fields'),
/** Row Color (Airtable-style) */
rowColor: RowColorConfigSchema.optional().describe('Color rows based on field value'),
/** Field Visibility & Ordering per View (Airtable-style) */
hiddenFields: z.array(z.string()).optional().describe('Fields to hide in this specific view'),
fieldOrder: z.array(z.string()).optional().describe('Explicit field display order for this view'),
/** Row & Bulk Actions */
rowActions: z.array(z.string()).optional().describe('Actions available for individual row items'),
bulkActions: z.array(z.string()).optional().describe('Actions available when multiple rows are selected'),
bulkActionDefs: z.array(z.record(z.string(), z.any())).optional().describe('Rich bulk action definitions (schema-driven, executed via BulkActionDialog)'),
/** Performance */
virtualScroll: z.boolean().optional().describe('Enable virtual scrolling for large datasets'),
/** Conditional Formatting */
conditionalFormatting: z.array(z.object({
condition: ExpressionInputSchema.describe('Predicate (CEL) to evaluate.'),
style: z.record(z.string(), z.string()).describe('CSS styles to apply when condition is true'),
})).optional().describe('Conditional formatting rules for list rows'),
/** Inline Edit */
inlineEdit: z.boolean().optional().describe('Allow inline editing of records directly in the list view'),
/** Export */
exportOptions: z.array(z.enum(['csv', 'xlsx', 'pdf', 'json'])).optional().describe('Available export format options'),
/** User Actions (Airtable Interface parity) */
userActions: UserActionsConfigSchema.optional().describe('User action toggles for the view toolbar'),
/** Appearance (Airtable Interface parity) */
appearance: AppearanceConfigSchema.optional().describe('Appearance and visualization configuration'),
/** Tabs (Airtable Interface parity) */
tabs: z.array(ViewTabSchema).optional().describe('Tab definitions for multi-tab view interface'),
/** Add Record (Airtable Interface parity) */
addRecord: AddRecordConfigSchema.optional().describe('Add record entry point configuration'),
/** Record Count Display (Airtable Interface parity) */
showRecordCount: z.boolean().optional().describe('Show record count at the bottom of the list'),
/** Advanced: Allow Printing (Airtable Interface parity) */
allowPrinting: z.boolean().optional().describe('Allow users to print the view'),
/** Empty State */
emptyState: z.object({
title: I18nLabelSchema.optional(),
message: I18nLabelSchema.optional(),
icon: z.string().optional(),
}).optional().describe('Empty state configuration when no records found'),
/** ARIA accessibility attributes */
aria: AriaPropsSchema.optional().describe('ARIA accessibility attributes for the list view'),
/** Responsive layout overrides per breakpoint */
responsive: ResponsiveConfigSchema.optional().describe('Responsive layout configuration'),
/** Performance optimization settings */
performance: PerformanceConfigSchema.optional().describe('Performance optimization settings'),
}));
/**
* Form Field Configuration Schema
* Detailed configuration for individual form fields.
*
* Reuses Data.FieldType and related constraints from the Data protocol to avoid duplication.
* The `type` field auto-infers widget rendering; explicit `widget` overrides are only needed
* for custom components.
*
* @example Auto-inferred select widget
* { field: 'status', type: 'select', options: [{ label: 'Open', value: 'open' }] }
*
* @example Lookup field with reference
* { field: 'account_id', type: 'lookup', reference: 'account', label: 'Account' }
*
* @example Custom widget override
* { field: 'filter', widget: 'filter-builder' }
*/
export const FormFieldSchema: z.ZodType<any> = lazySchema(() => z.object({
/** Field name (snake_case) */
field: z.string().describe('Field name (snake_case)'),
/** Field type — reuses Data.FieldType. When set, widget is auto-inferred (can be overridden). */
type: FieldType.optional().describe('Field type (auto-infers widget if omitted)'),
/** Select/multiselect options — only needed when type=select/multiselect/radio/checkboxes */
options: z.array(SelectOptionSchema).optional().describe('Options for select/multiselect/radio/checkboxes fields'),
/** Reference object for lookup/master_detail fields */
reference: z.string().optional().describe('Target object name for lookup/master_detail fields'),
/** Text constraints */
maxLength: z.number().optional().describe('Maximum character length (for text/textarea/email/url/phone)'),
minLength: z.number().optional().describe('Minimum character length'),
/** Number constraints */
min: z.number().optional().describe('Minimum value (for number/currency/percent/slider)'),
max: z.number().optional().describe('Maximum value'),
precision: z.number().optional().describe('Total digits (for number/currency)'),
scale: z.number().optional().describe('Decimal places'),
/** Multi-value flag */
multiple: z.boolean().optional().describe('Allow multiple values (for select/lookup/file/image)'),
/** UI overrides */
label: I18nLabelSchema.optional().describe('Display label override'),
placeholder: I18nLabelSchema.optional().describe('Placeholder text'),
helpText: I18nLabelSchema.optional().describe('Help/hint text'),
readonly: z.boolean().optional().describe('Read-only override'),
immutable: z.boolean().optional().describe('Editable on create, locked once the record exists (e.g. machine names).'),
required: z.boolean().optional().describe('Required override'),
hidden: z.boolean().optional().describe('Hidden override'),
colSpan: z.number().int().min(1).max(4).optional().describe('Column span in grid layout (1-4)'),
/** Custom widget override — only needed when auto-inference is insufficient */
widget: z.string().optional().describe('Custom widget/component name (overrides type-based inference)'),
/** For `code` fields: source language (e.g. 'javascript', 'sql', 'json', 'typescript', 'expression', 'cel'). Drives syntax highlighting. */
language: z.string().optional().describe('Code editor language (for type=code)'),
/**
* Sub-fields for `composite` / `repeater` / `record` types — declares
* the inner shape of an embedded sub-object (composite), each row of
* an embedded sub-object array (repeater), or each entry of a name-keyed
* map (record). Recursive: any of the three can nest.
*
* Use `lookup` / `master_detail` instead when the children are independent
* records with their own IDs in a separate object/table.
*/
fields: z.array(z.lazy(() => FormFieldSchema)).optional()
.describe('Sub-fields for composite/repeater/record types'),
/**
* For `record`-typed fields only. Declares how the map key is sourced,
* displayed, and validated when an admin creates a new entry.
*
* The same identifier is also stored as `name` on the inner value (so
* `record.fields[k].name === k`). Most callers can omit this and accept
* the defaults: `{ field: 'name', label: 'Name', regex: /^[a-z_][a-z0-9_]*$/, immutable: true }`.
*
* See ADR-0007 (record form field type).
*/
keyField: z.object({
field: z.string().default('name').describe('Property name that holds the key inside each item (defaults to "name")'),
label: I18nLabelSchema.optional().describe('Display label for the key column'),
placeholder: I18nLabelSchema.optional().describe('Placeholder when entering a new key'),
helpText: I18nLabelSchema.optional().describe('Help text under the key input'),
/** Validation pattern serialised as a regex source string (no flags). */
regex: z.string().optional().describe('JS regex source string the key must match (no flags)'),
/** Renamable after creation? Defaults to false — keys are usually identifiers. */
immutable: z.boolean().default(true).describe('If true, the key is read-only after creation'),
}).optional().describe('Key column config for record-typed fields'),
dependsOn: z.string().optional().describe('Parent field name for cascading'),
visibleOn: ExpressionInputSchema.optional().describe('Visibility predicate (CEL).'),
}));
/**
* Form Layout Section
*/
export const FormSectionSchema = lazySchema(() => z.object({
/**
* Stable identifier for translation lookup. snake_case convention.
* When provided, translation bundles can target this section's `label`
* and `description` via `metadataForms.<type>.sections.<name>`.
* Optional for backward-compat with sections that only have a `label`.
*/
name: z.string().optional().describe('Stable section identifier for i18n lookup (snake_case)'),
label: I18nLabelSchema.optional(),
description: z.string().optional().describe('Optional description rendered under the section header.'),
collapsible: z.boolean().default(false),
collapsed: z.boolean().default(false),
visibleOn: ExpressionInputSchema.optional().describe('Visibility predicate (CEL). Hides the whole section when false.'),
columns: z.union([
z.enum(['1', '2', '3', '4']),
z.literal(1),
z.literal(2),
z.literal(3),
z.literal(4),
]).default(1).transform(val => (typeof val === 'string' ? parseInt(val) : val) as 1 | 2 | 3 | 4),
fields: z.array(z.union([
z.string(), // Legacy: simple field name
FormFieldSchema, // Enhanced: detailed field config
])),
}));
/**
* Form View Schema
* Defines the layout for creating or editing a single record.
*
* @example Simple Sectioned Form
* {
* type: "simple",
* sections: [
* {
* label: "General Info",
* columns: 2,
* fields: ["name", "status"]
* },
* {
* label: "Details",
* fields: ["description", { field: "priority", widget: "rating" }]
* }
* ]
* }
*/
export const FormViewSchema = lazySchema(() => z.object({
type: z.enum([
'simple', // Single column or sections
'tabbed', // Tabs
'wizard', // Step by step
'split', // Master-Detail split
'drawer', // Side panel
'modal' // Dialog
]).default('simple'),
/** Data Source Configuration */
data: ViewDataSchema.optional().describe('Data source configuration (defaults to "object" provider)'),
sections: z.array(FormSectionSchema).optional(), // For simple layout
groups: z.array(FormSectionSchema).optional(), // Legacy support -> alias to sections
/**
* Inline child collections (master-detail). When present, the standard
* create/edit form for this object renders as a master-detail form — the
* object's own fields on top, an editable grid per child collection below,
* persisted together in ONE atomic transaction — with no bespoke page. Each
* entry needs only `childObject`; the relationship FK and grid columns are
* derived from the child object's metadata (override via
* `relationshipField` / `columns`).
*/
subforms: z.array(z.object({
childObject: z.string().describe('Child object whose records are entered inline'),
relationshipField: z.string().optional().describe('FK on the child pointing back to the parent (auto-detected when omitted)'),
columns: z.array(z.any()).optional().describe('Editable grid columns (derived from the child object when omitted)'),
amountField: z.string().optional().describe('Numeric child column summed for the running total'),
totalField: z.string().optional().describe('Parent field to receive the rolled-up sum'),
title: z.string().optional().describe('Section title'),
addLabel: z.string().optional().describe('Add-row button label'),
minRows: z.number().optional(),
maxRows: z.number().optional(),
})).optional().describe('Inline master-detail child collections'),
/** Default Sort for Related Lists (e.g., sort child records by date) */
defaultSort: z.array(z.object({
field: z.string().describe('Field name to sort by'),
order: z.enum(['asc', 'desc']).default('desc').describe('Sort direction'),
})).optional().describe('Default sort order for related list views within this form'),
/** Public form sharing configuration */
sharing: SharingConfigSchema.optional().describe('Public sharing configuration for this form'),
/**
* What happens after a successful submit.
*
* - `thank-you` (default) — show a confirmation panel
* - `redirect` — send the browser to a URL
* - `continue` — reset the form so another response can be entered
* - `next-record` — advance to the next record (internal queues only)
*/
submitBehavior: z.union([
z.object({
kind: z.literal('thank-you'),
title: z.string().optional(),
message: z.string().optional(),
}),
z.object({
kind: z.literal('redirect'),
url: z.string(),
delayMs: z.number().int().min(0).optional(),
}),
z.object({ kind: z.literal('continue') }),
z.object({ kind: z.literal('next-record') }),
]).optional().describe('Post-submit behavior'),
/** ARIA accessibility attributes */
aria: AriaPropsSchema.optional().describe('ARIA accessibility attributes for the form view'),
}));
/**
* Master View Schema
* Can define multiple named views.
*/
/**
* View Container Schema
* Aggregates all view definitions for a specific object or context.
*
* @example
* {
* list: { type: "grid", columns: ["name"] },
* form: { type: "simple", fields: ["name"] },
* listViews: {
* "all": { label: "All", filter: [] },
* "my": { label: "Mine", filter: [["owner", "=", "{user_id}"]] }
* }
* }
*/
export const ViewSchema = lazySchema(() => z.object({
list: ListViewSchema.optional(), // Default list view
form: FormViewSchema.optional(), // Default form view
listViews: z.record(z.string(), ListViewSchema).optional().describe('Additional named list views'),
formViews: z.record(z.string(), FormViewSchema).optional().describe('Additional named form views'),
/**
* ADR-0010 §3.7 — Package-level protection envelope. Package
* authors declare lock policy here; the loader translates it
* into the private `_lock` envelope at registration time and
* strips this block before persistence. See
* `shared/protection.zod.ts`.
*/
protection: ProtectionSchema.optional().describe(
'Package author protection block — lock policy for this view.',
),
// ADR-0010 — runtime protection envelope (internal — set by loader).
...MetadataProtectionFields,
}));
/**
* Type-safe factory for creating view definitions.
*
* Validates the config at creation time using Zod `.parse()`.
*
* @example
* ```ts
* const taskViews = defineView({
* list: {
* type: 'grid',
* data: { provider: 'object', object: 'task' },
* columns: ['subject', 'status', 'priority', 'due_date'],
* },
* form: {
* type: 'simple',
* sections: [{ label: 'Details', fields: [{ field: 'subject' }] }],
* },
* });
* ```
*/
export function defineView(config: z.input<typeof ViewSchema>): View {
return ViewSchema.parse(config);
}
// ───────────────────────────────────────────────────────────────────────────
// Independent View Item model (Object has-many View)
// ───────────────────────────────────────────────────────────────────────────
//
// The {@link ViewSchema} *container* above aggregates every view of an object
// into one document. That model cannot express runtime-authored views (a user
// adding "My high-value leads" at runtime cannot append to a developer's source
// file), and it forces multiple distinct views into a single designer.
//
// {@link ViewItemSchema} promotes each named view to a first-class, independently
// addressable entity bound to its object by a foreign key. The object↔view
// switcher is then a *query* (`getViewsByObject`) rather than embedded storage —
// mirroring Airtable / Salesforce / Notion, where a table/object has-many views.
//
// `defineView` is retained as authoring sugar: the backend loader expands an
// aggregated document into N ViewItems at registration time, so existing
// `*.view.ts` files and the published spec keep working unchanged.
/**
* Qualified view-item identity: `<object>.<viewKey>` — dotted snake_case
* segments, e.g. `crm_lead.pipeline`. Globally unique, and the object can be
* recovered from the prefix, so the registry key never collides across objects.
*/
export const ViewItemNameSchema = z
.string()
.regex(
/^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$/,
'View item name must be a dotted snake_case qualified name, e.g. "crm_lead.pipeline".',
)
.describe('Globally-unique view id, `<object>.<viewKey>`.');
/**
* Identity layer for a view item — its visibility scope and ownership.
*
* - `package` — shipped from `*.view.ts` source / an installed package.
* Not deletable (reinstall restores it); customisable via an
* override layer; hideable from the switcher.
* - `shared` — authored at runtime, visible org-wide. Creating one is gated
* by the `view.manageShared` capability.
* - `personal` — authored at runtime, scoped to `owner`. Any user with read
* access to the object may create one.
*
* Named `scope` (not `provenance`) to avoid colliding with the loader-set
* `_provenance` envelope field, which tracks a different axis
* (package | org | env-forced).
*/
export const ViewScopeSchema = z
.enum(['package', 'shared', 'personal'])
.describe('View identity layer: package | shared | personal.');
/** Discriminator for the kind of view a {@link ViewItemSchema} carries. */
export const ViewKindSchema = z
.enum(['list', 'form'])
.describe('Whether `config` is a ListView (list family) or a FormView.');
/**
* Fields shared by every independent view item, regardless of kind. Returned
* as a raw Zod shape so it composes into each discriminated-union member
* without forcing eager schema construction.
*/
function viewItemBaseShape() {
return {
name: ViewItemNameSchema,
object: z
.string()
.describe('Bound object name — the foreign key used to aggregate views.'),
label: I18nLabelSchema.optional().describe('Display label (supports i18n).'),
isDefault: z
.boolean()
.optional()
.describe("Whether this is the object's default view in the switcher."),
order: z
.number()
.int()
.optional()
.describe("Sort order within the object's view switcher / left rail."),
scope: ViewScopeSchema.optional().describe(
'Identity layer (defaults to `package` for source-loaded views).',
),
owner: z
.string()
.optional()
.describe('Owner user id — set when `scope` is `personal`.'),
hidden: z
.boolean()
.optional()
.describe('Hidden from the switcher (per-user / per-org declutter).'),
/**
* Package author protection block — same envelope as {@link ViewSchema};
* the loader translates it into the private `_lock` envelope.
*/
protection: ProtectionSchema.optional().describe(
'Package author protection block — lock policy for this view.',
),
// ADR-0010 — runtime protection envelope (internal — set by loader).
...MetadataProtectionFields,