hono-takibi 0.0.1
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/README.md +471 -0
- package/dist/core/schema/references/find-references.d.ts +35 -0
- package/dist/core/schema/references/find-references.js +42 -0
- package/dist/core/schema/references/get-camel-case-schema-name.d.ts +7 -0
- package/dist/core/schema/references/get-camel-case-schema-name.js +14 -0
- package/dist/core/schema/references/get-ref-name.d.ts +38 -0
- package/dist/core/schema/references/get-ref-name.js +47 -0
- package/dist/core/schema/references/resolve-schema-order.d.ts +30 -0
- package/dist/core/schema/references/resolve-schema-order.js +48 -0
- package/dist/core/schema/references/resolve-schema-references.d.ts +49 -0
- package/dist/core/schema/references/resolve-schema-references.js +61 -0
- package/dist/core/schema/references/resolve-schemas-dependencies.d.ts +49 -0
- package/dist/core/schema/references/resolve-schemas-dependencies.js +65 -0
- package/dist/core/schema/references/traverse_schema.d.ts +48 -0
- package/dist/core/schema/references/traverse_schema.js +70 -0
- package/dist/core/text/capitalize.d.ts +18 -0
- package/dist/core/text/capitalize.js +23 -0
- package/dist/core/text/decapitalize.d.ts +17 -0
- package/dist/core/text/decapitalize.js +22 -0
- package/dist/core/text/remove-zod-prefix.d.ts +8 -0
- package/dist/core/text/remove-zod-prefix.js +13 -0
- package/dist/core/validator/is-format-number.d.ts +2 -0
- package/dist/core/validator/is-format-number.js +6 -0
- package/dist/core/validator/is-format-string.d.ts +2 -0
- package/dist/core/validator/is-format-string.js +24 -0
- package/dist/core/validator/is-http-method.d.ts +9 -0
- package/dist/core/validator/is-http-method.js +19 -0
- package/dist/core/validator/is-operation.d.ts +15 -0
- package/dist/core/validator/is-operation.js +19 -0
- package/dist/core/zod/get-zod-string-format.d.ts +30 -0
- package/dist/core/zod/get-zod-string-format.js +66 -0
- package/dist/generators/hono/generate-hono.d.ts +9 -0
- package/dist/generators/hono/generate-hono.js +29 -0
- package/dist/generators/openapi/components/generate-components-code.d.ts +46 -0
- package/dist/generators/openapi/components/generate-components-code.js +79 -0
- package/dist/generators/openapi/paths/generate-create-route.d.ts +58 -0
- package/dist/generators/openapi/paths/generate-create-route.js +61 -0
- package/dist/generators/openapi/paths/generate-route-code.d.ts +53 -0
- package/dist/generators/openapi/paths/generate-route-code.js +85 -0
- package/dist/generators/openapi/paths/generate-route-name.d.ts +30 -0
- package/dist/generators/openapi/paths/generate-route-name.js +48 -0
- package/dist/generators/openapi/paths/generate-route.d.ts +51 -0
- package/dist/generators/openapi/paths/generate-route.js +73 -0
- package/dist/generators/openapi/paths/generate-schemas-export.d.ts +11 -0
- package/dist/generators/openapi/paths/generate-schemas-export.js +18 -0
- package/dist/generators/request/body/generate-insert-request-body.d.ts +32 -0
- package/dist/generators/request/body/generate-insert-request-body.js +37 -0
- package/dist/generators/request/body/generate-request-body.d.ts +18 -0
- package/dist/generators/request/body/generate-request-body.js +23 -0
- package/dist/generators/request/object/generate-format-request-object.d.ts +29 -0
- package/dist/generators/request/object/generate-format-request-object.js +34 -0
- package/dist/generators/request/params/generate-params-object.d.ts +62 -0
- package/dist/generators/request/params/generate-params-object.js +93 -0
- package/dist/generators/request/params/generate-request-parameter.d.ts +49 -0
- package/dist/generators/request/params/generate-request-parameter.js +79 -0
- package/dist/generators/request/params/generate-request-params-array.d.ts +37 -0
- package/dist/generators/request/params/generate-request-params-array.js +56 -0
- package/dist/generators/request/params/generate-request-params.d.ts +34 -0
- package/dist/generators/request/params/generate-request-params.js +39 -0
- package/dist/generators/response/schemas/generate-response-schema.d.ts +59 -0
- package/dist/generators/response/schemas/generate-response-schema.js +83 -0
- package/dist/generators/zod/generate-zod-array.d.ts +31 -0
- package/dist/generators/zod/generate-zod-array.js +36 -0
- package/dist/generators/zod/generate-zod-coerce.d.ts +10 -0
- package/dist/generators/zod/generate-zod-coerce.js +17 -0
- package/dist/generators/zod/generate-zod-integer-schema.d.ts +15 -0
- package/dist/generators/zod/generate-zod-integer-schema.js +24 -0
- package/dist/generators/zod/generate-zod-number-schema.d.ts +16 -0
- package/dist/generators/zod/generate-zod-number-schema.js +26 -0
- package/dist/generators/zod/generate-zod-object-schema.d.ts +36 -0
- package/dist/generators/zod/generate-zod-object-schema.js +43 -0
- package/dist/generators/zod/generate-zod-properties-schema.d.ts +50 -0
- package/dist/generators/zod/generate-zod-properties-schema.js +60 -0
- package/dist/generators/zod/generate-zod-property-schema.d.ts +25 -0
- package/dist/generators/zod/generate-zod-property-schema.js +47 -0
- package/dist/generators/zod/generate-zod-record-schema.d.ts +34 -0
- package/dist/generators/zod/generate-zod-record-schema.js +37 -0
- package/dist/generators/zod/generate-zod-schema-definition.d.ts +22 -0
- package/dist/generators/zod/generate-zod-schema-definition.js +27 -0
- package/dist/generators/zod/generate-zod-schema.d.ts +59 -0
- package/dist/generators/zod/generate-zod-schema.js +136 -0
- package/dist/generators/zod/generate-zod-string-schema.d.ts +46 -0
- package/dist/generators/zod/generate-zod-string-schema.js +54 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +85 -0
- package/dist/types/index.d.ts +170 -0
- package/dist/types/index.js +2 -0
- package/package.json +47 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a request object with parameters and optional request body
|
|
3
|
+
*
|
|
4
|
+
* @function generateRequestParams
|
|
5
|
+
* @param required - Whether the request body is required
|
|
6
|
+
* @param zodSchema - Zod schema string for request body validation
|
|
7
|
+
* @returns Generated request body configuration string
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // Required request body with user schema
|
|
11
|
+
* generateRequestBody(true, 'z.object({ name: z.string() })')
|
|
12
|
+
* // Returns:
|
|
13
|
+
* // body:{
|
|
14
|
+
* // required:true,
|
|
15
|
+
* // content:{
|
|
16
|
+
* // 'application/json':{
|
|
17
|
+
* // schema:z.object({ name: z.string() }),
|
|
18
|
+
* // },
|
|
19
|
+
* // },
|
|
20
|
+
* // },
|
|
21
|
+
*
|
|
22
|
+
* // Optional request body with post schema
|
|
23
|
+
* generateRequestBody(false, 'z.object({ title: z.string() })')
|
|
24
|
+
* // Returns:
|
|
25
|
+
* // body:{
|
|
26
|
+
* // required:false,
|
|
27
|
+
* // content:{
|
|
28
|
+
* // 'application/json':{
|
|
29
|
+
* // schema:z.object({ title: z.string() }),
|
|
30
|
+
* // },
|
|
31
|
+
* // },
|
|
32
|
+
* // },
|
|
33
|
+
*/
|
|
34
|
+
export declare function generateRequestParams(requestBodyCode: string): string;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateRequestParams = generateRequestParams;
|
|
4
|
+
/**
|
|
5
|
+
* Generates a request object with parameters and optional request body
|
|
6
|
+
*
|
|
7
|
+
* @function generateRequestParams
|
|
8
|
+
* @param required - Whether the request body is required
|
|
9
|
+
* @param zodSchema - Zod schema string for request body validation
|
|
10
|
+
* @returns Generated request body configuration string
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Required request body with user schema
|
|
14
|
+
* generateRequestBody(true, 'z.object({ name: z.string() })')
|
|
15
|
+
* // Returns:
|
|
16
|
+
* // body:{
|
|
17
|
+
* // required:true,
|
|
18
|
+
* // content:{
|
|
19
|
+
* // 'application/json':{
|
|
20
|
+
* // schema:z.object({ name: z.string() }),
|
|
21
|
+
* // },
|
|
22
|
+
* // },
|
|
23
|
+
* // },
|
|
24
|
+
*
|
|
25
|
+
* // Optional request body with post schema
|
|
26
|
+
* generateRequestBody(false, 'z.object({ title: z.string() })')
|
|
27
|
+
* // Returns:
|
|
28
|
+
* // body:{
|
|
29
|
+
* // required:false,
|
|
30
|
+
* // content:{
|
|
31
|
+
* // 'application/json':{
|
|
32
|
+
* // schema:z.object({ title: z.string() }),
|
|
33
|
+
* // },
|
|
34
|
+
* // },
|
|
35
|
+
* // },
|
|
36
|
+
*/
|
|
37
|
+
function generateRequestParams(requestBodyCode) {
|
|
38
|
+
return `request:{${requestBodyCode}},`;
|
|
39
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Responses } from '../../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Generates a response schema for different status codes
|
|
4
|
+
*
|
|
5
|
+
* @function generateResponseSchema
|
|
6
|
+
* @param responses - OpenAPI response definitions for different status codes
|
|
7
|
+
* @returns Generated TypeScript code string for response validation
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const responses = {
|
|
11
|
+
* '200': {
|
|
12
|
+
* description: 'Success response',
|
|
13
|
+
* content: {
|
|
14
|
+
* 'application/json': {
|
|
15
|
+
* schema: {
|
|
16
|
+
* type: 'object',
|
|
17
|
+
* properties: {
|
|
18
|
+
* users: {
|
|
19
|
+
* type: 'array',
|
|
20
|
+
* items: { $ref: '#/components/schemas/User' }
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* },
|
|
27
|
+
* '404': {
|
|
28
|
+
* description: 'Not found',
|
|
29
|
+
* content: {
|
|
30
|
+
* 'application/json': {
|
|
31
|
+
* schema: { $ref: '#/components/schemas/Error' }
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* },
|
|
35
|
+
* '204': {
|
|
36
|
+
* description: 'No content'
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* const schema = generateResponseSchema(responses)
|
|
41
|
+
* // Returns:
|
|
42
|
+
* // 200:{description:'Success response',content:{'application/json':{schema:z.object({users:z.array(User)})}}},
|
|
43
|
+
* // 404:{description:'Not found',content:{'application/json':{schema:Error}}},
|
|
44
|
+
* // 204:{description:'No content',}
|
|
45
|
+
*
|
|
46
|
+
* @note
|
|
47
|
+
* - Handles multiple response status codes
|
|
48
|
+
* - Supports:
|
|
49
|
+
* - JSON response bodies
|
|
50
|
+
* - Schema references
|
|
51
|
+
* - Array responses
|
|
52
|
+
* - No-content responses
|
|
53
|
+
* - Inline schema definitions
|
|
54
|
+
* - Generates Zod validation schemas for each response
|
|
55
|
+
* - Preserves response descriptions
|
|
56
|
+
* - Handles nested schema structures
|
|
57
|
+
* - Automatically resolves schema references
|
|
58
|
+
*/
|
|
59
|
+
export declare function generateResponseSchema(responses: Responses): string;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateResponseSchema = generateResponseSchema;
|
|
4
|
+
const generate_zod_property_schema_1 = require("../../zod/generate-zod-property-schema");
|
|
5
|
+
/**
|
|
6
|
+
* Generates a response schema for different status codes
|
|
7
|
+
*
|
|
8
|
+
* @function generateResponseSchema
|
|
9
|
+
* @param responses - OpenAPI response definitions for different status codes
|
|
10
|
+
* @returns Generated TypeScript code string for response validation
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const responses = {
|
|
14
|
+
* '200': {
|
|
15
|
+
* description: 'Success response',
|
|
16
|
+
* content: {
|
|
17
|
+
* 'application/json': {
|
|
18
|
+
* schema: {
|
|
19
|
+
* type: 'object',
|
|
20
|
+
* properties: {
|
|
21
|
+
* users: {
|
|
22
|
+
* type: 'array',
|
|
23
|
+
* items: { $ref: '#/components/schemas/User' }
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* },
|
|
30
|
+
* '404': {
|
|
31
|
+
* description: 'Not found',
|
|
32
|
+
* content: {
|
|
33
|
+
* 'application/json': {
|
|
34
|
+
* schema: { $ref: '#/components/schemas/Error' }
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* },
|
|
38
|
+
* '204': {
|
|
39
|
+
* description: 'No content'
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* const schema = generateResponseSchema(responses)
|
|
44
|
+
* // Returns:
|
|
45
|
+
* // 200:{description:'Success response',content:{'application/json':{schema:z.object({users:z.array(User)})}}},
|
|
46
|
+
* // 404:{description:'Not found',content:{'application/json':{schema:Error}}},
|
|
47
|
+
* // 204:{description:'No content',}
|
|
48
|
+
*
|
|
49
|
+
* @note
|
|
50
|
+
* - Handles multiple response status codes
|
|
51
|
+
* - Supports:
|
|
52
|
+
* - JSON response bodies
|
|
53
|
+
* - Schema references
|
|
54
|
+
* - Array responses
|
|
55
|
+
* - No-content responses
|
|
56
|
+
* - Inline schema definitions
|
|
57
|
+
* - Generates Zod validation schemas for each response
|
|
58
|
+
* - Preserves response descriptions
|
|
59
|
+
* - Handles nested schema structures
|
|
60
|
+
* - Automatically resolves schema references
|
|
61
|
+
*/
|
|
62
|
+
function generateResponseSchema(responses) {
|
|
63
|
+
// 1. get response codes (200, 404, etc.)
|
|
64
|
+
const responseCodes = Object.keys(responses);
|
|
65
|
+
// 2. processing for each response code
|
|
66
|
+
const responseEntries = responseCodes.map((code) => {
|
|
67
|
+
const response = responses[code];
|
|
68
|
+
// 2.1 no content (description only response)
|
|
69
|
+
if (!response.content)
|
|
70
|
+
return `${code}:{description:'${response.description}',},`;
|
|
71
|
+
// 2.2 processing application/json content types
|
|
72
|
+
const jsonContent = response.content['application/json'];
|
|
73
|
+
if (!jsonContent)
|
|
74
|
+
return `${code}:{description:'${response.description}',},`;
|
|
75
|
+
// 2.3 generate zod schema
|
|
76
|
+
const schema = jsonContent.schema;
|
|
77
|
+
const zodSchema = (0, generate_zod_property_schema_1.generatePropertySchema)(schema);
|
|
78
|
+
// 2.4 generating a response definition
|
|
79
|
+
return `${code}:{description:'${response.description}',content:{'application/json':{schema:${zodSchema},},},},`;
|
|
80
|
+
});
|
|
81
|
+
// 3.combine all response definitions
|
|
82
|
+
return responseEntries.join('');
|
|
83
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a Zod array schema with the specified schema type
|
|
3
|
+
*
|
|
4
|
+
* @function generateZodArray
|
|
5
|
+
* @param zodSchema - Schema type for array elements (e.g., 'Address', 'z.string().min(3)')
|
|
6
|
+
* @returns Zod array schema string wrapped with z.array()
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* // With type reference
|
|
10
|
+
* generateZodArray('Address')
|
|
11
|
+
* // Returns: 'z.array(Address)'
|
|
12
|
+
*
|
|
13
|
+
* // With primitive Zod schema
|
|
14
|
+
* generateZodArray('z.string()')
|
|
15
|
+
* // Returns: 'z.array(z.string())'
|
|
16
|
+
*
|
|
17
|
+
* // With complex Zod schema
|
|
18
|
+
* generateZodArray('z.string().min(3).max(10)')
|
|
19
|
+
* // Returns: 'z.array(z.string().min(3).max(10))'
|
|
20
|
+
*
|
|
21
|
+
* // With nested schema
|
|
22
|
+
* generateZodArray('z.object({ name: z.string() })')
|
|
23
|
+
* // Returns: 'z.array(z.object({ name: z.string() }))'
|
|
24
|
+
*
|
|
25
|
+
* @note
|
|
26
|
+
* - Commonly used in OpenAPI to Zod schema conversion
|
|
27
|
+
* - Accepts both type references and Zod schema strings
|
|
28
|
+
* - The input schema should be a valid Zod schema or type reference
|
|
29
|
+
* - Used for generating array type validations
|
|
30
|
+
*/
|
|
31
|
+
export declare function generateZodArray(zodSchema: string): string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateZodArray = generateZodArray;
|
|
4
|
+
/**
|
|
5
|
+
* Generates a Zod array schema with the specified schema type
|
|
6
|
+
*
|
|
7
|
+
* @function generateZodArray
|
|
8
|
+
* @param zodSchema - Schema type for array elements (e.g., 'Address', 'z.string().min(3)')
|
|
9
|
+
* @returns Zod array schema string wrapped with z.array()
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // With type reference
|
|
13
|
+
* generateZodArray('Address')
|
|
14
|
+
* // Returns: 'z.array(Address)'
|
|
15
|
+
*
|
|
16
|
+
* // With primitive Zod schema
|
|
17
|
+
* generateZodArray('z.string()')
|
|
18
|
+
* // Returns: 'z.array(z.string())'
|
|
19
|
+
*
|
|
20
|
+
* // With complex Zod schema
|
|
21
|
+
* generateZodArray('z.string().min(3).max(10)')
|
|
22
|
+
* // Returns: 'z.array(z.string().min(3).max(10))'
|
|
23
|
+
*
|
|
24
|
+
* // With nested schema
|
|
25
|
+
* generateZodArray('z.object({ name: z.string() })')
|
|
26
|
+
* // Returns: 'z.array(z.object({ name: z.string() }))'
|
|
27
|
+
*
|
|
28
|
+
* @note
|
|
29
|
+
* - Commonly used in OpenAPI to Zod schema conversion
|
|
30
|
+
* - Accepts both type references and Zod schema strings
|
|
31
|
+
* - The input schema should be a valid Zod schema or type reference
|
|
32
|
+
* - Used for generating array type validations
|
|
33
|
+
*/
|
|
34
|
+
function generateZodArray(zodSchema) {
|
|
35
|
+
return `z.array(${zodSchema})`;
|
|
36
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description
|
|
3
|
+
* Generates a zod pipe function to coerce a value to a zod schema.
|
|
4
|
+
*
|
|
5
|
+
* @function generateZodCoerce
|
|
6
|
+
* @param z - The zod schema to coerce to.
|
|
7
|
+
* @param zodSchema - The zod schema to coerce.
|
|
8
|
+
* @returns A zod pipe function to coerce a value to a zod schema.
|
|
9
|
+
*/
|
|
10
|
+
export declare function generateZodCoerce(z: string, zodSchema: string): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateZodCoerce = generateZodCoerce;
|
|
4
|
+
const remove_zod_prefix_1 = require("../../core/text/remove-zod-prefix");
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* Generates a zod pipe function to coerce a value to a zod schema.
|
|
8
|
+
*
|
|
9
|
+
* @function generateZodCoerce
|
|
10
|
+
* @param z - The zod schema to coerce to.
|
|
11
|
+
* @param zodSchema - The zod schema to coerce.
|
|
12
|
+
* @returns A zod pipe function to coerce a value to a zod schema.
|
|
13
|
+
*/
|
|
14
|
+
function generateZodCoerce(z, zodSchema) {
|
|
15
|
+
const zod = (0, remove_zod_prefix_1.removeZodPrefix)(zodSchema);
|
|
16
|
+
return `${z}.pipe(z.coerce.${zod})`;
|
|
17
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
type GenerateZodIntegerSchemaParams = {
|
|
2
|
+
minLength?: number;
|
|
3
|
+
maxLength?: number;
|
|
4
|
+
minimum?: number;
|
|
5
|
+
maximum?: number;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Generates a zod schema for an integer.
|
|
9
|
+
*
|
|
10
|
+
* @function generateZodIntegerSchema
|
|
11
|
+
* @param args - The parameters to generate the zod schema.
|
|
12
|
+
* @returns A zod schema for an integer.
|
|
13
|
+
*/
|
|
14
|
+
export declare function generateZodIntegerSchema(args: GenerateZodIntegerSchemaParams): string;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateZodIntegerSchema = generateZodIntegerSchema;
|
|
4
|
+
/**
|
|
5
|
+
* Generates a zod schema for an integer.
|
|
6
|
+
*
|
|
7
|
+
* @function generateZodIntegerSchema
|
|
8
|
+
* @param args - The parameters to generate the zod schema.
|
|
9
|
+
* @returns A zod schema for an integer.
|
|
10
|
+
*/
|
|
11
|
+
function generateZodIntegerSchema(args) {
|
|
12
|
+
const validations = ['z.number().int()'];
|
|
13
|
+
const { minLength, maxLength, minimum, maximum } = args;
|
|
14
|
+
if (minLength)
|
|
15
|
+
validations.push(`.min(${minLength})`);
|
|
16
|
+
if (maxLength)
|
|
17
|
+
validations.push(`.max(${maxLength})`);
|
|
18
|
+
// 0 falsy value
|
|
19
|
+
if (typeof minimum === 'number')
|
|
20
|
+
validations.push(`.min(${minimum})`);
|
|
21
|
+
if (typeof maximum === 'number')
|
|
22
|
+
validations.push(`.max(${maximum})`);
|
|
23
|
+
return validations.join('');
|
|
24
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type GenerateZodNumberSchemaParams = {
|
|
2
|
+
pattern?: string;
|
|
3
|
+
minLength?: number;
|
|
4
|
+
maxLength?: number;
|
|
5
|
+
minimum?: number;
|
|
6
|
+
maximum?: number;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Generates a zod schema for a number.
|
|
10
|
+
*
|
|
11
|
+
* @function generateZodNumberSchema
|
|
12
|
+
* @param args - The parameters to generate the zod schema.
|
|
13
|
+
* @returns A zod schema for a number.
|
|
14
|
+
*/
|
|
15
|
+
export declare function generateZodNumberSchema(args: GenerateZodNumberSchemaParams): string;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateZodNumberSchema = generateZodNumberSchema;
|
|
4
|
+
/**
|
|
5
|
+
* Generates a zod schema for a number.
|
|
6
|
+
*
|
|
7
|
+
* @function generateZodNumberSchema
|
|
8
|
+
* @param args - The parameters to generate the zod schema.
|
|
9
|
+
* @returns A zod schema for a number.
|
|
10
|
+
*/
|
|
11
|
+
function generateZodNumberSchema(args) {
|
|
12
|
+
const validations = ['z.number()'];
|
|
13
|
+
const { pattern, minLength, maxLength, minimum, maximum } = args;
|
|
14
|
+
if (pattern)
|
|
15
|
+
validations.push(`.regex(/${pattern}/)`);
|
|
16
|
+
if (minLength)
|
|
17
|
+
validations.push(`.min(${minLength})`);
|
|
18
|
+
if (maxLength)
|
|
19
|
+
validations.push(`.max(${maxLength})`);
|
|
20
|
+
// 0 falsy value
|
|
21
|
+
if (typeof minimum === 'number')
|
|
22
|
+
validations.push(`.min(${minimum})`);
|
|
23
|
+
if (typeof maximum === 'number')
|
|
24
|
+
validations.push(`.max(${maximum})`);
|
|
25
|
+
return validations.join('');
|
|
26
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a Zod object schema from a record of property types
|
|
3
|
+
*
|
|
4
|
+
* @function generateZodObjectSchema
|
|
5
|
+
* @param object - Record of property names and their Zod type strings
|
|
6
|
+
* @returns Generated Zod object schema string
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* // Basic object
|
|
10
|
+
* generateZodObjectSchema({ name: 'z.string()' })
|
|
11
|
+
* // Returns: 'z.object({name:z.string()})'
|
|
12
|
+
*
|
|
13
|
+
* // Multiple properties
|
|
14
|
+
* generateZodObjectSchema({
|
|
15
|
+
* name: 'z.string()',
|
|
16
|
+
* age: 'z.number()',
|
|
17
|
+
* isActive: 'z.boolean()'
|
|
18
|
+
* })
|
|
19
|
+
* // Returns: 'z.object({name:z.string(),age:z.number(),isActive:z.boolean()})'
|
|
20
|
+
*
|
|
21
|
+
* // With validated types
|
|
22
|
+
* generateZodObjectSchema({
|
|
23
|
+
* email: 'z.string().email()',
|
|
24
|
+
* age: 'z.number().min(0)'
|
|
25
|
+
* })
|
|
26
|
+
* // Returns: 'z.object({email:z.string().email(),age:z.number().min(0)})'
|
|
27
|
+
*
|
|
28
|
+
* @note
|
|
29
|
+
* - Each property value should be a valid Zod type string
|
|
30
|
+
* - Properties are joined with commas in the resulting object
|
|
31
|
+
* - Commonly used for generating simple object schemas
|
|
32
|
+
* - Does not support nested objects directly (use full schema generator for complex objects)
|
|
33
|
+
*
|
|
34
|
+
* @see generateZodSchema for complex object schema generation
|
|
35
|
+
*/
|
|
36
|
+
export declare function generateZodObjectSchema(object: Record<string, string>): string;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateZodObjectSchema = generateZodObjectSchema;
|
|
4
|
+
/**
|
|
5
|
+
* Generates a Zod object schema from a record of property types
|
|
6
|
+
*
|
|
7
|
+
* @function generateZodObjectSchema
|
|
8
|
+
* @param object - Record of property names and their Zod type strings
|
|
9
|
+
* @returns Generated Zod object schema string
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // Basic object
|
|
13
|
+
* generateZodObjectSchema({ name: 'z.string()' })
|
|
14
|
+
* // Returns: 'z.object({name:z.string()})'
|
|
15
|
+
*
|
|
16
|
+
* // Multiple properties
|
|
17
|
+
* generateZodObjectSchema({
|
|
18
|
+
* name: 'z.string()',
|
|
19
|
+
* age: 'z.number()',
|
|
20
|
+
* isActive: 'z.boolean()'
|
|
21
|
+
* })
|
|
22
|
+
* // Returns: 'z.object({name:z.string(),age:z.number(),isActive:z.boolean()})'
|
|
23
|
+
*
|
|
24
|
+
* // With validated types
|
|
25
|
+
* generateZodObjectSchema({
|
|
26
|
+
* email: 'z.string().email()',
|
|
27
|
+
* age: 'z.number().min(0)'
|
|
28
|
+
* })
|
|
29
|
+
* // Returns: 'z.object({email:z.string().email(),age:z.number().min(0)})'
|
|
30
|
+
*
|
|
31
|
+
* @note
|
|
32
|
+
* - Each property value should be a valid Zod type string
|
|
33
|
+
* - Properties are joined with commas in the resulting object
|
|
34
|
+
* - Commonly used for generating simple object schemas
|
|
35
|
+
* - Does not support nested objects directly (use full schema generator for complex objects)
|
|
36
|
+
*
|
|
37
|
+
* @see generateZodSchema for complex object schema generation
|
|
38
|
+
*/
|
|
39
|
+
function generateZodObjectSchema(object) {
|
|
40
|
+
return `z.object({${Object.entries(object)
|
|
41
|
+
.map(([key, val]) => `${key}:${val}`)
|
|
42
|
+
.join(',')}})`;
|
|
43
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Schema } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Generates a Zod object schema with properties and their requirements
|
|
4
|
+
*
|
|
5
|
+
* @function generateZodPropertiesSchema
|
|
6
|
+
* @param properties - Record of property names to their schema definitions
|
|
7
|
+
* @param required - Array of property names that are required
|
|
8
|
+
* @returns Generated Zod object schema string
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // Order schema with optional fields
|
|
12
|
+
* generateZodPropertiesSchema(
|
|
13
|
+
* {
|
|
14
|
+
* id: { type: 'integer', format: 'int64' },
|
|
15
|
+
* status: { type: 'string', enum: ['placed', 'approved', 'delivered'] }
|
|
16
|
+
* },
|
|
17
|
+
* []
|
|
18
|
+
* )
|
|
19
|
+
* // Returns: 'z.object({id: z.number().int().optional(),status: z.enum(["placed","approved","delivered"]).optional()})'
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Pet schema with required fields
|
|
23
|
+
* generateZodPropertiesSchema(
|
|
24
|
+
* {
|
|
25
|
+
* name: { type: 'string' },
|
|
26
|
+
* photoUrls: { type: 'array', items: { type: 'string' } }
|
|
27
|
+
* },
|
|
28
|
+
* ['name', 'photoUrls']
|
|
29
|
+
* )
|
|
30
|
+
* // Returns: 'z.object({name: z.string(),photoUrls: z.array(z.string())})'
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // Address schema with references
|
|
34
|
+
* generateZodPropertiesSchema(
|
|
35
|
+
* {
|
|
36
|
+
* category: { '$ref': '#/components/schemas/Category' },
|
|
37
|
+
* tags: { type: 'array', items: { '$ref': '#/components/schemas/Tag' } }
|
|
38
|
+
* },
|
|
39
|
+
* []
|
|
40
|
+
* )
|
|
41
|
+
* // Returns: 'z.object({category: categorySchema.optional(),tags: z.array(tagSchema).optional()})'
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* - Generates Zod schema strings for object properties
|
|
45
|
+
* - Handles required and optional properties
|
|
46
|
+
* - Supports primitive types, arrays, and references
|
|
47
|
+
* - Uses .partial() when no properties are required
|
|
48
|
+
* - Maintains property order from input
|
|
49
|
+
*/
|
|
50
|
+
export declare function generateZodPropertiesSchema(properties: Record<string, Schema>, required: string[]): string;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateZodPropertiesSchema = generateZodPropertiesSchema;
|
|
4
|
+
const generate_zod_property_schema_1 = require("./generate-zod-property-schema");
|
|
5
|
+
/**
|
|
6
|
+
* Generates a Zod object schema with properties and their requirements
|
|
7
|
+
*
|
|
8
|
+
* @function generateZodPropertiesSchema
|
|
9
|
+
* @param properties - Record of property names to their schema definitions
|
|
10
|
+
* @param required - Array of property names that are required
|
|
11
|
+
* @returns Generated Zod object schema string
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Order schema with optional fields
|
|
15
|
+
* generateZodPropertiesSchema(
|
|
16
|
+
* {
|
|
17
|
+
* id: { type: 'integer', format: 'int64' },
|
|
18
|
+
* status: { type: 'string', enum: ['placed', 'approved', 'delivered'] }
|
|
19
|
+
* },
|
|
20
|
+
* []
|
|
21
|
+
* )
|
|
22
|
+
* // Returns: 'z.object({id: z.number().int().optional(),status: z.enum(["placed","approved","delivered"]).optional()})'
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Pet schema with required fields
|
|
26
|
+
* generateZodPropertiesSchema(
|
|
27
|
+
* {
|
|
28
|
+
* name: { type: 'string' },
|
|
29
|
+
* photoUrls: { type: 'array', items: { type: 'string' } }
|
|
30
|
+
* },
|
|
31
|
+
* ['name', 'photoUrls']
|
|
32
|
+
* )
|
|
33
|
+
* // Returns: 'z.object({name: z.string(),photoUrls: z.array(z.string())})'
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* // Address schema with references
|
|
37
|
+
* generateZodPropertiesSchema(
|
|
38
|
+
* {
|
|
39
|
+
* category: { '$ref': '#/components/schemas/Category' },
|
|
40
|
+
* tags: { type: 'array', items: { '$ref': '#/components/schemas/Tag' } }
|
|
41
|
+
* },
|
|
42
|
+
* []
|
|
43
|
+
* )
|
|
44
|
+
* // Returns: 'z.object({category: categorySchema.optional(),tags: z.array(tagSchema).optional()})'
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* - Generates Zod schema strings for object properties
|
|
48
|
+
* - Handles required and optional properties
|
|
49
|
+
* - Supports primitive types, arrays, and references
|
|
50
|
+
* - Uses .partial() when no properties are required
|
|
51
|
+
* - Maintains property order from input
|
|
52
|
+
*/
|
|
53
|
+
function generateZodPropertiesSchema(properties, required) {
|
|
54
|
+
const objectProperties = Object.entries(properties).map(([key, schema]) => {
|
|
55
|
+
const isRequired = required.includes(key);
|
|
56
|
+
const propertySchema = (0, generate_zod_property_schema_1.generatePropertySchema)(schema);
|
|
57
|
+
return `${key}: ${propertySchema}${isRequired ? '' : '.optional()'}`;
|
|
58
|
+
});
|
|
59
|
+
return `z.object({${objectProperties}})${required ? '' : '.partial()'}`;
|
|
60
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Schema } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Generates a Zod schema string for a given OpenAPI schema definition
|
|
4
|
+
*
|
|
5
|
+
* @function generatePropertySchema
|
|
6
|
+
* @param schema - OpenAPI schema definition
|
|
7
|
+
* @returns Generated Zod schema string
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // Reference type
|
|
11
|
+
* generatePropertySchema({ $ref: '#/components/schemas/User' })
|
|
12
|
+
* // Returns: 'userSchema'
|
|
13
|
+
*
|
|
14
|
+
* // Array with reference type
|
|
15
|
+
* generatePropertySchema({
|
|
16
|
+
* type: 'array',
|
|
17
|
+
* items: { $ref: '#/components/schemas/Post' }
|
|
18
|
+
* })
|
|
19
|
+
* // Returns: 'z.array(postSchema)'
|
|
20
|
+
*
|
|
21
|
+
* // Basic type
|
|
22
|
+
* generatePropertySchema({ type: 'string' })
|
|
23
|
+
* // Returns: 'z.string()'
|
|
24
|
+
*/
|
|
25
|
+
export declare function generatePropertySchema(schema: Schema): string;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generatePropertySchema = generatePropertySchema;
|
|
4
|
+
const get_ref_name_1 = require("../../core/schema/references/get-ref-name");
|
|
5
|
+
const generate_zod_array_1 = require("./generate-zod-array");
|
|
6
|
+
const generate_zod_schema_1 = require("./generate-zod-schema");
|
|
7
|
+
const get_camel_case_schema_name_1 = require("../../core/schema/references/get-camel-case-schema-name");
|
|
8
|
+
/**
|
|
9
|
+
* Generates a Zod schema string for a given OpenAPI schema definition
|
|
10
|
+
*
|
|
11
|
+
* @function generatePropertySchema
|
|
12
|
+
* @param schema - OpenAPI schema definition
|
|
13
|
+
* @returns Generated Zod schema string
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Reference type
|
|
17
|
+
* generatePropertySchema({ $ref: '#/components/schemas/User' })
|
|
18
|
+
* // Returns: 'userSchema'
|
|
19
|
+
*
|
|
20
|
+
* // Array with reference type
|
|
21
|
+
* generatePropertySchema({
|
|
22
|
+
* type: 'array',
|
|
23
|
+
* items: { $ref: '#/components/schemas/Post' }
|
|
24
|
+
* })
|
|
25
|
+
* // Returns: 'z.array(postSchema)'
|
|
26
|
+
*
|
|
27
|
+
* // Basic type
|
|
28
|
+
* generatePropertySchema({ type: 'string' })
|
|
29
|
+
* // Returns: 'z.string()'
|
|
30
|
+
*/
|
|
31
|
+
function generatePropertySchema(schema) {
|
|
32
|
+
if (schema.$ref) {
|
|
33
|
+
const refName = (0, get_ref_name_1.getRefName)(schema.$ref);
|
|
34
|
+
if (refName) {
|
|
35
|
+
const camelCaseRefName = (0, get_camel_case_schema_name_1.getCamelCaseSchemaName)(refName);
|
|
36
|
+
return (0, get_ref_name_1.getRefName)(camelCaseRefName) || 'z.any()';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (schema.type === 'array' && schema.items?.$ref) {
|
|
40
|
+
const refName = (0, get_ref_name_1.getRefName)(schema.items.$ref);
|
|
41
|
+
if (refName) {
|
|
42
|
+
const camelCaseRefName = (0, get_camel_case_schema_name_1.getCamelCaseSchemaName)(refName);
|
|
43
|
+
return (0, generate_zod_array_1.generateZodArray)(camelCaseRefName);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return (0, generate_zod_schema_1.generateZodSchema)(schema);
|
|
47
|
+
}
|