api-core-lib 12.0.74 → 12.0.75

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.
Files changed (2) hide show
  1. package/dist/cli.cjs +119 -57
  2. package/package.json +1 -1
package/dist/cli.cjs CHANGED
@@ -77,95 +77,73 @@ var import_openapi_types = __toESM(require_dist(), 1);
77
77
 
78
78
  // src/generator/core/_propToZod.ts
79
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
80
  let zodChain;
81
+ const getRuleMessage = (rule, defaultKey) => {
82
+ const customMessages = prop.errorMessages;
83
+ const message = customMessages?.[rule] || defaultKey;
84
+ return `{ "message": "${message}" }`;
85
+ };
94
86
  switch (prop.type) {
95
87
  case "string": {
96
- const messages = getMessages(["required", "invalid_type"], prop.errorMessages);
97
- zodChain = `z.string(${JSON.stringify(messages)})`;
98
88
  if (prop.enum && prop.enum.length > 0) {
89
+ const messages = {
90
+ required_error: prop.errorMessages?.required || "validation.required",
91
+ invalid_type_error: prop.errorMessages?.invalid_type || "validation.string.enum"
92
+ };
99
93
  zodChain = `z.enum(${JSON.stringify(prop.enum)}, ${JSON.stringify(messages)})`;
100
94
  } 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)})`;
95
+ zodChain = "z.string()";
96
+ if (prop.isRequired) {
97
+ zodChain += `.min(1, ${getRuleMessage("minLength", "validation.string.nonempty")})`;
107
98
  }
108
99
  if (prop.maxLength !== void 0) {
109
- const msg = { message: prop.errorMessages?.maxLength || "validation.string.max" };
110
- zodChain += `.max(${prop.maxLength}, ${JSON.stringify(msg)})`;
100
+ zodChain += `.max(${prop.maxLength}, ${getRuleMessage("maxLength", "validation.string.max")})`;
111
101
  }
112
102
  if (prop.pattern) {
113
- const msg = { message: prop.errorMessages?.pattern || "validation.string.regex" };
114
- zodChain += `.regex(/${prop.pattern}/, ${JSON.stringify(msg)})`;
103
+ zodChain += `.regex(/${prop.pattern}/, ${getRuleMessage("pattern", "validation.string.regex")})`;
115
104
  }
116
105
  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)})`;
106
+ const msg = getRuleMessage("format", `validation.string.${prop.format}`);
107
+ if (prop.format === "email") zodChain += `.email(${msg})`;
108
+ if (prop.format === "url") zodChain += `.url(${msg})`;
109
+ if (prop.format === "uuid") zodChain += `.uuid(${msg})`;
110
+ if (prop.format === "datetime") zodChain += `.datetime(${msg})`;
122
111
  }
123
112
  }
124
113
  break;
125
114
  }
126
115
  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)})`;
116
+ zodChain = `z.number().int(${getRuleMessage("integer", "validation.number.integer")})`;
130
117
  if (prop.minimum !== void 0) {
131
- const msg = { message: prop.errorMessages?.minimum || "validation.number.min" };
132
- zodChain += `.min(${prop.minimum}, ${JSON.stringify(msg)})`;
118
+ zodChain += `.min(${prop.minimum}, ${getRuleMessage("minimum", "validation.number.min")})`;
133
119
  }
134
120
  if (prop.maximum !== void 0) {
135
- const msg = { message: prop.errorMessages?.maximum || "validation.number.max" };
136
- zodChain += `.max(${prop.maximum}, ${JSON.stringify(msg)})`;
121
+ zodChain += `.max(${prop.maximum}, ${getRuleMessage("maximum", "validation.number.max")})`;
137
122
  }
138
123
  break;
139
124
  }
140
125
  case "number": {
141
- const messages = getMessages(["required", "invalid_type"], prop.errorMessages);
142
- zodChain = `z.number(${JSON.stringify(messages)})`;
126
+ zodChain = "z.number()";
143
127
  if (prop.minimum !== void 0) {
144
- const msg = { message: prop.errorMessages?.minimum || "validation.number.min" };
145
- zodChain += `.min(${prop.minimum}, ${JSON.stringify(msg)})`;
128
+ zodChain += `.min(${prop.minimum}, ${getRuleMessage("minimum", "validation.number.min")})`;
146
129
  }
147
130
  if (prop.maximum !== void 0) {
148
- const msg = { message: prop.errorMessages?.maximum || "validation.number.max" };
149
- zodChain += `.max(${prop.maximum}, ${JSON.stringify(msg)})`;
131
+ zodChain += `.max(${prop.maximum}, ${getRuleMessage("maximum", "validation.number.max")})`;
150
132
  }
151
133
  break;
152
134
  }
153
135
  case "boolean": {
154
- const messages = getMessages(["required", "invalid_type"], prop.errorMessages);
155
- zodChain = `z.boolean(${JSON.stringify(messages)})`;
136
+ zodChain = "z.boolean()";
156
137
  break;
157
138
  }
158
139
  case "array": {
159
140
  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)})`;
141
+ zodChain = `z.array(${itemSchema})`;
162
142
  if (prop.minItems !== void 0) {
163
- const msg = { message: prop.errorMessages?.minItems || "validation.array.min" };
164
- zodChain += `.min(${prop.minItems}, ${JSON.stringify(msg)})`;
143
+ zodChain += `.min(${prop.minItems}, ${getRuleMessage("minItems", "validation.array.min")})`;
165
144
  }
166
145
  if (prop.maxItems !== void 0) {
167
- const msg = { message: prop.errorMessages?.maxItems || "validation.array.max" };
168
- zodChain += `.max(${prop.maxItems}, ${JSON.stringify(msg)})`;
146
+ zodChain += `.max(${prop.maxItems}, ${getRuleMessage("maxItems", "validation.array.max")})`;
169
147
  }
170
148
  break;
171
149
  }
@@ -402,25 +380,109 @@ ${prop.example ? ` * @example ${JSON.stringify(prop.example)}
402
380
  import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "types.ts"), typesContent);
403
381
  console.log(import_chalk.default.gray(` \u2713 types.ts`));
404
382
  indexContent.push(`export * from './types';`);
405
- let validationContent = `// This file is auto-generated.
406
- import { z } from 'zod';
407
- `;
408
- if (enumsToImport.length > 0) validationContent += `import { ${enumsToImport.join(", ")} } from './enums';
383
+ const validationFileHeader = `// This file is auto-generated.
384
+ import { z, ZodIssueCode, ZodError } from 'zod';
385
+ ${enumsToImport.length > 0 ? `import { ${enumsToImport.join(", ")} } from './enums';
386
+ ` : ""}
387
+ // =============================================================================
388
+ // I. \u0625\u0639\u062F\u0627\u062F \u062E\u0631\u064A\u0637\u0629 \u0623\u062E\u0637\u0627\u0621 \u0639\u0627\u0644\u0645\u064A\u0629 (Global Error Map) \u0644\u062F\u0639\u0645 \u0627\u0644\u062A\u0631\u062C\u0645\u0629
389
+ // =============================================================================
390
+ //
391
+ // \u0645\u0644\u0627\u062D\u0638\u0629 \u0647\u0627\u0645\u0629: \u0641\u064A \u062A\u0637\u0628\u064A\u0642 \u062D\u0642\u064A\u0642\u064A\u060C \u064A\u062C\u0628 \u062A\u0639\u0631\u064A\u0641 \u0647\u0630\u0647 \u0627\u0644\u062E\u0631\u064A\u0637\u0629 \u0645\u0631\u0629 \u0648\u0627\u062D\u062F\u0629 \u0641\u0642\u0637
392
+ // \u0641\u064A \u0646\u0642\u0637\u0629 \u0627\u0644\u062F\u062E\u0648\u0644 \u0644\u062A\u0637\u0628\u064A\u0642\u0643 (entry-point). \u062A\u0645 \u0648\u0636\u0639\u0647\u0627 \u0647\u0646\u0627 \u0644\u0644\u062A\u0648\u0636\u064A\u062D.
393
+ //
394
+ // \u0627\u0644\u0647\u062F\u0641: \u0631\u0628\u0637 \u0645\u0641\u0627\u062A\u064A\u062D \u0627\u0644\u062A\u0631\u062C\u0645\u0629 (\u0645\u062B\u0644 "validation.required") \u0627\u0644\u062A\u064A \u062A\u0645 \u062A\u0648\u0644\u064A\u062F\u0647\u0627
395
+ // \u0641\u064A \u0627\u0644\u0645\u062E\u0637\u0637\u0627\u062A \u0623\u062F\u0646\u0627\u0647 \u0628\u0631\u0633\u0627\u0626\u0644 \u062D\u0642\u064A\u0642\u064A\u0629 \u064A\u0645\u0643\u0646 \u0639\u0631\u0636\u0647\u0627 \u0644\u0644\u0645\u0633\u062A\u062E\u062F\u0645.
396
+
397
+ // 1. \u0645\u062B\u0627\u0644 \u0628\u0633\u064A\u0637 \u0644\u062F\u0627\u0644\u0629 \u062A\u0631\u062C\u0645\u0629 (\u0641\u064A \u062A\u0637\u0628\u064A\u0642\u0643 \u0633\u062A\u0633\u062A\u062E\u062F\u0645 \u0645\u0643\u062A\u0628\u0629 \u0645\u062B\u0644 i18next)
398
+ const t = (key: string, args?: any): string => {
399
+ // \u0647\u0646\u0627 \u062A\u0636\u0639 \u0645\u0646\u0637\u0642 \u0627\u0644\u062A\u0631\u062C\u0645\u0629 \u0627\u0644\u062D\u0642\u064A\u0642\u064A \u0627\u0644\u062E\u0627\u0635 \u0628\u0643.
400
+ // \u0647\u0630\u0627 \u0645\u062C\u0631\u062F \u0645\u062B\u0627\u0644 \u0628\u0633\u064A\u0637.
401
+ if (key === 'validation.string.nonempty') return '\u0647\u0630\u0627 \u0627\u0644\u062D\u0642\u0644 \u0645\u0637\u0644\u0648\u0628 \u0648\u0644\u0627 \u064A\u0645\u0643\u0646 \u0623\u0646 \u064A\u0643\u0648\u0646 \u0641\u0627\u0631\u063A\u064B\u0627.';
402
+ if (key === 'validation.string.email') return '\u0627\u0644\u0631\u062C\u0627\u0621 \u0625\u062F\u062E\u0627\u0644 \u0628\u0631\u064A\u062F \u0625\u0644\u0643\u062A\u0631\u0648\u0646\u064A \u0635\u0627\u0644\u062D.';
403
+ if (key === 'validation.required') return '\u0647\u0630\u0627 \u0627\u0644\u062D\u0642\u0644 \u0625\u0644\u0632\u0627\u0645\u064A.';
404
+ // \u0631\u0633\u0627\u0644\u0629 \u0627\u062D\u062A\u064A\u0627\u0637\u064A\u0629
405
+ return key.split('.').pop()?.replace(/_/g, ' ') || key;
406
+ };
407
+
408
+ // 2. \u062A\u0639\u0631\u064A\u0641 \u062E\u0631\u064A\u0637\u0629 \u0627\u0644\u0623\u062E\u0637\u0627\u0621 \u0627\u0644\u0645\u062E\u0635\u0635\u0629
409
+ const customErrorMap: z.ZodErrorMap = (issue, ctx) => {
410
+ if (issue.message) {
411
+ // \u0625\u0630\u0627 \u0643\u0627\u0646\u062A \u0627\u0644\u0631\u0633\u0627\u0644\u0629 \u0647\u064A \u0645\u0641\u062A\u0627\u062D \u062A\u0631\u062C\u0645\u0629\u060C \u0642\u0645 \u0628\u062A\u0631\u062C\u0645\u062A\u0647\u0627
412
+ if (issue.message.startsWith('validation.')) {
413
+ return { message: t(issue.message) };
414
+ }
415
+ // \u0648\u0625\u0644\u0627\u060C \u0641\u0647\u064A \u0631\u0633\u0627\u0644\u0629 \u0645\u062E\u0635\u0635\u0629 \u0645\u0628\u0627\u0634\u0631\u0629
416
+ return { message: issue.message };
417
+ }
418
+
419
+ // \u062A\u0631\u062C\u0645\u0629 \u0627\u0644\u0623\u062E\u0637\u0627\u0621 \u0627\u0644\u0627\u0641\u062A\u0631\u0627\u0636\u064A\u0629 \u0627\u0644\u0639\u0627\u0645\u0629
420
+ if (issue.code === ZodIssueCode.invalid_type) {
421
+ return { message: t('validation.required') };
422
+ }
423
+
424
+ return { message: ctx.defaultError };
425
+ };
409
426
 
427
+ // 3. \u0642\u0645 \u0628\u062A\u0639\u064A\u064A\u0646 \u0627\u0644\u062E\u0631\u064A\u0637\u0629 \u0627\u0644\u0645\u062E\u0635\u0635\u0629 \u0639\u0627\u0644\u0645\u064A\u064B\u0627 (\u064A\u062C\u0628 \u0639\u0645\u0644 \u0630\u0644\u0643 \u0645\u0631\u0629 \u0648\u0627\u062D\u062F\u0629 \u0641\u064A \u0627\u0644\u062A\u0637\u0628\u064A\u0642)
428
+ z.setErrorMap(customErrorMap);
429
+
430
+ // =============================================================================
431
+ // II. \u0645\u062E\u0637\u0637\u0627\u062A \u0627\u0644\u062A\u062D\u0642\u0642 \u0627\u0644\u0645\u0648\u0644\u062F\u0629 \u062A\u0644\u0642\u0627\u0626\u064A\u064B\u0627 (Generated Validation Schemas)
432
+ // =============================================================================
410
433
  `;
434
+ let validationContent = validationFileHeader;
411
435
  for (const typeName of schemasToImport) {
412
436
  const parsedSchema = allSchemas.get(typeName);
413
437
  if (parsedSchema) {
414
438
  let zodShape = parsedSchema.properties.map((p) => ` ${p.name}: ${_propToZod(p)}`).join(",\n");
439
+ validationContent += `
440
+ /**
441
+ * Schema for validating ${typeName} objects.
442
+ * @description ${parsedSchema.description || ""}
443
+ */
444
+ `;
415
445
  validationContent += `export const ${typeName}Schema = z.object({
416
446
  ${zodShape}
417
447
  });
418
-
448
+ `;
449
+ validationContent += `export type ${typeName}Schema = z.infer<typeof ${typeName}Schema>;
419
450
  `;
420
451
  }
421
452
  }
453
+ if (schemasToImport.length > 0) {
454
+ const firstSchemaName = schemasToImport[0];
455
+ validationContent += `
456
+ // =============================================================================
457
+ // 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
458
+ // =============================================================================
459
+ /*
460
+ function
461
+
462
+ validate${firstSchemaName}(data: unknown) {
463
+ try {
464
+ const validatedData = ${firstSchemaName}Schema.parse(data);
465
+ console.log("\u2705 Validation successful:", validatedData);
466
+ return { success: true, data: validatedData };
467
+ } catch (error) {
468
+ if (error instanceof ZodError) {
469
+ console.error("\u274C Validation failed:");
470
+ // .format() is great for getting a structured error object
471
+ console.log(JSON.stringify(error.format(), null, 2));
472
+ return { success: false, errors: error.format() };
473
+ }
474
+ throw error; // Rethrow other unexpected errors
475
+ }
476
+ }
477
+
478
+ // Example usage with invalid data:
479
+ const invalidData = { /* ... \u0628\u064A\u0627\u0646\u0627\u062A \u063A\u064A\u0631 \u0643\u0627\u0645\u0644\u0629 \u0623\u0648 \u062E\u0627\u0637\u0626\u0629 ... */ };
480
+ // validate${firstSchemaName}(invalidData);
481
+ */
482
+ `;
483
+ }
422
484
  import_fs.default.writeFileSync(import_path.default.join(moduleOutputPath, "validation.ts"), validationContent);
423
- console.log(import_chalk.default.gray(` \u2713 validation.ts`));
485
+ console.log(import_chalk.default.gray(` \u2713 validation.ts (with integrated usage example)`));
424
486
  indexContent.push(`export * from './validation';`);
425
487
  let mocksContent = `// This file is auto-generated.
426
488
  import type { ${schemasToImport.join(", ")} } from './types';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-core-lib",
3
- "version": "12.0.74",
3
+ "version": "12.0.75",
4
4
  "description": "A flexible and powerful API client library for modern web applications.",
5
5
  "type": "module",
6
6
  "exports": {