Moleculer validation example
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.data));
broker.call("say.hello", { name: 123 }).then(console.log).catch(err => console.error(err.data));
broker.call("say.hello", { name: "Walter" }).then(console.log).catch(err => console.error(err.data));
no comments