@postxl/schema 1.10.0 → 1.11.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.
package/dist/index.d.ts CHANGED
@@ -12,3 +12,4 @@ export type * from './model/model.brands';
12
12
  export * from './model/model.defaults';
13
13
  export type * from './model/model.types';
14
14
  export * from './samples/sample-schemas';
15
+ export { mergeSplitSchemaEntries } from './merge-split-schema-entries';
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.defaultProjectRoles = exports.zProjectSchema = void 0;
17
+ exports.mergeSplitSchemaEntries = exports.defaultProjectRoles = exports.zProjectSchema = void 0;
18
18
  var project_schema_transformer_1 = require("./project-schema/project-schema.transformer");
19
19
  Object.defineProperty(exports, "zProjectSchema", { enumerable: true, get: function () { return project_schema_transformer_1.zProjectSchema; } });
20
20
  __exportStar(require("./project-schema/project-schema.auth"), exports);
@@ -26,3 +26,5 @@ __exportStar(require("./enum/enum.defaults"), exports);
26
26
  __exportStar(require("./field"), exports);
27
27
  __exportStar(require("./model/model.defaults"), exports);
28
28
  __exportStar(require("./samples/sample-schemas"), exports);
29
+ var merge_split_schema_entries_1 = require("./merge-split-schema-entries");
30
+ Object.defineProperty(exports, "mergeSplitSchemaEntries", { enumerable: true, get: function () { return merge_split_schema_entries_1.mergeSplitSchemaEntries; } });
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Merges inline model/enum collections (from `postxl-schema.json`) with
3
+ * collections loaded from separate per-entry files (e.g. `schema/*.model.json`).
4
+ *
5
+ * Pure function — no filesystem access. The I/O side lives in `@postxl/cli`.
6
+ *
7
+ * The inline collection can be in any of the shapes accepted by the JSON
8
+ * decoder today: `undefined`, an array of named objects, or a record keyed
9
+ * by name. The `fromFiles` map is always `Record<name, definition>` as
10
+ * produced by the CLI file loader.
11
+ *
12
+ * Output is a record keyed by name with keys sorted alphabetically. This
13
+ * keeps the merged order deterministic regardless of whether an entry
14
+ * originated inline or in a file.
15
+ */
16
+ export declare function mergeSplitSchemaEntries(params: {
17
+ inline: unknown;
18
+ fromFiles: Record<string, unknown>;
19
+ kind: 'model' | 'enum';
20
+ fromFilesPaths?: Record<string, string>;
21
+ }): unknown;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mergeSplitSchemaEntries = mergeSplitSchemaEntries;
4
+ /**
5
+ * Merges inline model/enum collections (from `postxl-schema.json`) with
6
+ * collections loaded from separate per-entry files (e.g. `schema/*.model.json`).
7
+ *
8
+ * Pure function — no filesystem access. The I/O side lives in `@postxl/cli`.
9
+ *
10
+ * The inline collection can be in any of the shapes accepted by the JSON
11
+ * decoder today: `undefined`, an array of named objects, or a record keyed
12
+ * by name. The `fromFiles` map is always `Record<name, definition>` as
13
+ * produced by the CLI file loader.
14
+ *
15
+ * Output is a record keyed by name with keys sorted alphabetically. This
16
+ * keeps the merged order deterministic regardless of whether an entry
17
+ * originated inline or in a file.
18
+ */
19
+ function mergeSplitSchemaEntries(params) {
20
+ const { inline, fromFiles, kind, fromFilesPaths } = params;
21
+ const inlineRecord = normalizeInlineToRecord(inline);
22
+ if (inlineRecord === undefined) {
23
+ if (Object.keys(fromFiles).length === 0) {
24
+ return inline;
25
+ }
26
+ return sortByKey(fromFiles);
27
+ }
28
+ for (const name of Object.keys(fromFiles)) {
29
+ if (Object.prototype.hasOwnProperty.call(inlineRecord, name)) {
30
+ const kindCapitalized = kind === 'model' ? 'Model' : 'Enum';
31
+ const defaultBasename = name.charAt(0).toLowerCase() + name.slice(1);
32
+ const defaultFile = `schema/${defaultBasename}.${kind}.json`;
33
+ const filePath = fromFilesPaths?.[name] ?? defaultFile;
34
+ throw new Error(`${kindCapitalized} "${name}" is defined both inline in postxl-schema.json and in ${filePath}. Remove one.`);
35
+ }
36
+ }
37
+ const merged = { ...inlineRecord, ...fromFiles };
38
+ return sortByKey(merged);
39
+ }
40
+ function normalizeInlineToRecord(input) {
41
+ if (input === undefined || input === null) {
42
+ return undefined;
43
+ }
44
+ if (Array.isArray(input)) {
45
+ const record = {};
46
+ for (const entry of input) {
47
+ if (!entry || typeof entry !== 'object') {
48
+ continue;
49
+ }
50
+ const obj = entry;
51
+ const name = obj.name;
52
+ if (typeof name !== 'string' || name.length === 0) {
53
+ continue;
54
+ }
55
+ const { name: _name, ...rest } = obj;
56
+ record[name] = rest;
57
+ }
58
+ return record;
59
+ }
60
+ if (typeof input === 'object') {
61
+ return { ...input };
62
+ }
63
+ return undefined;
64
+ }
65
+ function sortByKey(record) {
66
+ const sorted = {};
67
+ for (const key of Object.keys(record).sort((a, b) => a.localeCompare(b))) {
68
+ sorted[key] = record[key];
69
+ }
70
+ return sorted;
71
+ }
@@ -129,6 +129,8 @@ export declare const zProjectSchemaJSON: z.ZodPipe<z.ZodTransform<unknown, unkno
129
129
  description: z.ZodOptional<z.ZodString>;
130
130
  }, z.core.$strip>, z.ZodString]>>;
131
131
  }, z.core.$strip>>>>;
132
+ modelFiles: z.ZodOptional<z.ZodArray<z.ZodString>>;
133
+ enumFiles: z.ZodOptional<z.ZodArray<z.ZodString>>;
132
134
  source: z.ZodRecord<z.ZodString, z.ZodUnknown>;
133
135
  }, z.core.$strip>>;
134
136
  /**
@@ -156,5 +156,15 @@ If any of the standard enums are also provided in the \`enums\` array, the custo
156
156
  .array(Enum.zEnumJSON)
157
157
  .optional()
158
158
  .describe('The enums that are used in the project.'),
159
+ modelFiles: zod_1.z
160
+ //
161
+ .array(zod_1.z.string().min(1))
162
+ .optional()
163
+ .describe('Optional override: glob patterns (relative to project root) for model files. Default: ["schema/*.model.json"].'),
164
+ enumFiles: zod_1.z
165
+ //
166
+ .array(zod_1.z.string().min(1))
167
+ .optional()
168
+ .describe('Optional override: glob patterns (relative to project root) for enum files. Default: ["schema/*.enum.json"].'),
159
169
  source: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()),
160
170
  }));
@@ -108,6 +108,8 @@ export declare const zProjectSchema: z.ZodPipe<z.ZodPipe<z.ZodTransform<unknown,
108
108
  description: z.ZodOptional<z.ZodString>;
109
109
  }, z.core.$strip>, z.ZodString]>>;
110
110
  }, z.core.$strip>>>>;
111
+ modelFiles: z.ZodOptional<z.ZodArray<z.ZodString>>;
112
+ enumFiles: z.ZodOptional<z.ZodArray<z.ZodString>>;
111
113
  source: z.ZodRecord<z.ZodString, z.ZodUnknown>;
112
114
  }, z.core.$strip>>, z.ZodTransform<ProjectSchema, {
113
115
  name: string;
@@ -214,4 +216,6 @@ export declare const zProjectSchema: z.ZodPipe<z.ZodPipe<z.ZodTransform<unknown,
214
216
  schema?: string | undefined;
215
217
  databaseName?: string | undefined;
216
218
  }[] | undefined;
219
+ modelFiles?: string[] | undefined;
220
+ enumFiles?: string[] | undefined;
217
221
  }>>;