@orval/zod 8.24.0 → 8.26.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/index.d.mts +9 -15
- package/dist/index.mjs +123 -44
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ClientBuilder, ClientDependenciesBuilder, ClientGeneratorsBuilder, ContextSpec, GeneratorMutator, OpenApiParameterObject, OpenApiReferenceObject, OpenApiSchemaObject, PackageJson, ZodCoerceType, ZodVariantOption, ZodVersionOption } from "@orval/core";
|
|
2
|
-
|
|
3
2
|
//#region src/compatible-v4.d.ts
|
|
4
3
|
declare const isZodVersionV4: (packageJson: PackageJson) => boolean;
|
|
5
4
|
/**
|
|
@@ -13,10 +12,7 @@ declare const isZodVersionV4: (packageJson: PackageJson) => boolean;
|
|
|
13
12
|
* target is pinned while preserving package-detection for `'auto'`.
|
|
14
13
|
*/
|
|
15
14
|
declare const resolveIsZodV4: (version: ZodVersionOption | undefined, packageJson: PackageJson | undefined) => boolean;
|
|
16
|
-
declare const assertZodTarget: ({
|
|
17
|
-
variant,
|
|
18
|
-
isZodV4
|
|
19
|
-
}: {
|
|
15
|
+
declare const assertZodTarget: ({ variant, isZodV4 }: {
|
|
20
16
|
variant: ZodVariantOption | undefined;
|
|
21
17
|
isZodV4: boolean;
|
|
22
18
|
}) => void;
|
|
@@ -104,6 +100,12 @@ interface ZodParamsContext {
|
|
|
104
100
|
interface ZodParamsInjection extends Pick<ZodParamsContext, 'operationId' | 'location' | 'schemaName'> {
|
|
105
101
|
mutator: GeneratorMutator;
|
|
106
102
|
}
|
|
103
|
+
interface ZodMetaArgs {
|
|
104
|
+
id?: string;
|
|
105
|
+
title?: string;
|
|
106
|
+
description?: string;
|
|
107
|
+
deprecated?: boolean;
|
|
108
|
+
}
|
|
107
109
|
declare const parseZodValidationSchemaDefinition: (input: ZodValidationSchemaDefinition, context: ContextSpec, coerceTypes: boolean | ZodCoerceType[] | undefined, strict: boolean, isZodV4: boolean, preprocess?: GeneratorMutator, paramsInjection?: ZodParamsInjection, variant?: ZodVariantOption, exactOptional?: boolean) => {
|
|
108
110
|
zod: string;
|
|
109
111
|
consts: string;
|
|
@@ -132,15 +134,7 @@ declare const dereference: (schema: OpenApiSchemaObject | OpenApiReferenceObject
|
|
|
132
134
|
declare const generateFormDataZodSchema: (schema: OpenApiSchemaObject, context: ContextSpec, name: string, strict: boolean, isZodV4: boolean, encoding?: Record<string, {
|
|
133
135
|
contentType?: string;
|
|
134
136
|
}>, useReusableSchemas?: boolean) => ZodValidationSchemaDefinition;
|
|
135
|
-
declare const parseParameters: ({
|
|
136
|
-
data,
|
|
137
|
-
context,
|
|
138
|
-
operationName,
|
|
139
|
-
isZodV4,
|
|
140
|
-
strict,
|
|
141
|
-
generate,
|
|
142
|
-
useReusableSchemas
|
|
143
|
-
}: {
|
|
137
|
+
declare const parseParameters: ({ data, context, operationName, isZodV4, strict, generate, useReusableSchemas }: {
|
|
144
138
|
data: (OpenApiParameterObject | OpenApiReferenceObject)[] | undefined;
|
|
145
139
|
context: ContextSpec;
|
|
146
140
|
operationName: string;
|
|
@@ -168,5 +162,5 @@ declare const parseParameters: ({
|
|
|
168
162
|
declare const generateZod: ClientBuilder;
|
|
169
163
|
declare const builder: () => () => ClientGeneratorsBuilder;
|
|
170
164
|
//#endregion
|
|
171
|
-
export { ZodParamsContext, ZodParamsInjection, ZodValidationSchemaDefinition, assertZodTarget, builder, builder as default, dereference, generateFormDataZodSchema, generateZod, generateZodValidationSchemaDefinition, getZodDependencies, getZodImportSource, getZodTypeName, isZodVersionV4, parseParameters, parseZodValidationSchemaDefinition, predefinedZodFormats, resolveIsZodV4 };
|
|
165
|
+
export { ZodMetaArgs, ZodParamsContext, ZodParamsInjection, ZodValidationSchemaDefinition, assertZodTarget, builder, builder as default, dereference, generateFormDataZodSchema, generateZod, generateZodValidationSchemaDefinition, getZodDependencies, getZodImportSource, getZodTypeName, isZodVersionV4, parseParameters, parseZodValidationSchemaDefinition, predefinedZodFormats, resolveIsZodV4 };
|
|
172
166
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { buildDynamicScope, buildInlineDynamicScope, camel, compareVersions, generateMutator, getDynamicAnchorName, getFormDataFieldFileType, getNumberWord, getRefInfo, isBoolean, isDynamicReference, isNumber, isObject, isString, jsStringEscape, jsStringLiteralEscape, logVerbose, pascal, resolveDynamicRef, resolveRef, stringify } from "@orval/core";
|
|
1
|
+
import { EnumGeneration, buildDynamicScope, buildInlineDynamicScope, camel, compareVersions, generateMutator, getDynamicAnchorName, getEnumImplementation, getEnumMembers, getEnumValueInfo, getFormDataFieldFileType, getNumberWord, getRefInfo, getRequiredKeys, hasEnumMetadata, isBoolean, isDynamicReference, isNumber, isObject, isString, jsStringEscape, jsStringLiteralEscape, logVerbose, pascal, resolveDynamicRef, resolveRef, stringify } from "@orval/core";
|
|
2
2
|
import jsesc from "jsesc";
|
|
3
3
|
import { unique } from "remeda";
|
|
4
4
|
//#region src/compatible-v4.ts
|
|
@@ -73,7 +73,7 @@ const getZodDependencies = (_hasGlobalMutator, _hasParamsSerializerOptions, _pac
|
|
|
73
73
|
/**
|
|
74
74
|
* values that may appear in "type". Equals SchemaObjectType
|
|
75
75
|
*/
|
|
76
|
-
const possibleSchemaTypes = new Set([
|
|
76
|
+
const possibleSchemaTypes = /* @__PURE__ */ new Set([
|
|
77
77
|
"integer",
|
|
78
78
|
"number",
|
|
79
79
|
"string",
|
|
@@ -83,7 +83,7 @@ const possibleSchemaTypes = new Set([
|
|
|
83
83
|
"null",
|
|
84
84
|
"array"
|
|
85
85
|
]);
|
|
86
|
-
const predefinedZodFormats = new Set([
|
|
86
|
+
const predefinedZodFormats = /* @__PURE__ */ new Set([
|
|
87
87
|
"date",
|
|
88
88
|
"time",
|
|
89
89
|
"date-time",
|
|
@@ -112,7 +112,7 @@ const resolveZodType = (schema) => {
|
|
|
112
112
|
}
|
|
113
113
|
return type ?? "unknown";
|
|
114
114
|
};
|
|
115
|
-
const COERCIBLE_TYPES = new Set([
|
|
115
|
+
const COERCIBLE_TYPES = /* @__PURE__ */ new Set([
|
|
116
116
|
"string",
|
|
117
117
|
"number",
|
|
118
118
|
"boolean",
|
|
@@ -122,6 +122,12 @@ const COERCIBLE_TYPES = new Set([
|
|
|
122
122
|
const PURE_COMMENT = "/*#__PURE__*/ ";
|
|
123
123
|
const zodMiniCall = (fn, args = "") => `${PURE_COMMENT}zod.${fn}(${args})`;
|
|
124
124
|
const zodMiniCoerceCall = (fn, args = "") => `${PURE_COMMENT}zod.coerce.${fn}(${args})`;
|
|
125
|
+
const hasUnicodePropertyEscape = (pattern) => /(^|[^\\])(?:\\\\)*\\[pP]\{/.test(pattern);
|
|
126
|
+
const buildRegExpLiteral = (pattern) => {
|
|
127
|
+
const innerPattern = pattern.slice(pattern.startsWith("/") ? 1 : 0, pattern.endsWith("/") ? -1 : void 0);
|
|
128
|
+
const unicodeFlag = hasUnicodePropertyEscape(innerPattern) ? ", 'u'" : "";
|
|
129
|
+
return `new RegExp('${jsStringLiteralEscape(innerPattern)}'${unicodeFlag})`;
|
|
130
|
+
};
|
|
125
131
|
/** Escapes string defaults for safe embedding in template literals. */
|
|
126
132
|
function formatDefaultValue(value) {
|
|
127
133
|
if (isString(value)) return jsesc(value, {
|
|
@@ -134,7 +140,7 @@ function formatDefaultValue(value) {
|
|
|
134
140
|
}) : formatDefaultValue(item)).join(", ")}]`;
|
|
135
141
|
return stringify(value) ?? "null";
|
|
136
142
|
}
|
|
137
|
-
const minAndMaxTypes = new Set([
|
|
143
|
+
const minAndMaxTypes = /* @__PURE__ */ new Set([
|
|
138
144
|
"number",
|
|
139
145
|
"integer",
|
|
140
146
|
"string",
|
|
@@ -178,7 +184,7 @@ const isPlainObjectSchema = (schema) => {
|
|
|
178
184
|
if (!schema || schema.oneOf || schema.anyOf || schema.allOf) return false;
|
|
179
185
|
return schema.type === "object" || isObject(schema.properties) && Object.keys(schema.properties).length > 0;
|
|
180
186
|
};
|
|
181
|
-
const SHAPELESS_MEMBER_KEYS = new Set([
|
|
187
|
+
const SHAPELESS_MEMBER_KEYS = /* @__PURE__ */ new Set([
|
|
182
188
|
"required",
|
|
183
189
|
"title",
|
|
184
190
|
"description",
|
|
@@ -190,6 +196,14 @@ const isConstraintOnlyMember = (member) => {
|
|
|
190
196
|
if (!Array.isArray(schema.required) || schema.required.length === 0) return false;
|
|
191
197
|
return Object.keys(schema).every((key) => SHAPELESS_MEMBER_KEYS.has(key));
|
|
192
198
|
};
|
|
199
|
+
const getForbiddenKeys = (member) => {
|
|
200
|
+
if (!isObject(member.not)) return void 0;
|
|
201
|
+
const not = member.not;
|
|
202
|
+
const branches = Array.isArray(not.anyOf) ? not.anyOf : Array.isArray(not.required) ? [not] : [];
|
|
203
|
+
const forbidden = /* @__PURE__ */ new Set();
|
|
204
|
+
for (const branch of branches) if (isObject(branch) && Array.isArray(branch.required)) for (const key of branch.required) forbidden.add(key);
|
|
205
|
+
return forbidden.size > 0 ? [...forbidden] : void 0;
|
|
206
|
+
};
|
|
193
207
|
const hasLiteralDiscriminator = (schema, property) => {
|
|
194
208
|
if (!schema || !isObject(schema.properties)) return false;
|
|
195
209
|
const propertySchema = schema.properties[property];
|
|
@@ -237,7 +251,7 @@ const generateZodValidationSchemaDefinition = (schema, context, name, strict, is
|
|
|
237
251
|
functions: [],
|
|
238
252
|
consts: []
|
|
239
253
|
};
|
|
240
|
-
const CHAINABLE_SIBLINGS = new Set([
|
|
254
|
+
const CHAINABLE_SIBLINGS = /* @__PURE__ */ new Set([
|
|
241
255
|
"nullable",
|
|
242
256
|
"default",
|
|
243
257
|
"description"
|
|
@@ -313,11 +327,19 @@ const generateZodValidationSchemaDefinition = (schema, context, name, strict, is
|
|
|
313
327
|
const pushDescriptionOrMeta = () => {
|
|
314
328
|
const description = typeof schema.description === "string" && schema.description.length > 0 ? schema.description : void 0;
|
|
315
329
|
const deprecated = "deprecated" in schema && schema.deprecated === true ? true : void 0;
|
|
330
|
+
const title = typeof schema.title === "string" && schema.title.length > 0 ? schema.title : void 0;
|
|
331
|
+
const isAnnotatedConst = schema.const !== void 0 && (title !== void 0 || deprecated === true);
|
|
316
332
|
if (rules?.emitMeta && isZodV4) {
|
|
317
333
|
const meta = { id: name };
|
|
318
334
|
if (description !== void 0) meta.description = description;
|
|
319
335
|
if (deprecated) meta.deprecated = true;
|
|
320
336
|
functions.push(["meta", meta]);
|
|
337
|
+
} else if (isZodV4 && isAnnotatedConst) {
|
|
338
|
+
const meta = {};
|
|
339
|
+
if (title !== void 0) meta.title = title;
|
|
340
|
+
if (description !== void 0) meta.description = description;
|
|
341
|
+
if (deprecated) meta.deprecated = true;
|
|
342
|
+
functions.push(["meta", meta]);
|
|
321
343
|
} else if (description !== void 0) functions.push(["describe", `'${jsStringEscape(description)}'`]);
|
|
322
344
|
};
|
|
323
345
|
const type = resolveZodType(schema);
|
|
@@ -354,25 +376,41 @@ const generateZodValidationSchemaDefinition = (schema, context, name, strict, is
|
|
|
354
376
|
return property;
|
|
355
377
|
})();
|
|
356
378
|
const unionSeparator = discriminatorProperty ? encodeDiscriminatorSeparator(separator, discriminatorProperty) : separator;
|
|
357
|
-
const allOfRequired = schema.allOf ? [
|
|
358
|
-
const
|
|
359
|
-
|
|
379
|
+
const allOfRequired = schema.allOf ? [.../* @__PURE__ */ new Set([...getRequiredKeys(schema, name), ...schemas.flatMap((member, index) => {
|
|
380
|
+
const isRef = "$ref" in member && typeof member.$ref === "string";
|
|
381
|
+
const resolved = isRef ? tryResolveRefSchema(member.$ref, context) : member;
|
|
382
|
+
if (!resolved) return [];
|
|
383
|
+
return getRequiredKeys(resolved, isRef ? member.$ref : `${name}.allOf[${index}]`);
|
|
360
384
|
})])] : void 0;
|
|
361
385
|
const withSiblingProperties = (member) => {
|
|
362
386
|
const properties = schema.properties;
|
|
363
387
|
if (!(schema.oneOf || schema.anyOf) || !isObject(properties) || Object.keys(properties).length === 0 || !isConstraintOnlyMember(member)) return member;
|
|
364
388
|
const required = member.required;
|
|
365
389
|
if (!required.every((key) => Object.hasOwn(properties, key))) return member;
|
|
390
|
+
const forbidden = getForbiddenKeys(member);
|
|
391
|
+
let propertyOverrides;
|
|
392
|
+
if (forbidden) {
|
|
393
|
+
propertyOverrides = {};
|
|
394
|
+
for (const key of forbidden) {
|
|
395
|
+
const isForbiddenAndRequired = required.includes(key);
|
|
396
|
+
propertyOverrides[key] = {
|
|
397
|
+
functions: [["never", void 0], ...isForbiddenAndRequired ? [] : [["optional", void 0]]],
|
|
398
|
+
consts: []
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
}
|
|
366
402
|
return {
|
|
367
403
|
type: "object",
|
|
368
404
|
properties,
|
|
369
405
|
required,
|
|
370
|
-
description: member.description
|
|
406
|
+
description: member.description,
|
|
407
|
+
"x-orval-property-overrides": propertyOverrides
|
|
371
408
|
};
|
|
372
409
|
};
|
|
373
410
|
const baseSchemas = schemas.map((schema, index) => generateZodValidationSchemaDefinition(withSiblingProperties(schema), context, `${camel(name)}${pascal(getNumberWord(index + 1))}`, strict, isZodV4, {
|
|
374
411
|
required: true,
|
|
375
412
|
additionalRequired: allOfRequired,
|
|
413
|
+
propertyOverrides: withSiblingProperties(schema)["x-orval-property-overrides"],
|
|
376
414
|
constNameRegistry,
|
|
377
415
|
useReusableSchemas,
|
|
378
416
|
urlEncoded
|
|
@@ -507,9 +545,7 @@ const generateZodValidationSchemaDefinition = (schema, context, name, strict, is
|
|
|
507
545
|
if (!predefinedZodFormats.has(schema.format ?? "")) {
|
|
508
546
|
if ("const" in schema) functions.push(["literal", JSON.stringify(String(schema.const))]);
|
|
509
547
|
else if (schema.pattern && schema.format) {
|
|
510
|
-
const
|
|
511
|
-
const isEndWithSlash = schema.pattern.endsWith("/");
|
|
512
|
-
const regexp = `new RegExp('${jsStringLiteralEscape(schema.pattern.slice(isStartWithSlash ? 1 : 0, isEndWithSlash ? -1 : void 0))}')`;
|
|
548
|
+
const regexp = buildRegExpLiteral(schema.pattern);
|
|
513
549
|
consts.push(`export const ${name}RegExp${constsCounterValue} = ${regexp};\n`);
|
|
514
550
|
functions.push(["stringFormat", [`'${jsStringLiteralEscape(schema.format)}'`, `${name}RegExp${constsCounterValue}`]]);
|
|
515
551
|
} else functions.push([type, void 0]);
|
|
@@ -552,6 +588,10 @@ const generateZodValidationSchemaDefinition = (schema, context, name, strict, is
|
|
|
552
588
|
break;
|
|
553
589
|
}
|
|
554
590
|
break;
|
|
591
|
+
case "never":
|
|
592
|
+
functions.push(["never", void 0]);
|
|
593
|
+
if (required) functions.push(["optional", void 0]);
|
|
594
|
+
break;
|
|
555
595
|
default: {
|
|
556
596
|
if ("const" in schema) {
|
|
557
597
|
const constValue = schema.const;
|
|
@@ -567,7 +607,7 @@ const generateZodValidationSchemaDefinition = (schema, context, name, strict, is
|
|
|
567
607
|
const shouldUseLooseObject = type === "object" && !hasDefinedProperties && schema.additionalProperties === void 0 && !hasAdditionalPropertiesSchema;
|
|
568
608
|
if (hasProperties && hasDefinedProperties) {
|
|
569
609
|
const objectType = getObjectFunctionName(isZodV4, strict);
|
|
570
|
-
const requiredKeys = new Set([...schema
|
|
610
|
+
const requiredKeys = /* @__PURE__ */ new Set([...getRequiredKeys(schema, name), ...rules?.additionalRequired ?? []]);
|
|
571
611
|
functions.push([objectType, Object.keys(properties).map((key) => ({ [key]: rules?.propertyOverrides?.[key] ?? generateZodValidationSchemaDefinition(properties[key], context, camel(`${name}-${key}`), strict, isZodV4, {
|
|
572
612
|
required: requiredKeys.has(key),
|
|
573
613
|
constNameRegistry,
|
|
@@ -606,10 +646,12 @@ const generateZodValidationSchemaDefinition = (schema, context, name, strict, is
|
|
|
606
646
|
if (shouldUseExclusiveMin && exclusiveMin !== void 0) {
|
|
607
647
|
consts.push(`export const ${name}ExclusiveMin${constsCounterValue} = ${exclusiveMin};`);
|
|
608
648
|
functions.push(["gt", `${name}ExclusiveMin${constsCounterValue}`]);
|
|
609
|
-
} else if (min !== void 0)
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
649
|
+
} else if (min !== void 0) {
|
|
650
|
+
if (min === 1) functions.push(["min", `${min}`]);
|
|
651
|
+
else {
|
|
652
|
+
consts.push(`export const ${name}Min${constsCounterValue} = ${min};`);
|
|
653
|
+
functions.push(["min", `${name}Min${constsCounterValue}`]);
|
|
654
|
+
}
|
|
613
655
|
}
|
|
614
656
|
if (shouldUseExclusiveMax && exclusiveMax !== void 0) {
|
|
615
657
|
consts.push(`export const ${name}ExclusiveMax${constsCounterValue} = ${exclusiveMax};`);
|
|
@@ -626,18 +668,25 @@ const generateZodValidationSchemaDefinition = (schema, context, name, strict, is
|
|
|
626
668
|
}
|
|
627
669
|
const stringFormatAlreadyEmitted = isZodV4 && type === "string" && !!matches && !!schema.format && !predefinedZodFormats.has(schema.format ?? "");
|
|
628
670
|
if (matches && !hasNonArrayEnum && type === "string" && !stringFormatAlreadyEmitted) {
|
|
629
|
-
const
|
|
630
|
-
const isEndWithSlash = matches.endsWith("/");
|
|
631
|
-
const regexp = `new RegExp('${jsStringLiteralEscape(matches.slice(isStartWithSlash ? 1 : 0, isEndWithSlash ? -1 : void 0))}')`;
|
|
671
|
+
const regexp = buildRegExpLiteral(matches);
|
|
632
672
|
consts.push(`export const ${name}RegExp${constsCounterValue} = ${regexp};\n`);
|
|
633
673
|
if (schema.format && !predefinedZodFormats.has(schema.format) && isZodV4) functions.push(["stringFormat", [`'${jsStringLiteralEscape(schema.format)}'`, `${name}RegExp${constsCounterValue}`]]);
|
|
634
674
|
else functions.push(["regex", `${name}RegExp${constsCounterValue}`]);
|
|
635
675
|
}
|
|
636
676
|
if (schema.enum && type !== "array") {
|
|
637
|
-
const
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
677
|
+
const enumMembers = getEnumMembers(schema);
|
|
678
|
+
const hasMetadata = hasEnumMetadata(enumMembers);
|
|
679
|
+
const enumValueInfo = getEnumValueInfo(enumMembers);
|
|
680
|
+
const enumValues = enumMembers.map((member) => member.value);
|
|
681
|
+
if (enumValueInfo.isHomogeneous && !enumValueInfo.isBoolean && hasMetadata) {
|
|
682
|
+
const enumContent = getEnumImplementation(enumMembers, {
|
|
683
|
+
enumNamingConvention: context.output.override.namingConvention.enum,
|
|
684
|
+
enumGenerationType: EnumGeneration.CONST
|
|
685
|
+
});
|
|
686
|
+
functions.push(["enumObject", `{\n${enumContent}}`]);
|
|
687
|
+
} else if (enumValues.every((value) => isString(value))) functions.push(["enum", `[${enumValues.map((value) => `'${jsStringLiteralEscape(value)}'`).join(", ")}]`]);
|
|
688
|
+
else functions.push(["oneOf", enumMembers.map((member) => ({
|
|
689
|
+
functions: [["literal", isString(member.value) ? `'${jsStringLiteralEscape(member.value)}'` : member.value]],
|
|
641
690
|
consts: []
|
|
642
691
|
}))]);
|
|
643
692
|
}
|
|
@@ -651,7 +700,7 @@ const generateZodValidationSchemaDefinition = (schema, context, name, strict, is
|
|
|
651
700
|
consts: unique(consts)
|
|
652
701
|
};
|
|
653
702
|
};
|
|
654
|
-
const PARAMS_MODIFIER_VALIDATORS = new Set([
|
|
703
|
+
const PARAMS_MODIFIER_VALIDATORS = /* @__PURE__ */ new Set([
|
|
655
704
|
"optional",
|
|
656
705
|
"nullable",
|
|
657
706
|
"nullish",
|
|
@@ -664,12 +713,20 @@ const PARAMS_MODIFIER_VALIDATORS = new Set([
|
|
|
664
713
|
"undefined",
|
|
665
714
|
"void"
|
|
666
715
|
]);
|
|
667
|
-
const PARAMS_MERGE_INTO_OPTIONS_VALIDATORS = new Set([
|
|
716
|
+
const PARAMS_MERGE_INTO_OPTIONS_VALIDATORS = /* @__PURE__ */ new Set([
|
|
668
717
|
"datetime",
|
|
669
718
|
"time",
|
|
670
719
|
"iso.datetime",
|
|
671
720
|
"iso.time"
|
|
672
721
|
]);
|
|
722
|
+
const buildMetaArgs = (args) => {
|
|
723
|
+
const parts = [];
|
|
724
|
+
if (args.id !== void 0) parts.push(`id: '${jsStringEscape(args.id)}'`);
|
|
725
|
+
if (args.title !== void 0) parts.push(`title: '${jsStringEscape(args.title)}'`);
|
|
726
|
+
if (args.description !== void 0) parts.push(`description: '${jsStringEscape(args.description)}'`);
|
|
727
|
+
if (args.deprecated) parts.push("deprecated: true");
|
|
728
|
+
return parts.length > 0 ? `{ ${parts.join(", ")} }` : void 0;
|
|
729
|
+
};
|
|
673
730
|
const parseZodValidationSchemaDefinition = (input, context, coerceTypes = false, strict, isZodV4, preprocess, paramsInjection, variant = "classic", exactOptional = false) => {
|
|
674
731
|
if (input.functions.length === 0) return {
|
|
675
732
|
zod: "",
|
|
@@ -772,6 +829,13 @@ ${Object.entries(objectArgs).map(([key, schema]) => {
|
|
|
772
829
|
};
|
|
773
830
|
continue;
|
|
774
831
|
}
|
|
832
|
+
if (fn === "enumObject") {
|
|
833
|
+
current = {
|
|
834
|
+
expr: zodMiniCall("enum", `${String(args)} as const`),
|
|
835
|
+
kind: "enum"
|
|
836
|
+
};
|
|
837
|
+
continue;
|
|
838
|
+
}
|
|
775
839
|
if (fn === "allOf") {
|
|
776
840
|
const allOfArgs = args;
|
|
777
841
|
const mergedProperties = strict ? mergeAllOfObjectsMini(allOfArgs) : null;
|
|
@@ -867,8 +931,9 @@ ${Object.entries(objectArgs).map(([key, schema]) => {
|
|
|
867
931
|
}
|
|
868
932
|
const combinedArgs = buildCombinedArgs(fn, args, fieldPath);
|
|
869
933
|
if (fn === "int" && shouldCoerce("number")) {
|
|
934
|
+
const numberArgs = buildCombinedArgs("number", void 0, fieldPath);
|
|
870
935
|
current = {
|
|
871
|
-
expr: zodMiniCall("pipe", `${zodMiniCoerceCall("number",
|
|
936
|
+
expr: zodMiniCall("pipe", `${zodMiniCoerceCall("number", numberArgs)}, ${zodMiniCall("int", combinedArgs)}`),
|
|
872
937
|
kind: "number"
|
|
873
938
|
};
|
|
874
939
|
continue;
|
|
@@ -889,7 +954,16 @@ ${Object.entries(objectArgs).map(([key, schema]) => {
|
|
|
889
954
|
};
|
|
890
955
|
continue;
|
|
891
956
|
}
|
|
892
|
-
if (fn === "
|
|
957
|
+
if (fn === "meta") {
|
|
958
|
+
const value = requireCurrent(fn);
|
|
959
|
+
const metaArgs = buildMetaArgs(args);
|
|
960
|
+
if (metaArgs) current = {
|
|
961
|
+
expr: `${value.expr}.check(${zodMiniCall(fn, metaArgs)})`,
|
|
962
|
+
kind: value.kind
|
|
963
|
+
};
|
|
964
|
+
continue;
|
|
965
|
+
}
|
|
966
|
+
if (fn === "describe") {
|
|
893
967
|
const value = requireCurrent(fn);
|
|
894
968
|
current = {
|
|
895
969
|
expr: `${value.expr}.check(${zodMiniCall(fn, combinedArgs)})`,
|
|
@@ -963,12 +1037,13 @@ ${Object.entries(mergedProperties).map(([key, schema]) => {
|
|
|
963
1037
|
usedRefs.add(refArgs.name);
|
|
964
1038
|
return `__REF_${refArgs.name}__`;
|
|
965
1039
|
}
|
|
1040
|
+
if (fn === "enumObject") {
|
|
1041
|
+
const enumObjectImplementation = args;
|
|
1042
|
+
return isZodV4 ? `.enum(${enumObjectImplementation})` : `.nativeEnum(${enumObjectImplementation} as const)`;
|
|
1043
|
+
}
|
|
966
1044
|
if (fn === "meta") {
|
|
967
|
-
const metaArgs = args;
|
|
968
|
-
|
|
969
|
-
if (metaArgs.description !== void 0) parts.push(`description: '${jsStringEscape(metaArgs.description)}'`);
|
|
970
|
-
if (metaArgs.deprecated) parts.push("deprecated: true");
|
|
971
|
-
return `.meta({ ${parts.join(", ")} })`;
|
|
1045
|
+
const metaArgs = buildMetaArgs(args);
|
|
1046
|
+
return metaArgs ? `.meta(${metaArgs})` : "";
|
|
972
1047
|
}
|
|
973
1048
|
if (fn === "fileOrString") return "zod.instanceof(File).or(zod.string())";
|
|
974
1049
|
if (fn === "allOf") {
|
|
@@ -1315,6 +1390,12 @@ const parseParameters = ({ data, context, operationName, isZodV4, strict, genera
|
|
|
1315
1390
|
consts: []
|
|
1316
1391
|
}
|
|
1317
1392
|
};
|
|
1393
|
+
const initialDefinitionsByParameters = {
|
|
1394
|
+
headers: {},
|
|
1395
|
+
queryParams: {},
|
|
1396
|
+
params: {}
|
|
1397
|
+
};
|
|
1398
|
+
const constNameRegistry = {};
|
|
1318
1399
|
const defintionsByParameters = data.reduce((acc, val) => {
|
|
1319
1400
|
const { schema: parameter } = resolveRef(val, context);
|
|
1320
1401
|
if (!parameter.schema) return acc;
|
|
@@ -1337,7 +1418,8 @@ const parseParameters = ({ data, context, operationName, isZodV4, strict, genera
|
|
|
1337
1418
|
if (parameter.in !== "path" && parameter.in !== "query" && parameter.in !== "header") return acc;
|
|
1338
1419
|
const definition = generateZodValidationSchemaDefinition(schemaForGen, context, camel(`${operationName}-${parameter.in}-${parameter.name}`), mapStrict[parameter.in], isZodV4, {
|
|
1339
1420
|
required: parameter.required,
|
|
1340
|
-
useReusableSchemas
|
|
1421
|
+
useReusableSchemas,
|
|
1422
|
+
constNameRegistry
|
|
1341
1423
|
});
|
|
1342
1424
|
if (parameter.in === "header" && mapGenerate.header) return {
|
|
1343
1425
|
...acc,
|
|
@@ -1361,11 +1443,7 @@ const parseParameters = ({ data, context, operationName, isZodV4, strict, genera
|
|
|
1361
1443
|
}
|
|
1362
1444
|
};
|
|
1363
1445
|
return acc;
|
|
1364
|
-
},
|
|
1365
|
-
headers: {},
|
|
1366
|
-
queryParams: {},
|
|
1367
|
-
params: {}
|
|
1368
|
-
});
|
|
1446
|
+
}, initialDefinitionsByParameters);
|
|
1369
1447
|
const headers = {
|
|
1370
1448
|
functions: [],
|
|
1371
1449
|
consts: []
|
|
@@ -1406,8 +1484,9 @@ const generateZodRoute = async ({ operationId, operationName, typeName, verb, ov
|
|
|
1406
1484
|
const useReusableSchemas = context.output.override.zod.generateReusableSchemas;
|
|
1407
1485
|
const spec = context.spec.paths?.[pathRoute];
|
|
1408
1486
|
if (spec == void 0) throw new Error(`No such path ${pathRoute} in ${context.projectName}`);
|
|
1487
|
+
const parameters = [...spec.parameters ?? [], ...spec[verb]?.parameters ?? []];
|
|
1409
1488
|
const parsedParameters = parseParameters({
|
|
1410
|
-
data:
|
|
1489
|
+
data: parameters,
|
|
1411
1490
|
context,
|
|
1412
1491
|
operationName,
|
|
1413
1492
|
isZodV4,
|
|
@@ -1493,7 +1572,7 @@ const generateZodRoute = async ({ operationId, operationName, typeName, verb, ov
|
|
|
1493
1572
|
const inputResponses = parsedResponses.map((parsedResponse, index) => parseZodValidationSchemaDefinition(parsedResponse.input, context, override.zod.coerce.response, override.zod.strict.response, isZodV4, preprocessResponse, makeParamsInjection("response", responses[index][0] ? `${responses[index][0]}Response` : "Response"), zodVariant, override.zod.exactOptional));
|
|
1494
1573
|
const SENTINEL_PATTERN = /__REF_([A-Za-z_$][A-Za-z0-9_$]*)__/g;
|
|
1495
1574
|
const rewriteSentinels = (s) => s.replaceAll(SENTINEL_PATTERN, (_m, name) => name);
|
|
1496
|
-
const allUsedRefs = new Set([
|
|
1575
|
+
const allUsedRefs = /* @__PURE__ */ new Set([
|
|
1497
1576
|
...inputParams.usedRefs,
|
|
1498
1577
|
...inputQueryParams.usedRefs,
|
|
1499
1578
|
...inputHeaders.usedRefs,
|
|
@@ -1573,7 +1652,7 @@ export const ${bodyName} = ${zodArrayWithBounds(bodyName + "Item", parsedBody.ru
|
|
|
1573
1652
|
const operationResponse = allocateExportName(pascal(`${typeName}-${responses[index][0]}-response`), parsedResponses[index].isArray);
|
|
1574
1653
|
if (!inputResponse.zod) {
|
|
1575
1654
|
if (!override.zod.generate.response) return [];
|
|
1576
|
-
const noContentStatusCodes = new Set(["204", "205"]);
|
|
1655
|
+
const noContentStatusCodes = /* @__PURE__ */ new Set(["204", "205"]);
|
|
1577
1656
|
const statusCode = responses[index][0];
|
|
1578
1657
|
const isEachHttpStatusMode = !!statusCode;
|
|
1579
1658
|
let isNoContent;
|