hono-takibi 0.6.2 → 0.6.3

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,6 @@
1
+ /**
2
+ * getToSafeIdentifierHelper
3
+ * @param {string} str - The string to be converted to a safe identifier
4
+ * @returns {string} - A normalized, safe identifier
5
+ */
6
+ export declare function getToSafeIdentifierHelper(str: string): string;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getToSafeIdentifierHelper = getToSafeIdentifierHelper;
4
+ /**
5
+ * getToSafeIdentifierHelper
6
+ * @param {string} str - The string to be converted to a safe identifier
7
+ * @returns {string} - A normalized, safe identifier
8
+ */
9
+ function getToSafeIdentifierHelper(str) {
10
+ const trimmed = str.trim();
11
+ if (trimmed.includes('-')) {
12
+ return trimmed.replace(/\s*-\s*/g, '_');
13
+ }
14
+ return trimmed;
15
+ }
@@ -20,13 +20,48 @@ function generateZodEnum(schema) {
20
20
  if (schema.type === 'number' && schema.enum) {
21
21
  return `z.literal(${schema.enum})`;
22
22
  }
23
+ // integer
24
+ if (schema.type === 'integer' && schema.enum) {
25
+ if (schema.enum.length > 1) {
26
+ const literals = schema.enum.map((value) => `z.literal(${value})`);
27
+ return `z.union([${literals}])`;
28
+ }
29
+ return `z.literal(${schema.enum[0]})`;
30
+ }
23
31
  // bigint
24
32
  if (schema.type === 'bigint' && schema.enum) {
25
- return `z.literal(${schema.enum}n)`;
33
+ return `z.literal(${schema.enum})`;
34
+ }
35
+ // array
36
+ if (schema.type === 'array' && Array.isArray(schema.enum)) {
37
+ if (schema.enum.length === 1 && Array.isArray(schema.enum[0])) {
38
+ const tupleItems = schema.enum[0].map((item) => `z.literal(${item})`).join(', ');
39
+ return `z.tuple([${tupleItems}])`;
40
+ }
41
+ // union
42
+ const unionParts = schema.enum.map((value) => {
43
+ if (Array.isArray(value)) {
44
+ const tupleItems = value.map((item) => `z.literal(${item})`);
45
+ return `z.tuple([${tupleItems}])`;
46
+ }
47
+ return `z.literal(${value})`;
48
+ });
49
+ return `z.union([${unionParts}])`;
26
50
  }
27
51
  // boolean
28
52
  if (schema.type === 'boolean' && schema.enum) {
29
53
  return `z.literal(${schema.enum})`;
30
54
  }
31
- return `z.enum(${JSON.stringify(schema.enum)})`;
55
+ // enum.length > 1
56
+ if (schema.enum.length > 1) {
57
+ const allStrings = schema.enum.every((v) => typeof v === 'string');
58
+ if (allStrings) {
59
+ return `z.enum(${JSON.stringify(schema.enum)})`;
60
+ }
61
+ const unionLiterals = schema.enum.map((value) => value === null
62
+ ? 'z.null()'
63
+ : `z.literal(${typeof value === 'string' ? `'${value}'` : value})`);
64
+ return `z.union([${unionLiterals}])`;
65
+ }
66
+ return `z.literal(${typeof schema.enum[0] === 'string' ? `'${schema.enum[0]}'` : schema.enum[0]})`;
32
67
  }
@@ -51,11 +51,15 @@ function generateZodNumber(args) {
51
51
  }
52
52
  // nonpositive
53
53
  if (args.minimum === 0 && !args.exclusiveMinimum) {
54
- validations.push('.nonpositive()');
54
+ if (!args.default) {
55
+ validations.push('.nonpositive()');
56
+ }
55
57
  }
56
58
  // negative
57
59
  if (args.maximum === 0 && args.exclusiveMaximum) {
58
- validations.push('.negative()');
60
+ if (!args.default) {
61
+ validations.push('.negative()');
62
+ }
59
63
  }
60
64
  // gt
61
65
  if (args.minimum) {
@@ -1,19 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateZod = generateZod;
4
- const generate_zod_array_1 = require("./generate-zod-array");
5
- const generate_zod_string_1 = require("./generate-zod-string");
6
4
  const is_format_string_1 = require("../../core/validator/is-format-string");
7
- const generate_zod_number_1 = require("./generate-zod-number");
8
- const generate_zod_integer_schema_1 = require("./generate-zod-integer-schema");
9
5
  const get_variable_schema_name_helper_1 = require("../../core/helper/get-variable-schema-name-helper");
10
6
  const generate_zod_object_1 = require("./generate-zod-object");
11
7
  const generate_zod_enum_1 = require("./generate-zod-enum");
12
8
  const generate_zod_max_1 = require("./generate-zod-max");
13
9
  const generate_zod_min_1 = require("./generate-zod-min");
10
+ const generate_zod_number_1 = require("./generate-zod-number");
11
+ const generate_zod_integer_schema_1 = require("./generate-zod-integer-schema");
12
+ const generate_zod_length_1 = require("./generate-zod-length");
13
+ const generate_zod_array_1 = require("./generate-zod-array");
14
+ const generate_zod_string_1 = require("./generate-zod-string");
14
15
  const strip_min_if_gt_exist_helper_1 = require("./helper/strip-min-if-gt-exist-helper");
15
16
  const strip_max_if_lt_exist_helper_1 = require("./helper/strip-max-if-lt-exist-helper");
16
- const generate_zod_length_1 = require("./generate-zod-length");
17
17
  const strip_min_max_exist_helper_1 = require("./helper/strip-min-max-exist-helper");
18
18
  const generate_oneof_code_1 = require("../zod-openapi-hono/openapi/component/oneof/generate-oneof-code");
19
19
  const generate_anyof_code_1 = require("../zod-openapi-hono/openapi/component/anyof/generate-anyof-code");
@@ -23,13 +23,20 @@ const generate_zod_infer_1 = require("../generate-zod-infer");
23
23
  */
24
24
  function generateZodToOpenAPISchemaDefinition(schemaName, zodSchema, config) {
25
25
  const variableName = (0, get_variable_schema_name_helper_1.getVariableSchemaNameHelper)(schemaName, config);
26
+ // "-" → "_"
27
+ const safeVariableName = variableName.replace(/[^a-zA-Z0-9_$]/g, '_');
28
+ // "-" → "_"
29
+ const safeSchemaName = schemaName.replace(/[^a-zA-Z0-9_$]/g, '_');
26
30
  // schema code
27
31
  const schemaCode = config.schema.export
28
- ? `export const ${variableName} = ${zodSchema}.openapi('${schemaName}')`
29
- : `const ${variableName} = ${zodSchema}.openapi('${schemaName}')`;
32
+ ? `export const ${safeVariableName} = ${zodSchema}.openapi('${safeSchemaName}')`
33
+ : `const ${safeVariableName} = ${zodSchema}.openapi('${safeSchemaName}')`;
30
34
  // zod infer code
31
35
  const typeVariableName = (0, get_variable_name_helper_1.getVariableNameHelper)(schemaName, config);
32
- const zodInferCode = config.type.export ? (0, generate_zod_infer_1.generateZodInfer)(typeVariableName, variableName) : '';
36
+ const safeTypeVariableName = typeVariableName.replace(/[^a-zA-Z0-9_$]/g, '_');
37
+ const zodInferCode = config.type.export
38
+ ? (0, generate_zod_infer_1.generateZodInfer)(safeTypeVariableName, safeVariableName)
39
+ : '';
33
40
  // return code
34
41
  return `${schemaCode}\n\n${zodInferCode}`;
35
42
  }
@@ -4,6 +4,7 @@ exports.generateZodPropertiesSchema = generateZodPropertiesSchema;
4
4
  const is_all_optional_1 = require("../../../core/validator/is-all-optional");
5
5
  const generate_zod_partial_schema_1 = require("../generate-zod-partial-schema");
6
6
  const generate_zod_property_schema_1 = require("./generate-zod-property-schema");
7
+ const get_to_safe_identifier_helper_1 = require("../../../core/helper/get-to-safe-identifier-helper");
7
8
  /**
8
9
  * Generates a Zod object schema with properties and their requirements
9
10
  * @param { Record<string, Schema> } properties - Record of property names to their schema definitions
@@ -50,7 +51,9 @@ function generateZodPropertiesSchema(properties, required, config) {
50
51
  const objectProperties = Object.entries(properties).map(([key, schema]) => {
51
52
  const isRequired = required.includes(key);
52
53
  const propertySchema = (0, generate_zod_property_schema_1.generatePropertySchema)(schema, config);
53
- return `${key}:${propertySchema}${isRequired ? '' : '.optional()'}`;
54
+ const safePropertySchema = (0, get_to_safe_identifier_helper_1.getToSafeIdentifierHelper)(propertySchema);
55
+ const safeKey = (0, get_to_safe_identifier_helper_1.getToSafeIdentifierHelper)(key);
56
+ return `${safeKey}:${safePropertySchema}${isRequired ? '' : '.optional()'}`;
54
57
  });
55
58
  // Check if all properties are optional
56
59
  const allOptional = (0, is_all_optional_1.isAllOptional)(objectProperties);
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateArrayReferenceSchema = generateArrayReferenceSchema;
4
+ const get_to_safe_identifier_helper_1 = require("../../../core/helper/get-to-safe-identifier-helper");
4
5
  const get_variable_schema_name_helper_1 = require("../../../core/helper/get-variable-schema-name-helper");
5
6
  const get_ref_name_1 = require("../../../core/schema/references/get-ref-name");
6
7
  const generate_zod_array_1 = require("../generate-zod-array");
@@ -19,5 +20,6 @@ function generateArrayReferenceSchema(schema, config) {
19
20
  return 'z.array(z.any())';
20
21
  }
21
22
  const variableName = (0, get_variable_schema_name_helper_1.getVariableSchemaNameHelper)(refName, config);
22
- return (0, generate_zod_array_1.generateZodArray)(variableName);
23
+ const safeVariableName = (0, get_to_safe_identifier_helper_1.getToSafeIdentifierHelper)(variableName);
24
+ return (0, generate_zod_array_1.generateZodArray)(safeVariableName);
23
25
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hono-takibi",
3
3
  "description": "Hono Takibi is a CLI tool that generates Hono routes from OpenAPI specifications.",
4
- "version": "0.6.2",
4
+ "version": "0.6.3",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "hono",