hono-takibi 0.8.1 → 0.8.2
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/takibi.js +1 -1
- package/dist/generator/zod/helper/array-reference-schema.js +2 -2
- package/dist/generator/zod/index.js +5 -4
- package/dist/generator/zod/z/array.d.ts +1 -1
- package/dist/generator/zod/z/array.js +13 -20
- package/dist/generator/zod/z/enum.d.ts +1 -1
- package/dist/generator/zod/z/index.d.ts +0 -1
- package/dist/generator/zod/z/index.js +0 -1
- package/dist/generator/zod/z/integer.d.ts +1 -1
- package/dist/generator/zod/z/integer.js +72 -38
- package/dist/generator/zod/z/number.d.ts +8 -1
- package/dist/generator/zod/z/number.js +57 -25
- package/dist/generator/zod/z/object.d.ts +1 -1
- package/dist/generator/zod/z/object.js +3 -2
- package/dist/generator/zod/z/string.d.ts +1 -1
- package/dist/generator/zod/z/string.js +13 -15
- package/dist/generator/zod-openapi-hono/app/helper/docs.d.ts +1 -1
- package/dist/generator/zod-openapi-hono/openapi/route/params/query-parameter.d.ts +3 -3
- package/dist/generator/zod-openapi-hono/openapi/route/params/query-parameter.js +6 -7
- package/dist/generator/zod-openapi-hono/openapi/route/params/request-params-array.js +3 -2
- package/dist/generator/zod-openapi-hono/openapi/route/request/body/index.d.ts +2 -2
- package/dist/generator/zod-openapi-hono/openapi/route/request/body/index.js +6 -6
- package/dist/helper/allof.d.ts +1 -1
- package/dist/helper/allof.js +7 -3
- package/dist/helper/anyof.d.ts +1 -1
- package/dist/helper/anyof.js +2 -3
- package/dist/helper/index.d.ts +0 -1
- package/dist/helper/index.js +0 -1
- package/dist/helper/maybe-apply-nullability.d.ts +1 -1
- package/dist/helper/oneof.d.ts +1 -1
- package/dist/helper/oneof.js +3 -3
- package/dist/helper/pick-types.d.ts +1 -1
- package/dist/helper/resolve-schemas-dependencies.d.ts +1 -1
- package/dist/openapi/index.d.ts +190 -2
- package/dist/openapi/index.js +26 -1
- package/dist/typespec/index.d.ts +2 -1
- package/dist/typespec/index.js +15 -8
- package/dist/utils/index.d.ts +0 -131
- package/dist/utils/index.js +0 -162
- package/dist/vite-plugin/index.d.ts +1 -1
- package/dist/vite-plugin/index.js +36 -5
- package/package.json +1 -1
- package/dist/generator/zod/z/record.d.ts +0 -8
- package/dist/generator/zod/z/record.js +0 -12
- package/dist/helper/not.d.ts +0 -13
- package/dist/helper/not.js +0 -18
- package/dist/openapi/parse-openapi.d.ts +0 -9
- package/dist/openapi/parse-openapi.js +0 -23
- package/dist/openapi/types.d.ts +0 -181
- package/dist/openapi/types.js +0 -1
- package/dist/vite-plugin/vite-tsp.d.ts +0 -10
- package/dist/vite-plugin/vite-tsp.js +0 -33
- package/dist/vite-plugin/vite.d.ts +0 -10
- package/dist/vite-plugin/vite.js +0 -31
package/dist/cli/takibi.js
CHANGED
|
@@ -2,7 +2,7 @@ import path from 'node:path';
|
|
|
2
2
|
import { fmt } from '../format/index.js';
|
|
3
3
|
import { mkdir, writeFile } from '../fsp/index.js';
|
|
4
4
|
import { zodOpenAPIHono } from '../generator/zod-openapi-hono/openapi/index.js';
|
|
5
|
-
import { parseOpenAPI } from '../openapi/
|
|
5
|
+
import { parseOpenAPI } from '../openapi/index.js';
|
|
6
6
|
import { asyncAndThen, ok } from '../result/index.js';
|
|
7
7
|
import { templateCode } from './template-code.js';
|
|
8
8
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { refName } from '../../../utils/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Generates a Zod schema string for an array of referenced schemas.
|
|
4
4
|
*
|
|
@@ -36,5 +36,5 @@ export function arrayReferenceSchema(schema) {
|
|
|
36
36
|
if (!ref) {
|
|
37
37
|
return 'z.array(z.any())';
|
|
38
38
|
}
|
|
39
|
-
return array(
|
|
39
|
+
return `z.array(${ref}Schema)`;
|
|
40
40
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { allOf } from '../../helper/allof.js';
|
|
2
2
|
import { anyOf } from '../../helper/anyof.js';
|
|
3
3
|
import { maybeApplyNullability, pickTypes } from '../../helper/index.js';
|
|
4
|
-
import { not } from '../../helper/not.js';
|
|
5
4
|
import { oneOf } from '../../helper/oneof.js';
|
|
6
5
|
import { refName } from '../../utils/index.js';
|
|
7
6
|
import { _enum, array, integer, number, object, string } from './z/index.js';
|
|
@@ -59,8 +58,9 @@ import { _enum, array, integer, number, object, string } from './z/index.js';
|
|
|
59
58
|
*/
|
|
60
59
|
export function zod(schema) {
|
|
61
60
|
/* $ref */
|
|
62
|
-
if (schema.$ref)
|
|
61
|
+
if (schema.$ref) {
|
|
63
62
|
return `${refName(schema.$ref)}Schema`;
|
|
63
|
+
}
|
|
64
64
|
/* const */
|
|
65
65
|
if (schema.const !== undefined) {
|
|
66
66
|
return maybeApplyNullability(`z.literal(${JSON.stringify(schema.const)})`, schema);
|
|
@@ -111,11 +111,12 @@ export function zod(schema) {
|
|
|
111
111
|
return allOf(schema);
|
|
112
112
|
}
|
|
113
113
|
if (schema.not) {
|
|
114
|
-
return
|
|
114
|
+
return 'z.unknown()';
|
|
115
115
|
}
|
|
116
116
|
/* null only */
|
|
117
|
-
if (types.length === 1 && types[0] === 'null')
|
|
117
|
+
if (types.length === 1 && types[0] === 'null') {
|
|
118
118
|
return 'z.null()';
|
|
119
|
+
}
|
|
119
120
|
console.warn(`Unknown schema: ${JSON.stringify(schema)} - fallback to z.any()`);
|
|
120
121
|
return 'z.any()';
|
|
121
122
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Schema } from '../../../openapi/
|
|
1
|
+
import type { Schema } from '../../../openapi/index.js';
|
|
2
2
|
export declare function array(schema: Schema): string;
|
|
@@ -1,27 +1,20 @@
|
|
|
1
|
-
import { length, max, min } from '../../../utils/index.js';
|
|
2
1
|
import { zod } from '../index.js';
|
|
3
2
|
export function array(schema) {
|
|
4
|
-
// fall back to any[] if items が無い
|
|
5
3
|
const z = `z.array(${schema.items ? zod(schema.items) : 'z.any()'})`;
|
|
6
|
-
// minItems / maxItems → .min() / .max()
|
|
7
|
-
if (schema.minItems
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
// minItems / maxItems → .length() / .min() / .max()
|
|
5
|
+
if (typeof schema.minItems === 'number' && typeof schema.maxItems === 'number') {
|
|
6
|
+
// minItems === maxItems → .length(n)
|
|
7
|
+
if (schema.minItems === schema.maxItems) {
|
|
8
|
+
return `${z}.length(${schema.minItems})`;
|
|
9
|
+
}
|
|
10
|
+
// minItems ≠ maxItems → .min(n).max(n)
|
|
11
|
+
return `${z}.min(${schema.minItems}).max(${schema.maxItems})`;
|
|
11
12
|
}
|
|
12
|
-
if (schema.minItems
|
|
13
|
-
return `${z}${
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (schema.minLength !== undefined && schema.maxLength !== undefined) {
|
|
18
|
-
return schema.minLength === schema.maxLength
|
|
19
|
-
? `${z}${length(schema.minLength)}`
|
|
20
|
-
: `${z}${min(schema.minLength)}${max(schema.maxLength)}`;
|
|
13
|
+
if (typeof schema.minItems === 'number') {
|
|
14
|
+
return `${z}.min(${schema.minItems})`;
|
|
15
|
+
}
|
|
16
|
+
if (typeof schema.maxItems === 'number') {
|
|
17
|
+
return `${z}.max(${schema.maxItems})`;
|
|
21
18
|
}
|
|
22
|
-
if (schema.minLength !== undefined)
|
|
23
|
-
return `${z}${min(schema.minLength)}`;
|
|
24
|
-
if (schema.maxLength !== undefined)
|
|
25
|
-
return `${z}${max(schema.maxLength)}`;
|
|
26
19
|
return z;
|
|
27
20
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { regex } from '../../../utils/index.js';
|
|
2
1
|
/**
|
|
3
2
|
* Generates a Zod schema for integer types based on OpenAPI schema.
|
|
4
3
|
* Supports int32, int64, and bigint formats.
|
|
@@ -7,55 +6,90 @@ import { regex } from '../../../utils/index.js';
|
|
|
7
6
|
* @returns The Zod schema string
|
|
8
7
|
*/
|
|
9
8
|
export function integer(schema) {
|
|
10
|
-
/* ─────────────── 1. base type ─────────────── */
|
|
11
9
|
const isInt32 = schema.format === 'int32';
|
|
12
10
|
const isInt64 = schema.format === 'int64';
|
|
13
11
|
const isBigInt = schema.format === 'bigint';
|
|
14
|
-
const
|
|
12
|
+
const o = [
|
|
15
13
|
isInt32 ? 'z.int32()' : isInt64 ? 'z.int64()' : isBigInt ? 'z.bigint()' : 'z.int()',
|
|
16
14
|
];
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
* Converts a numeric literal into the correct representation
|
|
20
|
-
* for int64 / bigint boundaries and defaults.
|
|
21
|
-
*/
|
|
22
|
-
const lit = (n, forceCtor = false) => {
|
|
23
|
-
if (isBigInt || forceCtor)
|
|
15
|
+
const lit = (n) => {
|
|
16
|
+
if (isBigInt)
|
|
24
17
|
return `BigInt(${n})`;
|
|
25
18
|
if (isInt64)
|
|
26
19
|
return `${n}n`;
|
|
27
20
|
return `${n}`;
|
|
28
21
|
};
|
|
29
|
-
|
|
30
|
-
if (schema.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
22
|
+
// minimum
|
|
23
|
+
if (schema.minimum !== undefined || schema.exclusiveMinimum !== undefined) {
|
|
24
|
+
// > 0
|
|
25
|
+
// z.int().positive().safeParse(1) // { success: true }
|
|
26
|
+
// z.int().positive().safeParse(0) // { success: false }
|
|
27
|
+
if ((schema.minimum ?? schema.exclusiveMinimum) === 0 && schema.exclusiveMinimum === true) {
|
|
28
|
+
o.push('.positive()');
|
|
29
|
+
}
|
|
30
|
+
// >= 0
|
|
31
|
+
// z.int().nonnegative().safeParse(0) // { success: true }
|
|
32
|
+
// z.int().nonnegative().safeParse(-1) // { success: false }
|
|
33
|
+
else if ((schema.minimum ?? schema.exclusiveMinimum) === 0 &&
|
|
34
|
+
schema.exclusiveMinimum === false) {
|
|
35
|
+
o.push('.nonnegative()');
|
|
36
|
+
}
|
|
37
|
+
// > value
|
|
38
|
+
// z.int().gt(100) // value > 100
|
|
39
|
+
// z.int().gt(100).safeParse(101) // { success: true }
|
|
40
|
+
// z.int().gt(100).safeParse(100) // { success: false }
|
|
41
|
+
else if ((schema.exclusiveMinimum === true || schema.minimum === undefined) &&
|
|
42
|
+
typeof (schema.minimum ?? schema.exclusiveMinimum) === 'number') {
|
|
43
|
+
o.push(`.gt(${lit((schema.minimum ?? schema.exclusiveMinimum))})`);
|
|
44
|
+
}
|
|
45
|
+
// >= value
|
|
46
|
+
// z.int().min(100) // value >= 100
|
|
47
|
+
// z.int().min(100).safeParse(100) // { success: true }
|
|
48
|
+
// z.int().min(100).safeParse(99) // { success: false }
|
|
49
|
+
else if (typeof schema.minimum === 'number') {
|
|
50
|
+
o.push(`.min(${lit(schema.minimum)})`);
|
|
51
|
+
}
|
|
35
52
|
}
|
|
36
|
-
|
|
37
|
-
|
|
53
|
+
// maximum
|
|
54
|
+
if (schema.maximum !== undefined || schema.exclusiveMaximum !== undefined) {
|
|
55
|
+
// < 0
|
|
56
|
+
// z.int().negative().safeParse(-1) // { success: true }
|
|
57
|
+
// z.int().negative().safeParse(0) // { success: false }
|
|
58
|
+
if ((schema.maximum ?? schema.exclusiveMaximum) === 0 && schema.exclusiveMaximum === true) {
|
|
59
|
+
o.push('.negative()');
|
|
60
|
+
}
|
|
61
|
+
// <= 0
|
|
62
|
+
// z.int().nonpositive().safeParse(0) // { success: true }
|
|
63
|
+
// z.int().nonpositive().safeParse(1) // { success: false }
|
|
64
|
+
else if ((schema.maximum ?? schema.exclusiveMaximum) === 0 &&
|
|
65
|
+
schema.exclusiveMaximum === false) {
|
|
66
|
+
o.push('.nonpositive()');
|
|
67
|
+
}
|
|
68
|
+
// < value
|
|
69
|
+
// z.int().lt(100) // value < 100
|
|
70
|
+
// z.int().lt(100).safeParse(99) -> { success: true }
|
|
71
|
+
// z.int().lt(100).safeParse(100) -> { success: false }
|
|
72
|
+
else if ((schema.exclusiveMaximum === true || schema.maximum === undefined) &&
|
|
73
|
+
typeof (schema.maximum ?? schema.exclusiveMaximum) === 'number') {
|
|
74
|
+
o.push(`.lt(${lit((schema.maximum ?? schema.exclusiveMaximum))})`);
|
|
75
|
+
}
|
|
76
|
+
// <= value
|
|
77
|
+
// z.int().max(100) // value <= 100
|
|
78
|
+
// z.int().max(100).safeParse(100) -> { success: true }
|
|
79
|
+
// z.int().max(100).safeParse(101) -> { success: false }
|
|
80
|
+
else if (typeof schema.maximum === 'number') {
|
|
81
|
+
o.push(`.max(${lit(schema.maximum)})`);
|
|
82
|
+
}
|
|
38
83
|
}
|
|
39
|
-
|
|
40
|
-
|
|
84
|
+
// multipleOf
|
|
85
|
+
// z.int().multipleOf(2).safeParse(2) // { success: true }
|
|
86
|
+
// z.int().multipleOf(2).safeParse(1) // { success: false }
|
|
87
|
+
if (schema.multipleOf !== undefined && typeof schema.multipleOf === 'number') {
|
|
88
|
+
o.push(`.multipleOf(${lit(schema.multipleOf)})`);
|
|
41
89
|
}
|
|
42
|
-
|
|
43
|
-
if (typeof schema.
|
|
44
|
-
|
|
90
|
+
// default (always last)
|
|
91
|
+
if (schema.default !== undefined && typeof schema.default === 'number') {
|
|
92
|
+
o.push(`.default(${lit(schema.default)})`);
|
|
45
93
|
}
|
|
46
|
-
|
|
47
|
-
out.push('.negative()');
|
|
48
|
-
}
|
|
49
|
-
else if (typeof schema.maximum === 'number') {
|
|
50
|
-
out.push(`.max(${lit(schema.maximum)})`);
|
|
51
|
-
}
|
|
52
|
-
/* ─────────────── 6. default (always last) ─────────────── */
|
|
53
|
-
if (schema.default !== undefined) {
|
|
54
|
-
const def = typeof schema.default === 'number'
|
|
55
|
-
? lit(schema.default, /* forceCtor: BigInt as needed */ true)
|
|
56
|
-
: JSON.stringify(schema.default);
|
|
57
|
-
out.push(`.default(${def})`);
|
|
58
|
-
}
|
|
59
|
-
/* ─────────────── 7. finish ─────────────── */
|
|
60
|
-
return out.join('');
|
|
94
|
+
return o.join('');
|
|
61
95
|
}
|
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
import type { Schema } from '../../../openapi/
|
|
1
|
+
import type { Schema } from '../../../openapi/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Generates a Zod schema for number types based on OpenAPI schema.
|
|
4
|
+
* Supports float, float32, float64, and number formats.
|
|
5
|
+
*
|
|
6
|
+
* @param schema - The OpenAPI schema object
|
|
7
|
+
* @returns The Zod schema string
|
|
8
|
+
*/
|
|
2
9
|
export declare function number(schema: Schema): string;
|
|
@@ -1,53 +1,85 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generates a Zod schema for number types based on OpenAPI schema.
|
|
3
|
+
* Supports float, float32, float64, and number formats.
|
|
4
|
+
*
|
|
5
|
+
* @param schema - The OpenAPI schema object
|
|
6
|
+
* @returns The Zod schema string
|
|
7
|
+
*/
|
|
2
8
|
export function number(schema) {
|
|
3
|
-
const
|
|
9
|
+
const o = [
|
|
4
10
|
schema.format === 'float' || schema.format === 'float32'
|
|
5
11
|
? 'z.float32()'
|
|
6
12
|
: schema.format === 'float64'
|
|
7
13
|
? 'z.float64()'
|
|
8
14
|
: 'z.number()',
|
|
9
15
|
];
|
|
10
|
-
|
|
11
|
-
parts.push(regex(schema.pattern));
|
|
12
|
-
if (schema.minLength !== undefined)
|
|
13
|
-
parts.push(min(schema.minLength));
|
|
14
|
-
if (schema.maxLength !== undefined)
|
|
15
|
-
parts.push(max(schema.maxLength));
|
|
16
|
-
/* -------- lower bound -------- */
|
|
16
|
+
// minimum
|
|
17
17
|
if (schema.minimum !== undefined) {
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
// > 0
|
|
19
|
+
// z.number().positive().safeParse(1) // { success: true }
|
|
20
|
+
// z.number().positive().safeParse(0) // { success: false }
|
|
21
|
+
if (schema.minimum === 0 && schema.exclusiveMinimum === true) {
|
|
22
|
+
o.push('.positive()');
|
|
20
23
|
}
|
|
24
|
+
// >= 0
|
|
25
|
+
// z.number().nonnegative().safeParse(0) // { success: true }
|
|
26
|
+
// z.number().nonnegative().safeParse(-1) // { success: false }
|
|
27
|
+
else if (schema.minimum === 0 && schema.exclusiveMinimum === false) {
|
|
28
|
+
o.push('.nonnegative()');
|
|
29
|
+
}
|
|
30
|
+
// > value
|
|
31
|
+
// z.number().gt(100) // value > 100
|
|
21
32
|
else if (schema.exclusiveMinimum === true) {
|
|
22
|
-
|
|
33
|
+
o.push(`.gt(${schema.minimum})`);
|
|
23
34
|
}
|
|
35
|
+
// >= value
|
|
36
|
+
// z.number().min(100) // value >= 100
|
|
24
37
|
else {
|
|
25
|
-
|
|
38
|
+
o.push(`.min(${schema.minimum})`);
|
|
26
39
|
}
|
|
27
40
|
}
|
|
28
41
|
else if (typeof schema.exclusiveMinimum === 'number') {
|
|
29
|
-
//
|
|
30
|
-
|
|
42
|
+
// > value (no minimum)
|
|
43
|
+
o.push(`.gt(${schema.exclusiveMinimum})`);
|
|
31
44
|
}
|
|
32
|
-
|
|
45
|
+
// maximum
|
|
33
46
|
if (schema.maximum !== undefined) {
|
|
34
|
-
|
|
35
|
-
|
|
47
|
+
// < 0
|
|
48
|
+
// z.number().negative().safeParse(-1) // { success: true }
|
|
49
|
+
// z.number().negative().safeParse(0) // { success: false }
|
|
50
|
+
if (schema.maximum === 0 && schema.exclusiveMaximum === true) {
|
|
51
|
+
o.push('.negative()');
|
|
52
|
+
}
|
|
53
|
+
// <= 0
|
|
54
|
+
// z.number().nonpositive().safeParse(0) // { success: true }
|
|
55
|
+
// z.number().nonpositive().safeParse(1) // { success: false }
|
|
56
|
+
else if (schema.maximum === 0 && schema.exclusiveMaximum === false) {
|
|
57
|
+
o.push('.nonpositive()');
|
|
36
58
|
}
|
|
59
|
+
// < value
|
|
60
|
+
// z.number().lt(100) // value < 100
|
|
37
61
|
else if (schema.exclusiveMaximum === true) {
|
|
38
|
-
|
|
62
|
+
o.push(`.lt(${schema.maximum})`);
|
|
39
63
|
}
|
|
64
|
+
// <= value
|
|
65
|
+
// z.number().max(100) // value <= 100
|
|
40
66
|
else {
|
|
41
|
-
|
|
67
|
+
o.push(`.max(${schema.maximum})`);
|
|
42
68
|
}
|
|
43
69
|
}
|
|
44
70
|
else if (typeof schema.exclusiveMaximum === 'number') {
|
|
45
|
-
//
|
|
46
|
-
|
|
71
|
+
// < value (no maximum)
|
|
72
|
+
o.push(`.lt(${schema.exclusiveMaximum})`);
|
|
73
|
+
}
|
|
74
|
+
// multipleOf
|
|
75
|
+
// z.number().multipleOf(2).safeParse(2) // { success: true }
|
|
76
|
+
// z.number().multipleOf(2).safeParse(1) // { success: false }
|
|
77
|
+
if (schema.multipleOf !== undefined) {
|
|
78
|
+
o.push(`.multipleOf(${schema.multipleOf})`);
|
|
47
79
|
}
|
|
48
|
-
|
|
80
|
+
// default (always last)
|
|
49
81
|
if (schema.default !== undefined) {
|
|
50
|
-
|
|
82
|
+
o.push(`.default(${JSON.stringify(schema.default)})`);
|
|
51
83
|
}
|
|
52
|
-
return
|
|
84
|
+
return o.join('');
|
|
53
85
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { allOf } from '../../../helper/allof.js';
|
|
2
2
|
import { anyOf } from '../../../helper/anyof.js';
|
|
3
3
|
import { oneOf } from '../../../helper/oneof.js';
|
|
4
|
+
import { zodToOpenAPI } from '../../../helper/zod-to-openapi.js';
|
|
4
5
|
import { propertiesSchema } from '../helper/properties-schema.js';
|
|
5
|
-
import {
|
|
6
|
+
import { zod } from '../index.js';
|
|
6
7
|
/**
|
|
7
8
|
* Generates a Zod object schema from an OpenAPI schema definition.
|
|
8
9
|
*
|
|
@@ -20,7 +21,7 @@ export function object(schema) {
|
|
|
20
21
|
}
|
|
21
22
|
return 'z.any()';
|
|
22
23
|
}
|
|
23
|
-
return record(schema.additionalProperties)
|
|
24
|
+
return `z.record(z.string(),${zodToOpenAPI(zod(schema.additionalProperties), schema.additionalProperties)})`;
|
|
24
25
|
}
|
|
25
26
|
if (schema.properties) {
|
|
26
27
|
const zodSchema = propertiesSchema(schema.properties, Array.isArray(schema.required) ? schema.required : []);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { regex } from '../../../utils/index.js';
|
|
2
2
|
const FORMAT_STRING = {
|
|
3
3
|
email: '.email()',
|
|
4
4
|
uuid: '.uuid()',
|
|
@@ -30,26 +30,24 @@ const FORMAT_STRING = {
|
|
|
30
30
|
/** Build a Zod string schema from an OpenAPI string schema. */
|
|
31
31
|
export function string(schema) {
|
|
32
32
|
const parts = [];
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
parts.push(fmt ? `z${fmt}` : 'z.string()');
|
|
36
|
-
/* ---------- pattern ---------- */
|
|
33
|
+
const format = schema.format && FORMAT_STRING[schema.format];
|
|
34
|
+
parts.push(format ? `z${format}` : 'z.string()');
|
|
37
35
|
if (schema.pattern)
|
|
38
36
|
parts.push(regex(schema.pattern));
|
|
39
|
-
/* ---------- length constraints ---------- */
|
|
40
|
-
const { minLength, maxLength } = schema;
|
|
41
37
|
// special-case: equal min & max → .length()
|
|
42
|
-
if (minLength !== undefined &&
|
|
43
|
-
|
|
38
|
+
if (schema.minLength !== undefined &&
|
|
39
|
+
schema.maxLength !== undefined &&
|
|
40
|
+
schema.minLength === schema.maxLength) {
|
|
41
|
+
parts.push(`.length(${schema.minLength})`);
|
|
44
42
|
}
|
|
45
43
|
else {
|
|
46
|
-
if (minLength !== undefined)
|
|
47
|
-
parts.push(min(minLength));
|
|
48
|
-
if (maxLength !== undefined)
|
|
49
|
-
parts.push(max(maxLength));
|
|
44
|
+
if (schema.minLength !== undefined)
|
|
45
|
+
parts.push(`.min(${schema.minLength})`);
|
|
46
|
+
if (schema.maxLength !== undefined)
|
|
47
|
+
parts.push(`.max(${schema.maxLength})`);
|
|
50
48
|
}
|
|
51
|
-
/*
|
|
49
|
+
/* default (always last)*/
|
|
52
50
|
if (schema.default !== undefined)
|
|
53
|
-
parts.push(
|
|
51
|
+
parts.push(`.default(${JSON.stringify(schema.default)})`);
|
|
54
52
|
return parts.join('');
|
|
55
53
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { Parameters } from '../../../../../openapi/
|
|
1
|
+
import type { Parameters } from '../../../../../openapi/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Generates a Zod schema string for a query parameter, with coercion applied based on its type.
|
|
4
4
|
*
|
|
5
|
-
* @param
|
|
5
|
+
* @param schema - The base Zod schema string.
|
|
6
6
|
* @param param - The OpenAPI parameter object.
|
|
7
7
|
* @returns The transformed Zod schema string.
|
|
8
8
|
*
|
|
@@ -11,4 +11,4 @@ import type { Parameters } from '../../../../../openapi/types.js';
|
|
|
11
11
|
* - Applies `stringbool` to booleans to support string-based boolean input.
|
|
12
12
|
* - Returns the base schema unchanged for other types or locations.
|
|
13
13
|
*/
|
|
14
|
-
export declare function queryParameter(
|
|
14
|
+
export declare function queryParameter(schema: string, param: Parameters): string;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { coerce, stringbool } from '../../../../../utils/index.js';
|
|
2
1
|
/**
|
|
3
2
|
* Generates a Zod schema string for a query parameter, with coercion applied based on its type.
|
|
4
3
|
*
|
|
5
|
-
* @param
|
|
4
|
+
* @param schema - The base Zod schema string.
|
|
6
5
|
* @param param - The OpenAPI parameter object.
|
|
7
6
|
* @returns The transformed Zod schema string.
|
|
8
7
|
*
|
|
@@ -11,15 +10,15 @@ import { coerce, stringbool } from '../../../../../utils/index.js';
|
|
|
11
10
|
* - Applies `stringbool` to booleans to support string-based boolean input.
|
|
12
11
|
* - Returns the base schema unchanged for other types or locations.
|
|
13
12
|
*/
|
|
14
|
-
export function queryParameter(
|
|
13
|
+
export function queryParameter(schema, param) {
|
|
15
14
|
if (param.in === 'query' && param.schema.type === 'number') {
|
|
16
|
-
return coerce(
|
|
15
|
+
return `z.coerce.${schema.replace('z.', '')}`;
|
|
17
16
|
}
|
|
18
17
|
if (param.in === 'query' && param.schema.type === 'boolean') {
|
|
19
|
-
return stringbool
|
|
18
|
+
return schema.replace('boolean', 'stringbool');
|
|
20
19
|
}
|
|
21
20
|
if (param.in === 'query' && param.schema.type === 'date') {
|
|
22
|
-
return coerce(
|
|
21
|
+
return `z.coerce.${schema.replace('z.', '')}`;
|
|
23
22
|
}
|
|
24
|
-
return
|
|
23
|
+
return schema;
|
|
25
24
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { schema } from '../../../../../utils/index.js';
|
|
2
1
|
/**
|
|
3
2
|
* Generates an array of Zod validator strings from OpenAPI parameter objects.
|
|
4
3
|
*
|
|
@@ -20,7 +19,9 @@ export function requestParamsArray(paramsObj) {
|
|
|
20
19
|
const obj = paramsObj[section];
|
|
21
20
|
// 2.1 process only if object is not empty
|
|
22
21
|
if (Object.keys(obj).length) {
|
|
23
|
-
const s =
|
|
22
|
+
const s = `z.object({${Object.entries(obj)
|
|
23
|
+
.map(([key, val]) => `${key}:${val}`)
|
|
24
|
+
.join(',')}})`;
|
|
24
25
|
// path is params convention
|
|
25
26
|
if (section === 'path') {
|
|
26
27
|
return `params:${s}`;
|
|
@@ -4,7 +4,7 @@ import type { Content } from '../../../../../../openapi/index.js';
|
|
|
4
4
|
*
|
|
5
5
|
* @param required - Indicates if the request body is required.
|
|
6
6
|
* @param content - OpenAPI content object describing media types and schemas.
|
|
7
|
-
* @param
|
|
7
|
+
* @param schema - Zod schema string used for validation.
|
|
8
8
|
* @returns A formatted request body definition string, or an empty string if unsupported.
|
|
9
9
|
*/
|
|
10
|
-
export declare function requestBody(required: boolean, content: Content,
|
|
10
|
+
export declare function requestBody(required: boolean, content: Content, schema: string): string;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isUniqueContentSchema } from '../../../../../../utils/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Generates a request body configuration string for a route.
|
|
4
4
|
*
|
|
5
5
|
* @param required - Indicates if the request body is required.
|
|
6
6
|
* @param content - OpenAPI content object describing media types and schemas.
|
|
7
|
-
* @param
|
|
7
|
+
* @param schema - Zod schema string used for validation.
|
|
8
8
|
* @returns A formatted request body definition string, or an empty string if unsupported.
|
|
9
9
|
*/
|
|
10
|
-
export function requestBody(required, content,
|
|
10
|
+
export function requestBody(required, content, schema) {
|
|
11
11
|
const contentTypes = Object.keys(content);
|
|
12
12
|
if (contentTypes.length === 0)
|
|
13
13
|
return '';
|
|
@@ -18,11 +18,11 @@ export function requestBody(required, content, zodSchema) {
|
|
|
18
18
|
const contentParts = [];
|
|
19
19
|
for (const contentType of contentTypes) {
|
|
20
20
|
// z.date() → z.coerce.date()
|
|
21
|
-
if (
|
|
22
|
-
contentParts.push(`'${contentType}':{schema
|
|
21
|
+
if (schema.includes('z.date()')) {
|
|
22
|
+
contentParts.push(`'${contentType}':{schema:z.coerce.${schema.replace('z.', '')}}`);
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
25
|
-
contentParts.push(`'${contentType}':{schema:${
|
|
25
|
+
contentParts.push(`'${contentType}':{schema:${schema}}`);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
return `body:{required:${required},content:{${contentParts.join(',')}},},`;
|
package/dist/helper/allof.d.ts
CHANGED
package/dist/helper/allof.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { zodSchemaFromSubSchema } from '../generator/zod/helper/zod-schema-from-sub-schema.js';
|
|
2
|
-
import { intersection, isNullableSchema } from '../utils/index.js';
|
|
3
2
|
/**
|
|
4
3
|
* Converts an OpenAPI `allOf` schema into a Zod intersection schema.
|
|
5
4
|
*
|
|
@@ -18,7 +17,12 @@ export function allOf(schema) {
|
|
|
18
17
|
return 'z.any()';
|
|
19
18
|
}
|
|
20
19
|
const { nullable, schemas } = schema.allOf.reduce((acc, subSchema) => {
|
|
21
|
-
|
|
20
|
+
const isNullable = typeof subSchema === 'object' &&
|
|
21
|
+
subSchema !== null &&
|
|
22
|
+
'nullable' in subSchema &&
|
|
23
|
+
subSchema.nullable === true &&
|
|
24
|
+
Object.keys(subSchema).length === 1;
|
|
25
|
+
if (isNullable) {
|
|
22
26
|
acc.nullable = true;
|
|
23
27
|
return acc;
|
|
24
28
|
}
|
|
@@ -32,5 +36,5 @@ export function allOf(schema) {
|
|
|
32
36
|
if (schemas.length === 1) {
|
|
33
37
|
return nullable ? `${schemas[0]}.nullable()` : schemas[0];
|
|
34
38
|
}
|
|
35
|
-
return
|
|
39
|
+
return `z.intersection(${schemas.join(',')})${nullable ? '.nullable()' : ''}`;
|
|
36
40
|
}
|
package/dist/helper/anyof.d.ts
CHANGED