@postxl/schema 2.3.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -150,7 +150,7 @@ export declare const fieldTransformer: (fieldInput: FieldEnriched, model: {
150
150
  name: Model.ModelName;
151
151
  isReadonly?: boolean;
152
152
  }>;
153
- }, ctx: z.RefinementCtx) => FieldId | FieldScalar | FieldJson | FieldRelation | FieldEnum;
153
+ }, ctx: z.RefinementCtx) => FieldRelation | FieldId | FieldScalar | FieldJson | FieldEnum;
154
154
  export type FieldDatabaseNative = FieldId | FieldScalar | FieldJson | FieldRelation | FieldEnum;
155
155
  export type Field = FieldDatabaseNative;
156
156
  export type Fields<FIELD = Field> = Map<Branded.FieldName, FIELD>;
@@ -63,6 +63,10 @@ export declare const zModelJSON: z.ZodPreprocess<z.ZodObject<{
63
63
  standardFields: z.ZodOptional<z.ZodArray<z.ZodString>>;
64
64
  defaultField: z.ZodOptional<z.ZodString>;
65
65
  labelField: z.ZodOptional<z.ZodString>;
66
+ labelQualifier: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
67
+ relation: z.ZodString;
68
+ separator: z.ZodOptional<z.ZodString>;
69
+ }, z.core.$strip>]>>;
66
70
  keyField: z.ZodOptional<z.ZodString>;
67
71
  defaultSort: z.ZodOptional<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>>;
68
72
  indexes: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodPipe<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>, z.ZodTransform<{
@@ -121,6 +121,27 @@ Example use case: default language, root region in region tree.`),
121
121
  .string()
122
122
  .optional().describe(`The name of the field that is used in the UI as the label of the entity.
123
123
  The label field must be a string field. If not provided, the \`name\` field is used as the label.`),
124
+ labelQualifier: zod_1.z
125
+ .union([
126
+ zod_1.z.string(),
127
+ zod_1.z.object({
128
+ relation: zod_1.z.string(),
129
+ separator: zod_1.z.string().optional(),
130
+ }),
131
+ ])
132
+ .optional().describe(`Qualifies this model's label with the label of a related parent entity.
133
+
134
+ Use this when a label is only unique *within* a parent — e.g. most projects have a session
135
+ named "main", so a session picker would otherwise show a list of identical entries.
136
+
137
+ Shorthand form names the relation field: "labelQualifier": "projectId"
138
+ Object form additionally sets a separator: { "relation": "projectId", "separator": ": " }
139
+
140
+ The value is the name of a relation field on this model. It must not be a self-relation, and
141
+ the chain of qualifiers across models must not be circular. The parent's own qualified label
142
+ is used, so qualifiers chain transitively.
143
+
144
+ When omitted, the label is just \`labelField\` and nothing about the model's output changes.`),
124
145
  keyField: zod_1.z
125
146
  //
126
147
  .string()
@@ -11,6 +11,10 @@ export declare function modelJSONTransformer(input: ModelJSON, ctx: z.Refinement
11
11
  databaseName: string & z.core.$brand<"PXL.DatabaseModelName">;
12
12
  defaultFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
13
13
  labelFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
14
+ labelQualifier: {
15
+ relationFieldName: string & z.core.$brand<"PXL.FieldName">;
16
+ separator: string | undefined;
17
+ } | undefined;
14
18
  keyFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
15
19
  defaultSort: (string & z.core.$brand<"PXL.FieldName">)[] | undefined;
16
20
  isReadonly: boolean;
@@ -78,6 +82,10 @@ declare function addDefaults({ description, fields, standardFields, seed, faker,
78
82
  map: string | undefined;
79
83
  })[];
80
84
  seed: any[];
85
+ labelQualifier: {
86
+ relation: string;
87
+ separator: string | undefined;
88
+ } | undefined;
81
89
  faker: {
82
90
  seed: number;
83
91
  items: number;
@@ -114,13 +122,17 @@ declare function addDefaults({ description, fields, standardFields, seed, faker,
114
122
  source?: Record<string, unknown> | undefined;
115
123
  };
116
124
  type WithDefaults = ReturnType<typeof addDefaults>;
117
- declare function brandModel({ name, schema, databaseName, standardFields, defaultField, labelField, keyField, defaultSort, ...input }: WithDefaults): {
125
+ declare function brandModel({ name, schema, databaseName, standardFields, defaultField, labelField, labelQualifier, keyField, defaultSort, ...input }: WithDefaults): {
118
126
  name: string & z.core.$brand<"PXL.ModelName">;
119
127
  schema: (string & z.core.$brand<"PXL.DatabaseSchemaName">) | undefined;
120
128
  databaseName: string & z.core.$brand<"PXL.DatabaseModelName">;
121
129
  standardFields: (string & z.core.$brand<"PXL.FieldName">)[];
122
130
  defaultFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
123
131
  labelFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
132
+ labelQualifier: {
133
+ relationFieldName: string & z.core.$brand<"PXL.FieldName">;
134
+ separator: string | undefined;
135
+ } | undefined;
124
136
  keyFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
125
137
  defaultSort: (string & z.core.$brand<"PXL.FieldName">)[] | undefined;
126
138
  isReadonly: boolean;
@@ -182,6 +194,10 @@ declare function combineFields({ fields, standardFields, ...model }: BrandedMode
182
194
  databaseName: string & z.core.$brand<"PXL.DatabaseModelName">;
183
195
  defaultFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
184
196
  labelFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
197
+ labelQualifier: {
198
+ relationFieldName: string & z.core.$brand<"PXL.FieldName">;
199
+ separator: string | undefined;
200
+ } | undefined;
185
201
  keyFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
186
202
  defaultSort: (string & z.core.$brand<"PXL.FieldName">)[] | undefined;
187
203
  isReadonly: boolean;
@@ -47,6 +47,14 @@ function modelJSONTransformer(input, ctx) {
47
47
  validateSeedData(withCombinedFields, ctx);
48
48
  return withCombinedFields;
49
49
  }
50
+ function normalizeLabelQualifier(input) {
51
+ if (input === undefined) {
52
+ return undefined;
53
+ }
54
+ return typeof input === 'string'
55
+ ? { relation: input, separator: undefined }
56
+ : { relation: input.relation, separator: input.separator };
57
+ }
50
58
  function addDefaults({ description, fields, standardFields, seed, faker, ...input }) {
51
59
  return {
52
60
  ...input,
@@ -59,6 +67,7 @@ function addDefaults({ description, fields, standardFields, seed, faker, ...inpu
59
67
  standardFields: standardFields ?? Field.standardFieldNames,
60
68
  indexes: input.indexes ?? [],
61
69
  seed: seed ?? [],
70
+ labelQualifier: normalizeLabelQualifier(input.labelQualifier),
62
71
  faker: {
63
72
  seed: faker?.seed ?? 0,
64
73
  // We use the provided number of faker items - if nothing is provided, we default to 10 - unless a seed is provided, then we default to 0
@@ -66,7 +75,7 @@ function addDefaults({ description, fields, standardFields, seed, faker, ...inpu
66
75
  },
67
76
  };
68
77
  }
69
- function brandModel({ name, schema, databaseName, standardFields, defaultField, labelField, keyField, defaultSort, ...input }) {
78
+ function brandModel({ name, schema, databaseName, standardFields, defaultField, labelField, labelQualifier, keyField, defaultSort, ...input }) {
70
79
  return {
71
80
  ...input,
72
81
  name: (0, model_brands_1.toModelName)(name),
@@ -75,6 +84,9 @@ function brandModel({ name, schema, databaseName, standardFields, defaultField,
75
84
  standardFields: standardFields.map((f) => Field.toFieldName(f)),
76
85
  defaultFieldName: defaultField ? Field.toFieldName(defaultField) : undefined,
77
86
  labelFieldName: labelField ? Field.toFieldName(labelField) : undefined,
87
+ labelQualifier: labelQualifier
88
+ ? { relationFieldName: Field.toFieldName(labelQualifier.relation), separator: labelQualifier.separator }
89
+ : undefined,
78
90
  keyFieldName: keyField ? Field.toFieldName(keyField) : undefined,
79
91
  defaultSort: defaultSort?.map((fieldName) => Field.toFieldName(fieldName)),
80
92
  };
@@ -3,6 +3,17 @@ import { AuthRuleSet } from '../project-schema/project-schema.auth';
3
3
  import { DatabaseSchemaName } from '../project-schema/project-schema.brands';
4
4
  import * as Branded from './model.brands';
5
5
  export type Models = Map<Branded.ModelName, Model>;
6
+ /**
7
+ * Qualifies a model's label with the label of a related parent entity, so that labels which are
8
+ * only unique within a parent stay distinguishable when the entity is referenced from elsewhere.
9
+ */
10
+ export type LabelQualifier = {
11
+ /** The to-one relation field on this model whose parent supplies the qualifying label. */
12
+ relationField: Field.FieldRelation;
13
+ referencedModelName: Branded.ModelName;
14
+ /** Fully resolved at schema time — consumers never re-resolve it against project defaults. */
15
+ separator: string;
16
+ };
6
17
  export type Model = {
7
18
  name: Branded.ModelName;
8
19
  description: string;
@@ -18,6 +29,12 @@ export type Model = {
18
29
  idField: Field.FieldId;
19
30
  defaultField: Field.FieldDatabaseNative | undefined;
20
31
  labelField: Field.FieldDatabaseNative;
32
+ /**
33
+ * When set, generated view models expose a `displayLabel` computed field that prefixes this
34
+ * model's label with the referenced parent's label. Undefined for models that do not opt in,
35
+ * which generate exactly the same output as before this feature existed.
36
+ */
37
+ labelQualifier: LabelQualifier | undefined;
21
38
  /**
22
39
  * The field used as the key/unique identifier for the entity.
23
40
  * Used in navigation, Excel import, and other places where a human-readable identifier is needed.
@@ -59,6 +59,7 @@ export declare const zProjectSchemaJSON: z.ZodPreprocess<z.ZodObject<{
59
59
  }, z.core.$strip>>;
60
60
  }, z.core.$strip>]>>>;
61
61
  defaultSchema: z.ZodOptional<z.ZodString>;
62
+ defaultLabelSeparator: z.ZodOptional<z.ZodString>;
62
63
  auth: z.ZodOptional<z.ZodObject<{
63
64
  provider: z.ZodDefault<z.ZodOptional<z.ZodLiteral<"keycloak">>>;
64
65
  roleClaimPath: z.ZodDefault<z.ZodOptional<z.ZodString>>;
@@ -109,6 +110,10 @@ export declare const zProjectSchemaJSON: z.ZodPreprocess<z.ZodObject<{
109
110
  standardFields: z.ZodOptional<z.ZodArray<z.ZodString>>;
110
111
  defaultField: z.ZodOptional<z.ZodString>;
111
112
  labelField: z.ZodOptional<z.ZodString>;
113
+ labelQualifier: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
114
+ relation: z.ZodString;
115
+ separator: z.ZodOptional<z.ZodString>;
116
+ }, z.core.$strip>]>>;
112
117
  keyField: z.ZodOptional<z.ZodString>;
113
118
  defaultSort: z.ZodOptional<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>>;
114
119
  indexes: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodPipe<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>, z.ZodTransform<{
@@ -116,6 +116,11 @@ exports.zProjectSchemaJSON = zod_1.z
116
116
  //
117
117
  .optional()
118
118
  .describe(`The default database schema that is used if model/enum does not specify a schema. If not provided, the default schema "Data" is used.`),
119
+ defaultLabelSeparator: zod_1.z
120
+ //
121
+ .string()
122
+ .optional()
123
+ .describe(`Separator inserted between a parent label and a child label when a model declares a \`labelQualifier\` without its own separator. Defaults to " – " (space, en dash, space).`),
119
124
  auth: project_schema_auth_1.zProjectAuthJSON
120
125
  .optional()
121
126
  .describe('Project-level authentication settings (provider, role claim path, and default behavior).'),
@@ -38,6 +38,7 @@ export declare const zProjectSchema: z.ZodPipe<z.ZodPreprocess<z.ZodObject<{
38
38
  }, z.core.$strip>>;
39
39
  }, z.core.$strip>]>>>;
40
40
  defaultSchema: z.ZodOptional<z.ZodString>;
41
+ defaultLabelSeparator: z.ZodOptional<z.ZodString>;
41
42
  auth: z.ZodOptional<z.ZodObject<{
42
43
  provider: z.ZodDefault<z.ZodOptional<z.ZodLiteral<"keycloak">>>;
43
44
  roleClaimPath: z.ZodDefault<z.ZodOptional<z.ZodString>>;
@@ -88,6 +89,10 @@ export declare const zProjectSchema: z.ZodPipe<z.ZodPreprocess<z.ZodObject<{
88
89
  standardFields: z.ZodOptional<z.ZodArray<z.ZodString>>;
89
90
  defaultField: z.ZodOptional<z.ZodString>;
90
91
  labelField: z.ZodOptional<z.ZodString>;
92
+ labelQualifier: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
93
+ relation: z.ZodString;
94
+ separator: z.ZodOptional<z.ZodString>;
95
+ }, z.core.$strip>]>>;
91
96
  keyField: z.ZodOptional<z.ZodString>;
92
97
  defaultSort: z.ZodOptional<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>>;
93
98
  indexes: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodPipe<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>, z.ZodTransform<{
@@ -162,6 +167,7 @@ export declare const zProjectSchema: z.ZodPipe<z.ZodPreprocess<z.ZodObject<{
162
167
  } | undefined;
163
168
  })[] | undefined;
164
169
  defaultSchema?: string | undefined;
170
+ defaultLabelSeparator?: string | undefined;
165
171
  auth?: {
166
172
  provider: "keycloak";
167
173
  roleClaimPath: string;
@@ -213,6 +219,10 @@ export declare const zProjectSchema: z.ZodPipe<z.ZodPreprocess<z.ZodObject<{
213
219
  standardFields?: string[] | undefined;
214
220
  defaultField?: string | undefined;
215
221
  labelField?: string | undefined;
222
+ labelQualifier?: string | {
223
+ relation: string;
224
+ separator?: string | undefined;
225
+ } | undefined;
216
226
  keyField?: string | undefined;
217
227
  defaultSort?: (string & z.core.$brand<"PXL.FieldName">)[] | undefined;
218
228
  indexes?: ({
@@ -95,6 +95,7 @@ function addDefaults(input) {
95
95
  schemas,
96
96
  schemaAuth,
97
97
  defaultSchema,
98
+ defaultLabelSeparator: input.defaultLabelSeparator,
98
99
  ...(input.auth ? { auth: input.auth } : {}),
99
100
  systemUser: {
100
101
  ...project_schema_defaults_1.defaultSystemUser,
@@ -287,7 +288,11 @@ function transformModels(projectSchema, ctx) {
287
288
  const model = transformModel(inputModel, projectSchema, ctx);
288
289
  models.set(model.name, model);
289
290
  }
290
- const { auth, ...remainingProjectSchema } = projectSchema;
291
+ validateLabelQualifierCycles(models, ctx);
292
+ // `defaultLabelSeparator` is only a fallback consulted while resolving each model's
293
+ // `labelQualifier.separator` above; it is fully resolved by this point and must not leak
294
+ // into the output (consumers never re-resolve it against project defaults).
295
+ const { auth, defaultLabelSeparator: _defaultLabelSeparatorInput, ...remainingProjectSchema } = projectSchema;
291
296
  // Keep optional field shape clean: only include `auth` when it is actually set,
292
297
  // instead of returning `auth: undefined`.
293
298
  return auth
@@ -301,15 +306,47 @@ function transformModels(projectSchema, ctx) {
301
306
  models,
302
307
  };
303
308
  }
309
+ /**
310
+ * Rejects circular label-qualifier chains (e.g. Session qualified by Project qualified by
311
+ * Session), which would otherwise recurse forever when the view services resolve display
312
+ * labels. Each distinct cycle is reported once, however many models enter it.
313
+ */
314
+ function validateLabelQualifierCycles(models, ctx) {
315
+ const reported = new Set();
316
+ for (const startName of models.keys()) {
317
+ const path = [];
318
+ let current = startName;
319
+ while (current !== undefined && !path.includes(current)) {
320
+ path.push(current);
321
+ current = models.get(current)?.labelQualifier?.referencedModelName;
322
+ }
323
+ if (current === undefined) {
324
+ continue;
325
+ }
326
+ const cycle = path.slice(path.indexOf(current));
327
+ // Sorted only to canonicalise the key, so the same cycle reported from a different starting
328
+ // model dedupes against itself. An explicit comparator keeps the order locale-independent.
329
+ const key = [...cycle].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)).join('|');
330
+ if (reported.has(key)) {
331
+ continue;
332
+ }
333
+ reported.add(key);
334
+ ctx.addIssue({
335
+ code: zod_1.z.ZodIssueCode.custom,
336
+ message: `Label qualifier cycle detected: ${[...cycle, current].join(' → ')}. A model's label qualifier chain must not be circular.`,
337
+ });
338
+ }
339
+ }
304
340
  function transformModel(model, projectSchema, ctx) {
305
341
  const fields = transformFields(model, projectSchema, ctx);
306
342
  validateFields(fields, ctx);
307
343
  const idField = extractIdField(fields, model, ctx);
308
344
  const defaultField = extractDefaultField(model, fields, ctx);
309
345
  const labelField = extractLabelField(model, fields, ctx);
346
+ const labelQualifier = extractLabelQualifier(model, fields, projectSchema.defaultLabelSeparator ?? ' – ', ctx);
310
347
  const keyField = extractKeyField(model, fields, idField, ctx);
311
348
  const defaultSort = extractDefaultSort(model, fields, ctx);
312
- const { defaultFieldName, labelFieldName, keyFieldName, auth, ...remainingModel } = model;
349
+ const { defaultFieldName, labelFieldName, keyFieldName, labelQualifier: _labelQualifierInput, auth, ...remainingModel } = model;
313
350
  return {
314
351
  ...remainingModel,
315
352
  ...(auth ? { auth } : {}),
@@ -317,6 +354,7 @@ function transformModel(model, projectSchema, ctx) {
317
354
  fields,
318
355
  defaultField,
319
356
  labelField,
357
+ labelQualifier,
320
358
  keyField,
321
359
  defaultSort,
322
360
  idField,
@@ -471,6 +509,39 @@ function extractLabelField({ labelFieldName, ...model }, fields, ctx) {
471
509
  }
472
510
  return labelField;
473
511
  }
512
+ function extractLabelQualifier(model, fields, defaultLabelSeparator, ctx) {
513
+ const { labelQualifier } = model;
514
+ if (labelQualifier === undefined) {
515
+ return undefined;
516
+ }
517
+ const field = fields.get(labelQualifier.relationFieldName);
518
+ if (field === undefined) {
519
+ ctx.addIssue({
520
+ code: zod_1.z.ZodIssueCode.custom,
521
+ message: `Label qualifier ${labelQualifier.relationFieldName} is not defined in model ${model.name}!`,
522
+ });
523
+ return zod_1.z.NEVER;
524
+ }
525
+ if (field.kind !== 'relation') {
526
+ ctx.addIssue({
527
+ code: zod_1.z.ZodIssueCode.custom,
528
+ message: `Label qualifier ${labelQualifier.relationFieldName} in model ${model.name} must be a relation field, but is of type \`${Field.getFieldType(field)}\`.`,
529
+ });
530
+ return zod_1.z.NEVER;
531
+ }
532
+ if (field.referencedModelName === model.name) {
533
+ ctx.addIssue({
534
+ code: zod_1.z.ZodIssueCode.custom,
535
+ message: `Label qualifier ${labelQualifier.relationFieldName} in model ${model.name} is a self-relation. Hierarchical labels are not supported.`,
536
+ });
537
+ return zod_1.z.NEVER;
538
+ }
539
+ return {
540
+ relationField: field,
541
+ referencedModelName: field.referencedModelName,
542
+ separator: labelQualifier.separator ?? defaultLabelSeparator,
543
+ };
544
+ }
474
545
  function extractLabelFieldFromNameOrIdField({ name }, fields, ctx) {
475
546
  // If no label field is provided, try name field first, then label field, then id field
476
547
  const fallbackField = fields.get(Field.standardFieldName_name) ??