moleculer-zod-validator
Validate Moleculer action parameters using the Zod validator.

Supports
Requires
As of v3.0.0, this package requires Node.js v17.0.0 or above.
Install
npm install moleculer-zod-validator
Usage
Broker
Import ZodValidator
, then set up the validator in your broker settings using the validator
property. For example:
const { ServiceBroker } = require("moleculer");
const { ZodValidator } = require("moleculer-zod-validator");
import { ServiceBroker } from "moleculer";
import { ZodValidator } from "moleculer-zod-validator";
const broker = new ServiceBroker({
validator: new ZodValidator()
});
As of v3.0.0, moleculer-zod-validator implements the default Moleculer validator (fastest-validator) as a compatibility fallback for fastest-validator schemas, like those used in Moleculer's internal services, so calling services using that should not be a problem.
Actions
One of Zod's main features is how it can infer TypeScript types from a schema. To simplify the usage of this, there is a convenience utility called ZodParams
that allows for easy access to the necessary data.
The ZodParams
constructor takes one or two arguments, schema
and optionally options
.
schema
- This is a schema object that gets passed directly into z.object
. For all available schema options, please look at the Zod documentation.options
- This provides access to some of the different functions available on a standard Zod object. All booleans default to false
except for strip
, which is implicitly set to true
.
partial
(boolean) - Shallowly makes all properties optional. (docs)deepPartial
(boolean) - Deeply makes all properties optional. (docs)strip
(boolean) - Removes unrecognized keys from the parsed input. This is Zod's default behavior and this validator's default behavior. Mutually exclusive with passthrough
and strict
, and will override them if set. (docs)passthrough
(boolean) - Passes through unrecognized keys. Mutually exclusive with strict
and strip
. (docs)strict
(boolean) - Throws an error if unrecognized keys are present. Mutually exclusive with passthrough
and strip
. (docs)catchall
(Zod validator) - Validates all unknown keys against this schema. Obviates strict
, passthrough
, and strip
. (docs)
Additionally, support for object transformations is present, allowing for the use of features such as preprocessing, refinements, transforms, and defaults.
Once constructed, there are four properties exposed on the ZodParams
object.
schema
- The raw schema passed in. This should be passed to the params
object in the action definition.context
- 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.call
- 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.validator
- The compiled validator.
const simpleValidator = new ZodParams({
string: z.string(),
number: z.number(),
optional: z.any().optional()
});
const complexValidator = new ZodParams({
string: z.string(),
number: z.number(),
object: z.object({
nestedString: z.string(),
nestedBoolean: z.boolean()
})
}, {
partial: true,
catchall: z.number()
}});
broker.createService({
name: "example",
actions: {
simpleExample: {
params: simpleValidator.schema,
handler(ctx: Context<typeof simpleValidator.context>) { ... }
},
complexExample: {
params: complexValidator.schema,
handler(ctx: Context<typeof complexExample.context>) { ... }
}
}
});
broker.call<
ReturnType,
typeof simpleValidator.call
>({ string: "yes", number: 42 });
broker.call<
ReturnType,
typeof complexValidator.call
>({
object: {
nestedString: "not optional",
nestedBoolean: false
},
unrecognizedKey: 69
});