moleculer-zod-validator
Version:
A validator for the Moleculer microservice framework to allow the use of Zod.
223 lines (222 loc) • 10.9 kB
TypeScript
import { ZodType, z } from "zod";
import type { ZodArray, ZodNullable, ZodObject, ZodOptional, ZodRawShape, ZodTuple, ZodTupleItems, ZodTypeAny } from "zod";
/**
* An adapter for a standard {@link https://github.com/colinhacks/zod#objects ZodObject},
* which can be easier to work with in Moleculer than Zod on its own.
*/
export declare class ZodParams<ZPSchema extends Parameters<(typeof z)["object"]>[0], ZPOptions extends ZodParamsOptionsType, ZPReturn = any> {
private _rawSchema;
private _rawSchemaWithOptions;
/** This property is purely for type inference and should not be used. */
_mode: ZPOptions["strip"] extends true ? "strip" : ZPOptions["strict"] extends true ? "strict" : ZPOptions["passthrough"] extends true ? "passthrough" : "strip";
/** This property is purely for type inference and should not be used. */
_processedSchema: ZPOptions["partial"] extends true ? ZodParamsMakeOptionalSchema<ZPSchema> : ZPOptions["deepPartial"] extends true ? {
[K in keyof ZPSchema]: ZPSchema[K] extends ZodOptional<ZPSchema[K]> ? ZodDeepPartial<ZPSchema[K]> : ZodOptional<ZodDeepPartial<ZPSchema[K]>>;
} : ZPSchema;
/** This property is purely for type inference and should not be used. */
_catchall: ZodTypeAny;
readonly _validator: z.ZodObject<this["_processedSchema"], this["_mode"], this["_catchall"]>;
/**
* Creates a new ZodParams adapter, which can be used to more easily provide typing
* information to Moleculer services and calls.
* @param {ZodRawShape} schema - The schema used in
* {@link https://github.com/colinhacks/zod#objects z.object()}.
* @param {ZodParamsOptionsType} options - This exposes several methods available
* on the ZodObject type,
* {@link https://github.com/colinhacks/zod#table-of-contents all of which can be referenced under the Objects section in the Zod documentation}.
* @param {any} returnType - The return type of the action in question. This does
* nothing at runtime and is used purely for storing a type on the object that can
* then be referenced later on. This can be done like so:
*
* @example <caption>The return type will be `Promise<string>`</caption>
* new ZodParams({ property: z.string() }, undefined, {} as Promise<string>)
*
* @todo
* **Note**: {@link https://github.com/colinhacks/zod/issues/1949 There's currently a known issue in Zod where catchall type inferences don't work correctly.}
* Until this upstream issue is fixed, catchall type inferences on ZodParams will
* be disabled as not to break existing projects. This will not impact the runtime
* behavior of catchall in the validator, just the type inference.
*
* If you wish to emulate the type inference, you can do so by using a TS union
* when using broker.call or ctx.call.
*
* @example
* broker.call<
* typeof zodParamObject.return,
* typeof zodParamObject.call & {[index: string]: string}
* >({ ... })
*
*/
constructor(schema: ZPSchema, options?: ZPOptions, returnType?: ZPReturn);
/**
* Returns the raw Zod schema provided in the constructor. This should be passed
* to the `params` object in the action definition.
*
* @example
* broker.createService({
* name: "example",
* actions: {
* exampleAction: {
* params: zodParamObject.schema,
* handler(ctx: Context<typeof zodParamObject.context>) { ... }
* }
* }
* });
*/
get schema(): ZPSchema & {
$$$options: {
partial?: boolean | undefined;
deepPartial?: boolean | undefined;
strict?: boolean | undefined;
catchall?: any;
passthrough?: boolean | undefined;
strip?: boolean | undefined;
refine?: ((args_0: any, ...args_1: unknown[]) => unknown) | {
validator: (args_0: any, ...args_1: unknown[]) => unknown;
params?: {
message?: string | undefined;
path?: (string | number)[] | undefined;
params?: z.objectOutputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
} | undefined;
} | undefined;
superRefine?: ((args_0: any, args_1: any, ...args_2: unknown[]) => unknown) | undefined;
};
};
/**
* Returns the compiled ZodObject validator.
*/
get validator(): z.ZodObject<this["_processedSchema"], this["_mode"], this["_catchall"], z.objectOutputType<this["_processedSchema"], this["_catchall"], this["_mode"]>, z.objectInputType<this["_processedSchema"], this["_catchall"], this["_mode"]>>;
/**
* The inferred input type from the compiled validator. This should be used with
* `broker.call` or `ctx.call` as the second type parameter to get proper types
* for the action call.
*
* @example
* broker.call<
* typeof zodParamObject.return,
* typeof zodParamObject.call
* >({ ... })
*/
readonly call: z.input<(typeof this)["_validator"]>;
/**
* The inferred output type from the compiled validator. This should be used within
* the `Context` object in the action definition to get the proper types after the
* parameters have passed through validation (and possible transformations).
*
* @example
* broker.createService({
* name: "example",
* actions: {
* exampleAction: {
* params: zodParamObject.schema,
* handler(ctx: Context<typeof zodParamObject.context>) { ... }
* }
* }
* });
*/
readonly context: z.output<(typeof this)["_validator"]>;
/**
* The output type provided at the time of instantiation. This should be used with
* `broker.call` or `ctx.call` as the first type parameter to get proper types for
* the action call.
*
* @example
* broker.call<
* typeof zodParamObject.return,
* typeof zodParamObject.call
* >({ ... })
*/
readonly return: Promise<ZPReturn>;
}
declare const ZodParamsOptions: z.ZodObject<{
partial: z.ZodOptional<z.ZodDefault<z.ZodBoolean>>;
deepPartial: z.ZodOptional<z.ZodDefault<z.ZodBoolean>>;
strict: z.ZodOptional<z.ZodDefault<z.ZodBoolean>>;
catchall: z.ZodOptional<z.ZodAny>;
passthrough: z.ZodOptional<z.ZodBoolean>;
strip: z.ZodOptional<z.ZodDefault<z.ZodBoolean>>;
refine: z.ZodOptional<z.ZodUnion<[z.ZodFunction<z.ZodTuple<[z.ZodAny], z.ZodUnknown>, z.ZodUnknown>, z.ZodObject<{
validator: z.ZodFunction<z.ZodTuple<[z.ZodAny], z.ZodUnknown>, z.ZodUnknown>;
params: z.ZodOptional<z.ZodObject<{
message: z.ZodOptional<z.ZodString>;
path: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">>;
params: z.ZodOptional<z.ZodObject<{}, "passthrough", z.ZodTypeAny, z.objectOutputType<{}, z.ZodTypeAny, "passthrough">, z.objectInputType<{}, z.ZodTypeAny, "passthrough">>>;
}, "strip", z.ZodTypeAny, {
message?: string | undefined;
path?: (string | number)[] | undefined;
params?: z.objectOutputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
}, {
message?: string | undefined;
path?: (string | number)[] | undefined;
params?: z.objectInputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
}>>;
}, "strip", z.ZodTypeAny, {
validator: (args_0: any, ...args_1: unknown[]) => unknown;
params?: {
message?: string | undefined;
path?: (string | number)[] | undefined;
params?: z.objectOutputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
} | undefined;
}, {
validator: (args_0: any, ...args_1: unknown[]) => unknown;
params?: {
message?: string | undefined;
path?: (string | number)[] | undefined;
params?: z.objectInputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
} | undefined;
}>]>>;
superRefine: z.ZodOptional<z.ZodFunction<z.ZodTuple<[z.ZodAny, z.ZodAny], z.ZodUnknown>, z.ZodUnknown>>;
}, "strip", z.ZodTypeAny, {
partial?: boolean | undefined;
deepPartial?: boolean | undefined;
strict?: boolean | undefined;
catchall?: any;
passthrough?: boolean | undefined;
strip?: boolean | undefined;
refine?: ((args_0: any, ...args_1: unknown[]) => unknown) | {
validator: (args_0: any, ...args_1: unknown[]) => unknown;
params?: {
message?: string | undefined;
path?: (string | number)[] | undefined;
params?: z.objectOutputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
} | undefined;
} | undefined;
superRefine?: ((args_0: any, args_1: any, ...args_2: unknown[]) => unknown) | undefined;
}, {
partial?: boolean | undefined;
deepPartial?: boolean | undefined;
strict?: boolean | undefined;
catchall?: any;
passthrough?: boolean | undefined;
strip?: boolean | undefined;
refine?: ((args_0: any, ...args_1: unknown[]) => unknown) | {
validator: (args_0: any, ...args_1: unknown[]) => unknown;
params?: {
message?: string | undefined;
path?: (string | number)[] | undefined;
params?: z.objectInputType<{}, z.ZodTypeAny, "passthrough"> | undefined;
} | undefined;
} | undefined;
superRefine?: ((args_0: any, args_1: any, ...args_2: unknown[]) => unknown) | undefined;
}>;
export type ZodParamsOptionsType = {
catchall?: ZodTypeAny;
refine?: Parameters<ZodType["refine"]>[0] | {
validator: Parameters<ZodType["refine"]>[0];
params?: {
message?: string;
path?: (string | number)[];
params?: object;
};
};
superRefine?: Parameters<ZodType["superRefine"]>[0];
} & Omit<z.input<typeof ZodParamsOptions>, "catchall" | "refine" | "superRefine">;
type ZodParamsMakeOptionalSchema<T extends Parameters<(typeof z)["object"]>[0]> = {
[K in keyof T]: T[K] extends ZodOptional<T[K]> ? T[K] : ZodOptional<T[K]>;
};
type ZodDeepPartial<T extends ZodTypeAny> = T extends ZodObject<ZodRawShape> ? ZodObject<{
[k in keyof T["shape"]]: ZodOptional<ZodDeepPartial<T["shape"][k]>>;
}, T["_def"]["unknownKeys"], T["_def"]["catchall"]> : T extends ZodArray<infer Type, infer Card> ? ZodArray<ZodDeepPartial<Type>, Card> : T extends ZodOptional<infer Type> ? ZodOptional<ZodDeepPartial<Type>> : T extends ZodNullable<infer Type> ? ZodNullable<ZodDeepPartial<Type>> : T extends ZodTuple<infer Items> ? {
[k in keyof Items]: Items[k] extends ZodTypeAny ? ZodDeepPartial<Items[k]> : never;
} extends infer PI ? PI extends ZodTupleItems ? ZodTuple<PI> : never : never : T;
export {};