@postxl/schema 2.2.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.
@@ -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) ??