@postxl/schema 1.5.0 → 1.6.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.
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts inline enum definitions from model fields and promotes them to top-level enums.
|
|
3
|
+
*
|
|
4
|
+
* When a field's `type` is an object or array (instead of a string), it is treated as an
|
|
5
|
+
* inline enum definition. The enum members are extracted, a name is generated as
|
|
6
|
+
* `{ModelName}{PascalCase(fieldName)}`, and the field's `type` is replaced with that name.
|
|
7
|
+
*
|
|
8
|
+
* Inline enum format examples:
|
|
9
|
+
*
|
|
10
|
+
* Object format:
|
|
11
|
+
* "type": { "Draft": "description", "Published": "description" }
|
|
12
|
+
*
|
|
13
|
+
* Array format:
|
|
14
|
+
* "type": ["Draft", { "value": "Published", "description": "..." }]
|
|
15
|
+
*
|
|
16
|
+
* The field's `description` is copied to the enum and also kept on the field.
|
|
17
|
+
* The enum's database schema is derived from the parent model's `schema` property.
|
|
18
|
+
*/
|
|
19
|
+
export declare function extractInlineEnumDefinitions(input: Record<string, unknown>): Record<string, unknown>;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractInlineEnumDefinitions = extractInlineEnumDefinitions;
|
|
4
|
+
const utils_1 = require("@postxl/utils");
|
|
5
|
+
/**
|
|
6
|
+
* Extracts inline enum definitions from model fields and promotes them to top-level enums.
|
|
7
|
+
*
|
|
8
|
+
* When a field's `type` is an object or array (instead of a string), it is treated as an
|
|
9
|
+
* inline enum definition. The enum members are extracted, a name is generated as
|
|
10
|
+
* `{ModelName}{PascalCase(fieldName)}`, and the field's `type` is replaced with that name.
|
|
11
|
+
*
|
|
12
|
+
* Inline enum format examples:
|
|
13
|
+
*
|
|
14
|
+
* Object format:
|
|
15
|
+
* "type": { "Draft": "description", "Published": "description" }
|
|
16
|
+
*
|
|
17
|
+
* Array format:
|
|
18
|
+
* "type": ["Draft", { "value": "Published", "description": "..." }]
|
|
19
|
+
*
|
|
20
|
+
* The field's `description` is copied to the enum and also kept on the field.
|
|
21
|
+
* The enum's database schema is derived from the parent model's `schema` property.
|
|
22
|
+
*/
|
|
23
|
+
function extractInlineEnumDefinitions(input) {
|
|
24
|
+
const models = input.models;
|
|
25
|
+
if (!models || typeof models !== 'object') {
|
|
26
|
+
return input;
|
|
27
|
+
}
|
|
28
|
+
const extractedEnums = [];
|
|
29
|
+
const existingEnumNames = getExistingEnumNames(input.enums);
|
|
30
|
+
const generatedEnumNames = new Set();
|
|
31
|
+
let modelsModified = false;
|
|
32
|
+
const createProcessField = (modelSchema) => (modelName, fieldName, fieldDef) => {
|
|
33
|
+
const type = fieldDef.type;
|
|
34
|
+
if (type === undefined || type === null || typeof type === 'string') {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
// type is an object or array — this is an inline enum definition
|
|
38
|
+
const enumName = modelName + (0, utils_1.toPascalCase)(fieldName);
|
|
39
|
+
if (existingEnumNames.has(enumName)) {
|
|
40
|
+
throw new Error(`Inline enum "${enumName}" generated from field "${modelName}.${fieldName}" conflicts with an existing top-level enum of the same name. ` +
|
|
41
|
+
`Rename the field or define the enum in the top-level "enums" section instead.`);
|
|
42
|
+
}
|
|
43
|
+
if (generatedEnumNames.has(enumName)) {
|
|
44
|
+
throw new Error(`Inline enum "${enumName}" generated from field "${modelName}.${fieldName}" conflicts with another inline enum of the same name. ` +
|
|
45
|
+
`Rename one of the fields to avoid the collision.`);
|
|
46
|
+
}
|
|
47
|
+
generatedEnumNames.add(enumName);
|
|
48
|
+
const enumDef = {
|
|
49
|
+
name: enumName,
|
|
50
|
+
members: type,
|
|
51
|
+
};
|
|
52
|
+
// Copy description to the enum (it stays on the field as well)
|
|
53
|
+
if (fieldDef.description !== undefined) {
|
|
54
|
+
enumDef.description = fieldDef.description;
|
|
55
|
+
}
|
|
56
|
+
// Use the parent model's schema for the enum
|
|
57
|
+
if (modelSchema !== undefined) {
|
|
58
|
+
enumDef.schema = modelSchema;
|
|
59
|
+
}
|
|
60
|
+
extractedEnums.push(enumDef);
|
|
61
|
+
// Build the modified field: replace type with enum name, remove schema (not a field property)
|
|
62
|
+
const modifiedField = {};
|
|
63
|
+
for (const [key, value] of Object.entries(fieldDef)) {
|
|
64
|
+
if (key === 'type') {
|
|
65
|
+
modifiedField.type = enumName;
|
|
66
|
+
}
|
|
67
|
+
else if (key === 'schema') {
|
|
68
|
+
// schema is not a field property — skip it
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
modifiedField[key] = value;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return modifiedField;
|
|
76
|
+
};
|
|
77
|
+
let newModels = models;
|
|
78
|
+
if (Array.isArray(models)) {
|
|
79
|
+
const modifiedModels = models.map((model) => {
|
|
80
|
+
if (!model || typeof model !== 'object' || !model.name) {
|
|
81
|
+
return model;
|
|
82
|
+
}
|
|
83
|
+
const modelSchema = model.schema;
|
|
84
|
+
const newFields = processFields(model.name, model.fields, createProcessField(modelSchema));
|
|
85
|
+
if (newFields === undefined) {
|
|
86
|
+
return model;
|
|
87
|
+
}
|
|
88
|
+
modelsModified = true;
|
|
89
|
+
return { ...model, fields: newFields };
|
|
90
|
+
});
|
|
91
|
+
if (modelsModified) {
|
|
92
|
+
newModels = modifiedModels;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
const modifiedModels = {};
|
|
97
|
+
for (const [modelName, modelDef] of Object.entries(models)) {
|
|
98
|
+
if (!modelDef || typeof modelDef !== 'object') {
|
|
99
|
+
modifiedModels[modelName] = modelDef;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const model = modelDef;
|
|
103
|
+
const modelSchema = model.schema;
|
|
104
|
+
const newFields = processFields(modelName, model.fields, createProcessField(modelSchema));
|
|
105
|
+
if (newFields === undefined) {
|
|
106
|
+
modifiedModels[modelName] = modelDef;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
modelsModified = true;
|
|
110
|
+
modifiedModels[modelName] = { ...model, fields: newFields };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (modelsModified) {
|
|
114
|
+
newModels = modifiedModels;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (extractedEnums.length === 0) {
|
|
118
|
+
return input;
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
...input,
|
|
122
|
+
models: newModels,
|
|
123
|
+
enums: mergeEnums(input.enums, extractedEnums),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function processFields(modelName, fields, processField) {
|
|
127
|
+
if (!fields || typeof fields !== 'object') {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
let anyModified = false;
|
|
131
|
+
if (Array.isArray(fields)) {
|
|
132
|
+
const modifiedFields = fields.map((field) => {
|
|
133
|
+
if (!field || typeof field !== 'object' || !field.name) {
|
|
134
|
+
return field;
|
|
135
|
+
}
|
|
136
|
+
const result = processField(modelName, field.name, field);
|
|
137
|
+
if (result === undefined) {
|
|
138
|
+
return field;
|
|
139
|
+
}
|
|
140
|
+
anyModified = true;
|
|
141
|
+
return result;
|
|
142
|
+
});
|
|
143
|
+
return anyModified ? modifiedFields : undefined;
|
|
144
|
+
}
|
|
145
|
+
const modifiedFields = {};
|
|
146
|
+
for (const [fieldName, fieldDef] of Object.entries(fields)) {
|
|
147
|
+
if (!fieldDef || typeof fieldDef !== 'object') {
|
|
148
|
+
modifiedFields[fieldName] = fieldDef;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const result = processField(modelName, fieldName, fieldDef);
|
|
152
|
+
if (result === undefined) {
|
|
153
|
+
modifiedFields[fieldName] = fieldDef;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
anyModified = true;
|
|
157
|
+
modifiedFields[fieldName] = result;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return anyModified ? modifiedFields : undefined;
|
|
161
|
+
}
|
|
162
|
+
function getExistingEnumNames(enums) {
|
|
163
|
+
const names = new Set();
|
|
164
|
+
if (Array.isArray(enums)) {
|
|
165
|
+
for (const e of enums) {
|
|
166
|
+
if (e && typeof e === 'object' && typeof e.name === 'string') {
|
|
167
|
+
names.add(e.name);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else if (enums && typeof enums === 'object') {
|
|
172
|
+
for (const key of Object.keys(enums)) {
|
|
173
|
+
names.add(key);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return names;
|
|
177
|
+
}
|
|
178
|
+
function mergeEnums(existing, extracted) {
|
|
179
|
+
if (existing === undefined || existing === null) {
|
|
180
|
+
return extracted;
|
|
181
|
+
}
|
|
182
|
+
if (Array.isArray(existing)) {
|
|
183
|
+
return [...existing, ...extracted];
|
|
184
|
+
}
|
|
185
|
+
if (typeof existing === 'object') {
|
|
186
|
+
// Existing enums are in object format — convert extracted enums to object format and merge
|
|
187
|
+
const result = { ...existing };
|
|
188
|
+
for (const enumDef of extracted) {
|
|
189
|
+
const { name, ...rest } = enumDef;
|
|
190
|
+
result[name] = rest;
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
return existing;
|
|
195
|
+
}
|
|
@@ -37,6 +37,7 @@ exports.zProjectSchemaJSON = exports.zSchemaName = void 0;
|
|
|
37
37
|
const zod_1 = require("zod");
|
|
38
38
|
const utils_1 = require("@postxl/utils");
|
|
39
39
|
const Enum = __importStar(require("../enum"));
|
|
40
|
+
const extract_inline_enums_1 = require("../extract-inline-enums");
|
|
40
41
|
const Model = __importStar(require("../model"));
|
|
41
42
|
const normalize_named_collection_1 = require("../normalize-named-collection");
|
|
42
43
|
exports.zSchemaName = zod_1.z
|
|
@@ -54,13 +55,18 @@ exports.zProjectSchemaJSON = zod_1.z
|
|
|
54
55
|
.preprocess((input) => {
|
|
55
56
|
if (typeof input === 'object' && input !== null) {
|
|
56
57
|
const original = input;
|
|
58
|
+
// Extract inline enum definitions from model fields before normalization.
|
|
59
|
+
// This converts inline enum types (objects/arrays in field.type) to top-level enums
|
|
60
|
+
// and replaces the field type with the generated enum name.
|
|
61
|
+
const withExtractedEnums = (0, extract_inline_enums_1.extractInlineEnumDefinitions)(original);
|
|
57
62
|
return {
|
|
58
|
-
...
|
|
59
|
-
models: (0, normalize_named_collection_1.normalizeNamedCollection)(
|
|
60
|
-
enums: (0, normalize_named_collection_1.normalizeNamedCollection)(
|
|
63
|
+
...withExtractedEnums,
|
|
64
|
+
models: (0, normalize_named_collection_1.normalizeNamedCollection)(withExtractedEnums.models),
|
|
65
|
+
enums: (0, normalize_named_collection_1.normalizeNamedCollection)(withExtractedEnums.enums),
|
|
61
66
|
// We omit models from the source fields as we keep the original input in model.source
|
|
62
|
-
// as part of the model schema.
|
|
63
|
-
|
|
67
|
+
// as part of the model schema. We use the post-extraction data so that source.enums
|
|
68
|
+
// reflects the extracted inline enums.
|
|
69
|
+
source: (0, utils_1.omit)(withExtractedEnums, 'models'),
|
|
64
70
|
};
|
|
65
71
|
}
|
|
66
72
|
return input;
|