hono-takibi 0.0.9 → 0.1.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.
- package/dist/core/text/remove-zod-optional.d.ts +8 -0
- package/dist/core/text/remove-zod-optional.js +13 -0
- package/dist/core/validator/is-optional.d.ts +8 -0
- package/dist/core/validator/is-optional.js +13 -0
- package/dist/generators/zod/generate-zod-openapi-example.d.ts +13 -0
- package/dist/generators/zod/generate-zod-openapi-example.js +22 -0
- package/dist/generators/zod/generate-zod-properties-schema.js +2 -1
- package/dist/generators/zod/generate-zod-property-schema.js +8 -1
- package/dist/generators/zod/generate-zod-record-schema.js +1 -1
- package/dist/generators/zod/generate-zod-schema.js +1 -2
- package/dist/generators/zod/generate-zod-string-schema.d.ts +1 -2
- package/dist/generators/zod/generate-zod-string-schema.js +1 -3
- package/package.json +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes the `.optional()` method from a Zod schema string
|
|
3
|
+
*
|
|
4
|
+
* @function removeZodOptional
|
|
5
|
+
* @param zodSchema - The Zod schema string to remove the `.optional()` method from
|
|
6
|
+
* @returns The Zod schema string with the `.optional()` method removed
|
|
7
|
+
*/
|
|
8
|
+
export declare function removeZodOptional(zodSchema: string): string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.removeZodOptional = removeZodOptional;
|
|
4
|
+
/**
|
|
5
|
+
* Removes the `.optional()` method from a Zod schema string
|
|
6
|
+
*
|
|
7
|
+
* @function removeZodOptional
|
|
8
|
+
* @param zodSchema - The Zod schema string to remove the `.optional()` method from
|
|
9
|
+
* @returns The Zod schema string with the `.optional()` method removed
|
|
10
|
+
*/
|
|
11
|
+
function removeZodOptional(zodSchema) {
|
|
12
|
+
return zodSchema.replace('.optional()', '');
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a Zod schema string contains the `.optional()` method.
|
|
3
|
+
*
|
|
4
|
+
* @function isOptional
|
|
5
|
+
* @param zodSchema - The Zod schema string to check.
|
|
6
|
+
* @returns `true` if the `.optional()` method is present, `false` otherwise.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isOptional(zodSchema: string): boolean;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isOptional = isOptional;
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a Zod schema string contains the `.optional()` method.
|
|
6
|
+
*
|
|
7
|
+
* @function isOptional
|
|
8
|
+
* @param zodSchema - The Zod schema string to check.
|
|
9
|
+
* @returns `true` if the `.optional()` method is present, `false` otherwise.
|
|
10
|
+
*/
|
|
11
|
+
function isOptional(zodSchema) {
|
|
12
|
+
return zodSchema.includes('.optional()');
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a Zod schema string with OpenAPI example while preserving optional status
|
|
3
|
+
*
|
|
4
|
+
* @function generateZodOpenAPIExample
|
|
5
|
+
* @param schema - Original Zod schema string
|
|
6
|
+
* @param example - Example value to be added
|
|
7
|
+
* @returns Generated Zod schema string with OpenAPI example
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* generateZodOpenAPIExample('z.string().optional()', 'test@example.com')
|
|
11
|
+
* // Returns: 'z.string().optional().openapi({example:"test@example.com"})'
|
|
12
|
+
*/
|
|
13
|
+
export declare function generateZodOpenAPIExample(schema: string, example: unknown): string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateZodOpenAPIExample = generateZodOpenAPIExample;
|
|
4
|
+
const remove_zod_optional_1 = require("../../core/text/remove-zod-optional");
|
|
5
|
+
const is_optional_1 = require("../../core/validator/is-optional");
|
|
6
|
+
/**
|
|
7
|
+
* Generates a Zod schema string with OpenAPI example while preserving optional status
|
|
8
|
+
*
|
|
9
|
+
* @function generateZodOpenAPIExample
|
|
10
|
+
* @param schema - Original Zod schema string
|
|
11
|
+
* @param example - Example value to be added
|
|
12
|
+
* @returns Generated Zod schema string with OpenAPI example
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* generateZodOpenAPIExample('z.string().optional()', 'test@example.com')
|
|
16
|
+
* // Returns: 'z.string().optional().openapi({example:"test@example.com"})'
|
|
17
|
+
*/
|
|
18
|
+
function generateZodOpenAPIExample(schema, example) {
|
|
19
|
+
const hasOptional = (0, is_optional_1.isOptional)(schema);
|
|
20
|
+
const schemaWithoutOptional = (0, remove_zod_optional_1.removeZodOptional)(schema);
|
|
21
|
+
return `${schemaWithoutOptional}${hasOptional ? '.optional()' : ''}.openapi({example:${JSON.stringify(example)}})`;
|
|
22
|
+
}
|
|
@@ -54,7 +54,8 @@ function generateZodPropertiesSchema(properties, required) {
|
|
|
54
54
|
const objectProperties = Object.entries(properties).map(([key, schema]) => {
|
|
55
55
|
const isRequired = required.includes(key);
|
|
56
56
|
const propertySchema = (0, generate_zod_property_schema_1.generatePropertySchema)(schema);
|
|
57
|
-
return `${key}
|
|
57
|
+
return `${key}:${propertySchema}${isRequired ? '' : '.optional()'}`;
|
|
58
58
|
});
|
|
59
|
+
// Maybe you don't need to use .partial().
|
|
59
60
|
return `z.object({${objectProperties}})${required ? '' : '.partial()'}`;
|
|
60
61
|
}
|
|
@@ -5,6 +5,7 @@ const get_ref_name_1 = require("../../core/schema/references/get-ref-name");
|
|
|
5
5
|
const generate_zod_array_1 = require("./generate-zod-array");
|
|
6
6
|
const generate_zod_schema_1 = require("./generate-zod-schema");
|
|
7
7
|
const get_camel_case_schema_name_1 = require("../../core/schema/references/get-camel-case-schema-name");
|
|
8
|
+
const generate_zod_openapi_example_1 = require("./generate-zod-openapi-example");
|
|
8
9
|
/**
|
|
9
10
|
* Generates a Zod schema string for a given OpenAPI schema definition
|
|
10
11
|
*
|
|
@@ -43,5 +44,11 @@ function generatePropertySchema(schema) {
|
|
|
43
44
|
return (0, generate_zod_array_1.generateZodArray)(camelCaseRefName);
|
|
44
45
|
}
|
|
45
46
|
}
|
|
46
|
-
|
|
47
|
+
const zodSchema = (0, generate_zod_schema_1.generateZodSchema)(schema);
|
|
48
|
+
const { example } = schema;
|
|
49
|
+
// add example
|
|
50
|
+
if (example) {
|
|
51
|
+
return (0, generate_zod_openapi_example_1.generateZodOpenAPIExample)(zodSchema, example);
|
|
52
|
+
}
|
|
53
|
+
return zodSchema;
|
|
47
54
|
}
|
|
@@ -33,5 +33,5 @@ const generate_zod_schema_1 = require("./generate-zod-schema");
|
|
|
33
33
|
*/
|
|
34
34
|
function generateZodRecordSchema(additionalProperties) {
|
|
35
35
|
const schema = (0, generate_zod_schema_1.generateZodSchema)(additionalProperties);
|
|
36
|
-
return `z.record(z.string()
|
|
36
|
+
return `z.record(z.string(),${schema})`;
|
|
37
37
|
}
|
|
@@ -87,7 +87,7 @@ const TYPE_TO_ZOD_SCHEMA = {
|
|
|
87
87
|
* - Returns z.any() for unknown types with a warning
|
|
88
88
|
*/
|
|
89
89
|
function generateZodSchema(schema) {
|
|
90
|
-
const { type, format, pattern, minLength, maxLength, minimum, maximum,
|
|
90
|
+
const { type, format, pattern, minLength, maxLength, minimum, maximum, properties, required = [], items, enum: enumValues, additionalProperties, } = schema;
|
|
91
91
|
// enum
|
|
92
92
|
if (enumValues)
|
|
93
93
|
return `z.enum(${JSON.stringify(enumValues)})`;
|
|
@@ -105,7 +105,6 @@ function generateZodSchema(schema) {
|
|
|
105
105
|
pattern,
|
|
106
106
|
minLength,
|
|
107
107
|
maxLength,
|
|
108
|
-
example,
|
|
109
108
|
format: format && (0, is_format_string_1.isFormatString)(format) ? format : undefined,
|
|
110
109
|
});
|
|
111
110
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import type { FormatString
|
|
1
|
+
import type { FormatString } from '../../types';
|
|
2
2
|
type GenerateZodStringSchemaParams = {
|
|
3
3
|
pattern?: string;
|
|
4
4
|
minLength?: number;
|
|
5
5
|
maxLength?: number;
|
|
6
6
|
format?: FormatString;
|
|
7
|
-
example?: ExampleValue;
|
|
8
7
|
};
|
|
9
8
|
/**
|
|
10
9
|
* Generates a Zod schema string for string validation
|
|
@@ -41,7 +41,7 @@ const get_zod_string_format_1 = require("../../core/zod/get-zod-string-format");
|
|
|
41
41
|
*/
|
|
42
42
|
function generateZodStringSchema(args) {
|
|
43
43
|
const validations = ['z.string()'];
|
|
44
|
-
const { pattern, minLength, maxLength, format
|
|
44
|
+
const { pattern, minLength, maxLength, format } = args;
|
|
45
45
|
if (pattern)
|
|
46
46
|
validations.push(`.regex(/${pattern}/)`);
|
|
47
47
|
if (minLength)
|
|
@@ -50,7 +50,5 @@ function generateZodStringSchema(args) {
|
|
|
50
50
|
validations.push(`.max(${maxLength})`);
|
|
51
51
|
if (format)
|
|
52
52
|
validations.push((0, get_zod_string_format_1.getZodFormatString)(format));
|
|
53
|
-
if (example)
|
|
54
|
-
validations.push(`.openapi({example:${JSON.stringify(example)}})`);
|
|
55
53
|
return validations.join('');
|
|
56
54
|
}
|