api-core-lib 12.0.74 → 12.0.76
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 +30 -141
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -74,132 +74,6 @@ 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
|
|
203
77
|
var DEBUG_MODE = process.env.DEBUG === "true";
|
|
204
78
|
var debugLog = (title, data) => DEBUG_MODE && console.log(import_chalk.default.yellow(`
|
|
205
79
|
[DEBUG: ${title}]`), import_util.default.inspect(data, { depth: 5, colors: true }));
|
|
@@ -402,25 +276,40 @@ ${prop.example ? ` * @example ${JSON.stringify(prop.example)}
|
|
|
402
276
|
import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "types.ts"), typesContent);
|
|
403
277
|
console.log(import_chalk.default.gray(` \u2713 types.ts`));
|
|
404
278
|
indexContent.push(`export * from './types';`);
|
|
405
|
-
let validationContent =
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
});
|
|
279
|
+
let validationContent = "";
|
|
280
|
+
if (schemasToImport.length > 0) {
|
|
281
|
+
const firstSchemaName = schemasToImport[0];
|
|
282
|
+
validationContent = `
|
|
283
|
+
// =============================================================================
|
|
284
|
+
// III. \u0645\u062B\u0627\u0644 \u0639\u0644\u0649 \u0643\u064A\u0641\u064A\u0629 \u0627\u0633\u062A\u062E\u062F\u0627\u0645 \u0627\u0644\u0645\u062E\u0637\u0637 \u0641\u064A \u0627\u0644\u0643\u0648\u062F
|
|
285
|
+
// =============================================================================
|
|
286
|
+
/*
|
|
287
|
+
function
|
|
288
|
+
|
|
289
|
+
validate${firstSchemaName}(data: unknown) {
|
|
290
|
+
try {
|
|
291
|
+
const validatedData = ${firstSchemaName}Schema.parse(data);
|
|
292
|
+
console.log("\u2705 Validation successful:", validatedData);
|
|
293
|
+
return { success: true, data: validatedData };
|
|
294
|
+
} catch (error) {
|
|
295
|
+
if (error instanceof ZodError) {
|
|
296
|
+
console.error("\u274C Validation failed:");
|
|
297
|
+
// .format() is great for getting a structured error object
|
|
298
|
+
console.log(JSON.stringify(error.format(), null, 2));
|
|
299
|
+
return { success: false, errors: error.format() };
|
|
300
|
+
}
|
|
301
|
+
throw error; // Rethrow other unexpected errors
|
|
302
|
+
}
|
|
303
|
+
}
|
|
418
304
|
|
|
305
|
+
// Example usage with invalid data:
|
|
306
|
+
const invalidData = { /* ... \u0628\u064A\u0627\u0646\u0627\u062A \u063A\u064A\u0631 \u0643\u0627\u0645\u0644\u0629 \u0623\u0648 \u062E\u0627\u0637\u0626\u0629 ... */ };
|
|
307
|
+
// validate${firstSchemaName}(invalidData);
|
|
308
|
+
*/
|
|
419
309
|
`;
|
|
420
|
-
}
|
|
421
310
|
}
|
|
422
311
|
import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "validation.ts"), validationContent);
|
|
423
|
-
console.log(import_chalk.default.gray(` \u2713 validation.ts`));
|
|
312
|
+
console.log(import_chalk.default.gray(` \u2713 validation.ts (with integrated usage example)`));
|
|
424
313
|
indexContent.push(`export * from './validation';`);
|
|
425
314
|
let mocksContent = `// This file is auto-generated.
|
|
426
315
|
import type { ${schemasToImport.join(", ")} } from './types';
|