Validation

Moleculer has a built-in validator module. It uses the fastest-validator library.

let { ServiceBroker } = require("moleculer");
let broker = new ServiceBroker({
validation: true // Default is true
});
broker.createService({
name: "say",
actions: {
hello: {
// Parameters definitions to validator
params: {
name: { type: "string", min: 2 }
},
handler(ctx) {
return "Hello " + ctx.params.name;
}
}
}
});
broker.call("say.hello").then(console.log)
.catch(err => console.error(err.message));
// -> throw ValidationError: "The 'name' field is required!"
broker.call("say.hello", { name: 123 }).then(console.log)
.catch(err => console.error(err.message));
// -> throw ValidationError: "The 'name' field must be a string!"
broker.call("say.hello", { name: "Walter" }).then(console.log)
.catch(err => console.error(err.message));
// -> "Hello Walter"

Play it on Runkit

Example validation schema

{
id: { type: "number", positive: true, integer: true },
name: { type: "string", min: 3, max: 255 },
status: "boolean" // short-hand def
}

Documentation

You can find more information about validation schema in the documentation of the library