api-core-lib 12.0.73 → 12.0.74
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/cli.cjs +126 -65
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -74,6 +74,132 @@ var import_chalk = __toESM(require("chalk"), 1);
|
|
|
74
74
|
var import_dotenv = __toESM(require("dotenv"), 1);
|
|
75
75
|
var import_swagger_parser = __toESM(require("@apidevtools/swagger-parser"), 1);
|
|
76
76
|
var import_openapi_types = __toESM(require_dist(), 1);
|
|
77
|
+
|
|
78
|
+
// src/generator/core/_propToZod.ts
|
|
79
|
+
function _propToZod(prop) {
|
|
80
|
+
const getMessages = (keys, customErrorMessages) => {
|
|
81
|
+
const messages = {};
|
|
82
|
+
const keyMap = {
|
|
83
|
+
required: "required_error",
|
|
84
|
+
invalid_type: "invalid_type_error"
|
|
85
|
+
};
|
|
86
|
+
for (const key of keys) {
|
|
87
|
+
const zodKey = keyMap[key] || "message";
|
|
88
|
+
const customMessage = customErrorMessages?.[key];
|
|
89
|
+
messages[zodKey] = customMessage || `validation.${key}`;
|
|
90
|
+
}
|
|
91
|
+
return messages;
|
|
92
|
+
};
|
|
93
|
+
let zodChain;
|
|
94
|
+
switch (prop.type) {
|
|
95
|
+
case "string": {
|
|
96
|
+
const messages = getMessages(["required", "invalid_type"], prop.errorMessages);
|
|
97
|
+
zodChain = `z.string(${JSON.stringify(messages)})`;
|
|
98
|
+
if (prop.enum && prop.enum.length > 0) {
|
|
99
|
+
zodChain = `z.enum(${JSON.stringify(prop.enum)}, ${JSON.stringify(messages)})`;
|
|
100
|
+
} else {
|
|
101
|
+
if (prop.minLength !== void 0) {
|
|
102
|
+
const msg = { message: prop.errorMessages?.minLength || "validation.string.min" };
|
|
103
|
+
zodChain += `.min(${prop.minLength}, ${JSON.stringify(msg)})`;
|
|
104
|
+
} else if (prop.isRequired) {
|
|
105
|
+
const msg = { message: prop.errorMessages?.minLength || "validation.string.nonempty" };
|
|
106
|
+
zodChain += `.min(1, ${JSON.stringify(msg)})`;
|
|
107
|
+
}
|
|
108
|
+
if (prop.maxLength !== void 0) {
|
|
109
|
+
const msg = { message: prop.errorMessages?.maxLength || "validation.string.max" };
|
|
110
|
+
zodChain += `.max(${prop.maxLength}, ${JSON.stringify(msg)})`;
|
|
111
|
+
}
|
|
112
|
+
if (prop.pattern) {
|
|
113
|
+
const msg = { message: prop.errorMessages?.pattern || "validation.string.regex" };
|
|
114
|
+
zodChain += `.regex(/${prop.pattern}/, ${JSON.stringify(msg)})`;
|
|
115
|
+
}
|
|
116
|
+
if (prop.format) {
|
|
117
|
+
const msg = { message: prop.errorMessages?.format || `validation.string.${prop.format}` };
|
|
118
|
+
if (prop.format === "email") zodChain += `.email(${JSON.stringify(msg)})`;
|
|
119
|
+
if (prop.format === "url") zodChain += `.url(${JSON.stringify(msg)})`;
|
|
120
|
+
if (prop.format === "uuid") zodChain += `.uuid(${JSON.stringify(msg)})`;
|
|
121
|
+
if (prop.format === "datetime") zodChain += `.datetime(${JSON.stringify(msg)})`;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case "integer": {
|
|
127
|
+
const messages = getMessages(["required", "invalid_type"], prop.errorMessages);
|
|
128
|
+
const intMessage = { message: prop.errorMessages?.integer || "validation.number.integer" };
|
|
129
|
+
zodChain = `z.number(${JSON.stringify(messages)}).int(${JSON.stringify(intMessage)})`;
|
|
130
|
+
if (prop.minimum !== void 0) {
|
|
131
|
+
const msg = { message: prop.errorMessages?.minimum || "validation.number.min" };
|
|
132
|
+
zodChain += `.min(${prop.minimum}, ${JSON.stringify(msg)})`;
|
|
133
|
+
}
|
|
134
|
+
if (prop.maximum !== void 0) {
|
|
135
|
+
const msg = { message: prop.errorMessages?.maximum || "validation.number.max" };
|
|
136
|
+
zodChain += `.max(${prop.maximum}, ${JSON.stringify(msg)})`;
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case "number": {
|
|
141
|
+
const messages = getMessages(["required", "invalid_type"], prop.errorMessages);
|
|
142
|
+
zodChain = `z.number(${JSON.stringify(messages)})`;
|
|
143
|
+
if (prop.minimum !== void 0) {
|
|
144
|
+
const msg = { message: prop.errorMessages?.minimum || "validation.number.min" };
|
|
145
|
+
zodChain += `.min(${prop.minimum}, ${JSON.stringify(msg)})`;
|
|
146
|
+
}
|
|
147
|
+
if (prop.maximum !== void 0) {
|
|
148
|
+
const msg = { message: prop.errorMessages?.maximum || "validation.number.max" };
|
|
149
|
+
zodChain += `.max(${prop.maximum}, ${JSON.stringify(msg)})`;
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case "boolean": {
|
|
154
|
+
const messages = getMessages(["required", "invalid_type"], prop.errorMessages);
|
|
155
|
+
zodChain = `z.boolean(${JSON.stringify(messages)})`;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case "array": {
|
|
159
|
+
const itemSchema = prop.items ? _propToZod(prop.items) : "z.any()";
|
|
160
|
+
const messages = getMessages(["required", "invalid_type"], prop.errorMessages);
|
|
161
|
+
zodChain = `z.array(${itemSchema}, ${JSON.stringify(messages)})`;
|
|
162
|
+
if (prop.minItems !== void 0) {
|
|
163
|
+
const msg = { message: prop.errorMessages?.minItems || "validation.array.min" };
|
|
164
|
+
zodChain += `.min(${prop.minItems}, ${JSON.stringify(msg)})`;
|
|
165
|
+
}
|
|
166
|
+
if (prop.maxItems !== void 0) {
|
|
167
|
+
const msg = { message: prop.errorMessages?.maxItems || "validation.array.max" };
|
|
168
|
+
zodChain += `.max(${prop.maxItems}, ${JSON.stringify(msg)})`;
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
case "object": {
|
|
173
|
+
if (prop.properties && prop.properties.length > 0) {
|
|
174
|
+
const shape = prop.properties.map((p) => ` ${p.name}: ${_propToZod(p)}`).join(",\n");
|
|
175
|
+
zodChain = `z.object({
|
|
176
|
+
${shape}
|
|
177
|
+
})`;
|
|
178
|
+
} else {
|
|
179
|
+
zodChain = "z.record(z.unknown())";
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
default:
|
|
184
|
+
zodChain = "z.any()";
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
if (prop.description) {
|
|
188
|
+
zodChain += `.describe(${JSON.stringify(prop.description)})`;
|
|
189
|
+
}
|
|
190
|
+
if (!prop.isRequired) {
|
|
191
|
+
zodChain += ".optional()";
|
|
192
|
+
}
|
|
193
|
+
if (prop.isNullable) {
|
|
194
|
+
zodChain += ".nullable()";
|
|
195
|
+
}
|
|
196
|
+
if (prop.defaultValue !== void 0) {
|
|
197
|
+
zodChain += `.default(${JSON.stringify(prop.defaultValue)})`;
|
|
198
|
+
}
|
|
199
|
+
return zodChain;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// src/generator/index.ts
|
|
77
203
|
var DEBUG_MODE = process.env.DEBUG === "true";
|
|
78
204
|
var debugLog = (title, data) => DEBUG_MODE && console.log(import_chalk.default.yellow(`
|
|
79
205
|
[DEBUG: ${title}]`), import_util.default.inspect(data, { depth: 5, colors: true }));
|
|
@@ -321,71 +447,6 @@ import type { ${schemasToImport.join(", ")} } from './types';
|
|
|
321
447
|
import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "index.ts"), indexContent.join("\n"));
|
|
322
448
|
console.log(import_chalk.default.gray(` \u2713 index.ts`));
|
|
323
449
|
}
|
|
324
|
-
function _propToZod(prop) {
|
|
325
|
-
let chain;
|
|
326
|
-
const requiredErrorMessage = { required_error: `${prop.name} is required.` };
|
|
327
|
-
switch (prop.type) {
|
|
328
|
-
case "string":
|
|
329
|
-
chain = `z.string({ ...requiredErrorMessage, invalid_type_error: "Expected a string for ${prop.name}" })`;
|
|
330
|
-
if (prop.format === "email") chain += `.email({ message: "Invalid email address for ${prop.name}" })`;
|
|
331
|
-
if (prop.format === "uuid") chain += `.uuid({ message: "Invalid UUID for ${prop.name}" })`;
|
|
332
|
-
if (prop.format === "url") chain += `.url({ message: "Invalid URL for ${prop.name}" })`;
|
|
333
|
-
if (prop.format === "datetime") chain += `.datetime({ message: "Invalid datetime format for ${prop.name}" })`;
|
|
334
|
-
if (prop.minLength !== void 0) chain += `.min(${prop.minLength}, { message: "${prop.name} must be at least ${prop.minLength} characters long" })`;
|
|
335
|
-
if (prop.maxLength !== void 0) chain += `.max(${prop.maxLength}, { message: "${prop.name} must be at most ${prop.maxLength} characters long" })`;
|
|
336
|
-
if (prop.pattern) chain += `.regex(/${prop.pattern}/, { message: "Invalid format for ${prop.name}" })`;
|
|
337
|
-
if (prop.enum) {
|
|
338
|
-
if (prop.enum.length > 0) {
|
|
339
|
-
chain = `z.enum(${JSON.stringify(prop.enum)})`;
|
|
340
|
-
} else {
|
|
341
|
-
chain = `z.string().refine(() => false, { message: "Enum for ${prop.name} is empty" })`;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
break;
|
|
345
|
-
case "integer":
|
|
346
|
-
chain = `z.number({ ...requiredErrorMessage, invalid_type_error: "Expected a number for ${prop.name}" }).int({ message: "${prop.name} must be an integer" })`;
|
|
347
|
-
if (prop.minimum !== void 0) chain += `.min(${prop.minimum}, { message: "${prop.name} must be at least ${prop.minimum}" })`;
|
|
348
|
-
if (prop.maximum !== void 0) chain += `.max(${prop.maximum}, { message: "${prop.name} must be at most ${prop.maximum}" })`;
|
|
349
|
-
break;
|
|
350
|
-
case "number":
|
|
351
|
-
chain = `z.number({ ...requiredErrorMessage, invalid_type_error: "Expected a number for ${prop.name}" })`;
|
|
352
|
-
if (prop.minimum !== void 0) chain += `.min(${prop.minimum}, { message: "${prop.name} must be at least ${prop.minimum}" })`;
|
|
353
|
-
if (prop.maximum !== void 0) chain += `.max(${prop.maximum}, { message: "${prop.name} must be at most ${prop.maximum}" })`;
|
|
354
|
-
break;
|
|
355
|
-
case "boolean":
|
|
356
|
-
chain = `z.boolean({ ...requiredErrorMessage, invalid_type_error: "Expected a boolean for ${prop.name}" })`;
|
|
357
|
-
break;
|
|
358
|
-
case "array":
|
|
359
|
-
const itemSchema = prop.items ? _propToZod(prop.items) : "z.any()";
|
|
360
|
-
chain = `z.array(${itemSchema})`;
|
|
361
|
-
if (prop.minItems !== void 0) chain += `.min(${prop.minItems}, { message: "${prop.name} must contain at least ${prop.minItems} item(s)" })`;
|
|
362
|
-
if (prop.maxItems !== void 0) chain += `.max(${prop.maxItems}, { message: "${prop.name} must contain at most ${prop.maxItems} item(s)" })`;
|
|
363
|
-
break;
|
|
364
|
-
case "object":
|
|
365
|
-
if (prop.properties && prop.properties.length > 0) {
|
|
366
|
-
const shape = prop.properties.map((p) => ` ${p.name}: ${_propToZod(p)}`).join(",\n");
|
|
367
|
-
chain = `z.object({
|
|
368
|
-
${shape}
|
|
369
|
-
})`;
|
|
370
|
-
} else {
|
|
371
|
-
chain = "z.record(z.unknown())";
|
|
372
|
-
}
|
|
373
|
-
break;
|
|
374
|
-
default:
|
|
375
|
-
chain = "z.any()";
|
|
376
|
-
break;
|
|
377
|
-
}
|
|
378
|
-
if (prop.description) {
|
|
379
|
-
chain += `.describe(${JSON.stringify(prop.description)})`;
|
|
380
|
-
}
|
|
381
|
-
if (!prop.isRequired) {
|
|
382
|
-
chain += ".optional()";
|
|
383
|
-
}
|
|
384
|
-
if (prop.isNullable) {
|
|
385
|
-
chain += ".nullable()";
|
|
386
|
-
}
|
|
387
|
-
return chain;
|
|
388
|
-
}
|
|
389
450
|
function _propToMock(prop) {
|
|
390
451
|
if (prop.example) return prop.example;
|
|
391
452
|
if (prop.name.match(/image|avatar|logo|url/i)) return "https://via.placeholder.com/150";
|