This version has reached end-of-life. It is no longer maintained. For the latest stable version,
click here.
Validating
Moleculer has a built-in validator module. It uses the fastest-validator library.
Built-in validator It’s enabled by default, so you should just define params
property in action definition which contains validation schema for the incoming ctx.params
.
Example
const { ServiceBroker } = require ("moleculer" );const broker = new ServiceBroker({ validation: true }); broker.createService({ name: "say" , actions: { hello: { 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)); broker.call("say.hello" , { name : 123 }).then(console .log) .catch(err => console .error(err.message)); broker.call("say.hello" , { name : "Walter" }).then(console .log) .catch(err => console .error(err.message));
Play it on Runkit
Example validation schema
{ id: { type : "number" , positive : true , integer : true }, name: { type : "string" , min : 3 , max : 255 }, status: "boolean" }
Documentation Find more information about validation schema in the documentation of the library
Custom validator Custom validator can be created. You should implement compile
and validate
methods of BaseValidator
.
Create a Joi validator const BaseValidator = require ("moleculer" ).Validator;const { ValidationError } = require ("moleculer" ).Errors;class JoiValidator extends BaseValidator { constructor () { super (); this .validator = require ("joi" ); } compile(schema) { return (params ) => this .validate(params, schema); } validate(params, schema) { const res = this .validator.validate(params, schema); if (res.error) throw new ValidationError(res.error.message, null , res.error.details); return true ; } } module .exports = JoiValidator;
Use custom Joi validator
const { ServiceBroker } = require ("moleculer" );const Joi = require ("joi" );const JoiValidator = require ("./joi.validator" );const broker = new ServiceBroker({ logger: true , validation: true , validator: new JoiValidator() }); broker.createService({ name: "greeter" , actions: { hello: { params: Joi.object().keys({ name: Joi.string().min(4 ).max(30 ).required() }), handler(ctx) { return `Hello ${ctx.params.name} ` ; } } } }); broker.start() .then(() => broker.call("greeter.hello" ).then(res => broker.logger.info(res))) .catch(err => broker.logger.error(err.message, err.data)) .then(() => broker.call("greeter.hello" , { name : 100 }).then(res => broker.logger.info(res))) .catch(err => broker.logger.error(err.message, err.data)) .then(() => broker.call("greeter.hello" , { name : "Joe" }).then(res => broker.logger.info(res))) .catch(err => broker.logger.error(err.message, err.data)) .then(() => broker.call("greeter.hello" , { name : "John" }).then(res => broker.logger.info(res))) .catch(err => broker.logger.error(err.message, err.data));
Find more validators Check the modules page and find more validators.