zod-to-x 2.5.0 → 2.6.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/README.md +15 -0
- package/dist/lib/zod_ext.d.ts +6 -0
- package/dist/lib/zod_helpers.d.ts +10 -2
- package/dist/lib/zod_helpers.js +14 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -601,6 +601,21 @@ readonly DiscriminantDataRetrieve = z.lazy(() => // Lazy the zod type itself
|
|
|
601
601
|
// | HttpSuccessfulResponse<SomeDtoResult>
|
|
602
602
|
// | HttpUnsuccessfulResponse;
|
|
603
603
|
```
|
|
604
|
+
4) A default type can be provided as the second argument of `createGenericType`. It can be any supported type: a basic type, an array, a named type, ... Templates with a default type can be omitted in `useGenericType`, and the default type is used instead. The default is applied when the generic type is instantiated, so the output is valid for all supported languages, including the ones without default type parameters (Go, Dart):
|
|
605
|
+
```ts
|
|
606
|
+
// inside GenericsInfrastructure class
|
|
607
|
+
readonly Page = z.object({
|
|
608
|
+
cursor: createGenericType("TCursor", z.string()),
|
|
609
|
+
content: createGenericType("TContent", this.SomeDtoResult),
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
readonly DefaultPage = useGenericType(this.Page, {});
|
|
613
|
+
readonly PageOfIds = useGenericType(this.Page, { content: z.array(z.string()) });
|
|
614
|
+
|
|
615
|
+
// Output
|
|
616
|
+
// export interface DefaultPage extends Page<string, SomeDtoResult> {}
|
|
617
|
+
// export interface PageOfIds extends Page<string, string[]> {}
|
|
618
|
+
```
|
|
604
619
|
|
|
605
620
|
Complete definition examples can be found [here](https://github.com/rroumenov/zod-to-x/blob/main/test/common/layered_generics.ts).
|
|
606
621
|
|
package/dist/lib/zod_ext.d.ts
CHANGED
|
@@ -112,6 +112,12 @@ export interface IZod2xMetadata {
|
|
|
112
112
|
generic: ZodObject<any>;
|
|
113
113
|
templates: [string, ZodType][];
|
|
114
114
|
};
|
|
115
|
+
/**
|
|
116
|
+
* For Layered Modeling defining generics.
|
|
117
|
+
* For generic templates created with `createGenericType`, the type used by `useGenericType`
|
|
118
|
+
* when no type is provided for the template.
|
|
119
|
+
*/
|
|
120
|
+
genericDefault?: ZodType;
|
|
115
121
|
}
|
|
116
122
|
declare module "zod/v4" {
|
|
117
123
|
interface ZodObject<in Shape, Config> {
|
|
@@ -125,13 +125,21 @@ export declare class ZodHelpers {
|
|
|
125
125
|
static createZodObject(properties: Map<string, ZodType>): ZodObject<any>;
|
|
126
126
|
static getZodNumberConstraints(i: ZodNumber | z.ZodBigInt): ZodNumberConstraints;
|
|
127
127
|
}
|
|
128
|
-
|
|
128
|
+
/**
|
|
129
|
+
* Creates a generic template to be used as a property of a generic ZodObject.
|
|
130
|
+
* @param name Template name. Ex: "T", "K", "TData", ...
|
|
131
|
+
* @param defaultType Optional type used by `useGenericType` when no type is provided for the
|
|
132
|
+
* template. It can be any supported type: basic types, arrays, named types, ...
|
|
133
|
+
* @returns The generic template.
|
|
134
|
+
*/
|
|
135
|
+
export declare function createGenericType(name: string, defaultType?: ZodType): any;
|
|
129
136
|
/**
|
|
130
137
|
* Use generic types defined in a generic ZodObject by replacing them with the actual child types.
|
|
131
138
|
* The new type references the generic type and the child types used for each template, so its
|
|
132
139
|
* type name, layer and templates translation are resolved from them when transpiled.
|
|
133
140
|
* @param genObj ZodObject with generic types.
|
|
134
|
-
* @param childrens Record of child types to replace generics.
|
|
141
|
+
* @param childrens Record of child types to replace generics. Templates created with a default
|
|
142
|
+
* type can be omitted.
|
|
135
143
|
* @returns The extended ZodObject with replaced generic types.
|
|
136
144
|
*/
|
|
137
145
|
export declare function useGenericType(genObj: ZodObject<any>, childrens: Record<string, ZodType>, skipLazy: true): ZodObject<any>;
|
package/dist/lib/zod_helpers.js
CHANGED
|
@@ -294,25 +294,33 @@ class ZodHelpers {
|
|
|
294
294
|
}
|
|
295
295
|
}
|
|
296
296
|
exports.ZodHelpers = ZodHelpers;
|
|
297
|
-
|
|
298
|
-
|
|
297
|
+
/**
|
|
298
|
+
* Creates a generic template to be used as a property of a generic ZodObject.
|
|
299
|
+
* @param name Template name. Ex: "T", "K", "TData", ...
|
|
300
|
+
* @param defaultType Optional type used by `useGenericType` when no type is provided for the
|
|
301
|
+
* template. It can be any supported type: basic types, arrays, named types, ...
|
|
302
|
+
* @returns The generic template.
|
|
303
|
+
*/
|
|
304
|
+
function createGenericType(name, defaultType) {
|
|
305
|
+
const template = zod_ext_1.Extended.getZ().promise(zod_ext_1.Extended.getZ().literal(name));
|
|
306
|
+
return defaultType ? (0, zod2x_metadata_1.setZod2xMetadata)(template, { genericDefault: defaultType }) : template;
|
|
299
307
|
}
|
|
300
308
|
function useGenericType(genObj, childrens, skipLazy) {
|
|
301
309
|
const builder = () => {
|
|
302
|
-
var _a, _b;
|
|
310
|
+
var _a, _b, _c, _d;
|
|
303
311
|
let extended = genObj;
|
|
304
312
|
const templates = [];
|
|
305
313
|
// Zod 4's extend() doesn't copy meta() (unlike Zod 3 which spread _def).
|
|
306
314
|
// Capture it once so it can be restored after each extend call, keeping the
|
|
307
315
|
// description of the generic type.
|
|
308
316
|
// The id is not restored because it must be unique in Zod's global registry.
|
|
309
|
-
const
|
|
317
|
+
const _e = (_a = genObj.meta()) !== null && _a !== void 0 ? _a : {}, { id: _id } = _e, genObjMeta = __rest(_e, ["id"]);
|
|
310
318
|
for (const [key, property] of Object.entries(genObj.shape)) {
|
|
311
319
|
if (ZodHelpers.isZod2XGeneric(property)) {
|
|
312
|
-
const childType = childrens[key];
|
|
320
|
+
const childType = (_b = childrens[key]) !== null && _b !== void 0 ? _b : (_c = (0, zod2x_metadata_1.getZod2xMetadata)(property)) === null || _c === void 0 ? void 0 : _c.genericDefault;
|
|
313
321
|
if (!childType) {
|
|
314
322
|
throw new Error(`Missing child type for generic property ${key} in ` +
|
|
315
|
-
`${(
|
|
323
|
+
`${(_d = (0, zod2x_metadata_1.resolveZod2xMetadata)(genObj)) === null || _d === void 0 ? void 0 : _d.typeName}.`);
|
|
316
324
|
}
|
|
317
325
|
extended = extended.extend({ [key]: childType });
|
|
318
326
|
if (Object.keys(genObjMeta).length > 0) {
|