hono-takibi 0.4.3 → 0.4.4
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/generators/zod/generate-zod-enum.js +12 -0
- package/dist/generators/zod/generate-zod-number-schema.d.ts +2 -0
- package/dist/generators/zod/generate-zod-number-schema.js +30 -3
- package/dist/generators/zod/generate-zod-string-schema.d.ts +1 -0
- package/dist/generators/zod/generate-zod-string-schema.js +3 -0
- package/dist/generators/zod/schema/generate-zod-schema.js +36 -2
- package/dist/types/index.d.ts +4 -0
- package/package.json +1 -1
|
@@ -7,5 +7,17 @@ function generateZodEnum(schema) {
|
|
|
7
7
|
const openapi_example = (0, generate_zod_to_openapi_1.generateZodToOpenAPI)(schema.example);
|
|
8
8
|
return `z.enum(${JSON.stringify(schema.enum)})${openapi_example}`;
|
|
9
9
|
}
|
|
10
|
+
// number
|
|
11
|
+
if (schema.type === 'number' && schema.enum) {
|
|
12
|
+
return `z.literal(${schema.enum})`;
|
|
13
|
+
}
|
|
14
|
+
// bigint
|
|
15
|
+
if (schema.type === 'bigint' && schema.enum) {
|
|
16
|
+
return `z.literal(${schema.enum}n)`;
|
|
17
|
+
}
|
|
18
|
+
// boolean
|
|
19
|
+
if (schema.type === 'boolean' && schema.enum) {
|
|
20
|
+
return `z.literal(${schema.enum})`;
|
|
21
|
+
}
|
|
10
22
|
return `z.enum(${JSON.stringify(schema.enum)})`;
|
|
11
23
|
}
|
|
@@ -24,12 +24,11 @@ function generateZodNumberSchema(args) {
|
|
|
24
24
|
// maxLength
|
|
25
25
|
if (args.maxLength)
|
|
26
26
|
validations.push((0, generate_zod_max_1.generateZodMax)(args.maxLength));
|
|
27
|
-
// 0 falsy value
|
|
28
27
|
// minimum
|
|
29
|
-
if (
|
|
28
|
+
if (args.minimum)
|
|
30
29
|
validations.push((0, generate_zod_min_1.generateZodMin)(args.minimum));
|
|
31
30
|
// maximum
|
|
32
|
-
if (
|
|
31
|
+
if (args.maximum)
|
|
33
32
|
validations.push((0, generate_zod_max_1.generateZodMax)(args.maximum));
|
|
34
33
|
// default
|
|
35
34
|
if (args.default)
|
|
@@ -37,5 +36,33 @@ function generateZodNumberSchema(args) {
|
|
|
37
36
|
// example
|
|
38
37
|
if (args.example)
|
|
39
38
|
validations.push((0, generate_zod_to_openapi_1.generateZodToOpenAPI)(args.example, args.paramName, args.isPath));
|
|
39
|
+
// 0 falsy value
|
|
40
|
+
// minimum === 0
|
|
41
|
+
// positive
|
|
42
|
+
if (args.minimum === 0 && args.exclusiveMinimum) {
|
|
43
|
+
validations.push('.positive()');
|
|
44
|
+
}
|
|
45
|
+
// nonpositive
|
|
46
|
+
if (args.minimum === 0 && !args.exclusiveMinimum) {
|
|
47
|
+
validations.push('.nonpositive()');
|
|
48
|
+
}
|
|
49
|
+
// negative
|
|
50
|
+
if (args.maximum === 0 && args.exclusiveMaximum) {
|
|
51
|
+
validations.push('.negative()');
|
|
52
|
+
}
|
|
53
|
+
// gt
|
|
54
|
+
if (args.minimum) {
|
|
55
|
+
if (args.minimum > 0 && args.exclusiveMinimum) {
|
|
56
|
+
const res = `.gt(${args.minimum})`;
|
|
57
|
+
validations.push(res);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// lt
|
|
61
|
+
if (args.maximum) {
|
|
62
|
+
if (args.maximum > 0 && args.exclusiveMaximum) {
|
|
63
|
+
const res = `.lt(${args.maximum})`;
|
|
64
|
+
validations.push(res);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
40
67
|
return validations.join('');
|
|
41
68
|
}
|
|
@@ -61,6 +61,9 @@ function generateZodStringSchema(args) {
|
|
|
61
61
|
// default
|
|
62
62
|
if (args.default)
|
|
63
63
|
validations.push((0, generate_zod_default_1.generateZodDefault)(args.default));
|
|
64
|
+
// nullable
|
|
65
|
+
if (args.nullable)
|
|
66
|
+
validations.push('.nullable()');
|
|
64
67
|
// example
|
|
65
68
|
if (args.example)
|
|
66
69
|
validations.push((0, generate_zod_to_openapi_1.generateZodToOpenAPI)(args.example, args.paramName, args.isPath));
|
|
@@ -12,6 +12,8 @@ const generate_oneof_code_1 = require("../../zod-openapi-hono/openapi/component/
|
|
|
12
12
|
const get_variable_schema_name_helper_1 = require("../../../core/helper/get-variable-schema-name-helper");
|
|
13
13
|
const generate_zod_object_1 = require("../generate-zod-object");
|
|
14
14
|
const generate_zod_enum_1 = require("../generate-zod-enum");
|
|
15
|
+
const generate_zod_max_1 = require("../generate-zod-max");
|
|
16
|
+
const generate_zod_min_1 = require("../generate-zod-min");
|
|
15
17
|
/**
|
|
16
18
|
* Mapping of OpenAPI/JSON Schema types to Zod schema strings
|
|
17
19
|
*
|
|
@@ -106,6 +108,7 @@ function generateZodSchema(config, schema, paramName, isPath) {
|
|
|
106
108
|
minLength: schema.minLength,
|
|
107
109
|
maxLength: schema.maxLength,
|
|
108
110
|
format: schema.format && (0, is_format_string_1.isFormatString)(schema.format) ? schema.format : undefined,
|
|
111
|
+
nullable: schema.nullable,
|
|
109
112
|
default: schema.default,
|
|
110
113
|
example: schema.example,
|
|
111
114
|
paramName,
|
|
@@ -114,7 +117,7 @@ function generateZodSchema(config, schema, paramName, isPath) {
|
|
|
114
117
|
}
|
|
115
118
|
// number
|
|
116
119
|
if (schema.type === 'number') {
|
|
117
|
-
|
|
120
|
+
const res = (0, generate_zod_number_schema_1.generateZodNumberSchema)({
|
|
118
121
|
pattern: schema.pattern,
|
|
119
122
|
minLength: schema.minLength,
|
|
120
123
|
maxLength: schema.maxLength,
|
|
@@ -124,7 +127,18 @@ function generateZodSchema(config, schema, paramName, isPath) {
|
|
|
124
127
|
example: schema.example,
|
|
125
128
|
paramName,
|
|
126
129
|
isPath,
|
|
130
|
+
exclusiveMinimum: schema.exclusiveMinimum,
|
|
131
|
+
exclusiveMaximum: schema.exclusiveMaximum,
|
|
127
132
|
});
|
|
133
|
+
// gt
|
|
134
|
+
if (res.includes(`min(${schema.minimum})`) && res.includes(`gt(${schema.minimum})`)) {
|
|
135
|
+
return res.replace(`.min(${schema.minimum})`, '');
|
|
136
|
+
}
|
|
137
|
+
// lt
|
|
138
|
+
if (res.includes(`max(${schema.maximum})`) && res.includes(`lt(${schema.maximum})`)) {
|
|
139
|
+
return res.replace(`.max(${schema.maximum})`, '');
|
|
140
|
+
}
|
|
141
|
+
return res;
|
|
128
142
|
}
|
|
129
143
|
// integer
|
|
130
144
|
if (schema.type === 'integer') {
|
|
@@ -140,8 +154,28 @@ function generateZodSchema(config, schema, paramName, isPath) {
|
|
|
140
154
|
});
|
|
141
155
|
}
|
|
142
156
|
// array
|
|
143
|
-
if (schema.type === 'array' && schema.items)
|
|
157
|
+
if (schema.type === 'array' && schema.items) {
|
|
158
|
+
if (schema.minItems && schema.maxItems) {
|
|
159
|
+
const minItemsSchema = (0, generate_zod_min_1.generateZodMin)(schema.minItems);
|
|
160
|
+
const maxItemsSchema = (0, generate_zod_max_1.generateZodMax)(schema.maxItems);
|
|
161
|
+
const zodArray = (0, generate_zod_array_1.generateZodArray)(generateZodSchema(config, schema.items, undefined, undefined));
|
|
162
|
+
const res = `${zodArray}${minItemsSchema}${maxItemsSchema}`;
|
|
163
|
+
return res;
|
|
164
|
+
}
|
|
165
|
+
if (schema.minItems) {
|
|
166
|
+
const minItemsSchema = (0, generate_zod_min_1.generateZodMin)(schema.minItems);
|
|
167
|
+
const zodArray = (0, generate_zod_array_1.generateZodArray)(generateZodSchema(config, schema.items, undefined, undefined));
|
|
168
|
+
const res = `${zodArray}${minItemsSchema}`;
|
|
169
|
+
return res;
|
|
170
|
+
}
|
|
171
|
+
if (schema.maxItems) {
|
|
172
|
+
const maxItemsSchema = (0, generate_zod_max_1.generateZodMax)(schema.maxItems);
|
|
173
|
+
const zodArray = (0, generate_zod_array_1.generateZodArray)(generateZodSchema(config, schema.items, undefined, undefined));
|
|
174
|
+
const res = `${zodArray}${maxItemsSchema}`;
|
|
175
|
+
return res;
|
|
176
|
+
}
|
|
144
177
|
return (0, generate_zod_array_1.generateZodArray)(generateZodSchema(config, schema.items, undefined, undefined));
|
|
178
|
+
}
|
|
145
179
|
if (schema.allOf) {
|
|
146
180
|
return (0, generate_allof_code_1.generateAllOfCode)(schema, config);
|
|
147
181
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -129,6 +129,10 @@ export type Schema = {
|
|
|
129
129
|
maxLength?: number;
|
|
130
130
|
minimum?: number;
|
|
131
131
|
maximum?: number;
|
|
132
|
+
exclusiveMinimum?: boolean;
|
|
133
|
+
exclusiveMaximum?: boolean;
|
|
134
|
+
minItems?: number;
|
|
135
|
+
maxItems?: number;
|
|
132
136
|
default?: DefaultValue;
|
|
133
137
|
example?: ExampleValue;
|
|
134
138
|
properties?: Record<string, Schema>;
|