Created
January 8, 2019 04:50
-
-
Save aquilesb/f38d3b2410881a68970e0e2bbd740330 to your computer and use it in GitHub Desktop.
How create a Moleculer service broker with multi-language validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
ServiceBroker | |
} = require('moleculer'); | |
const I18NValidator = require('./I18NValidator'); | |
const broker = new ServiceBroker({ | |
nodeID: 'auth-server', | |
logLevel: process.env.LOGGER_LEVEL, | |
logger: true, | |
requestTimeout: 5 * 1000, | |
requestRetry: 3, | |
validation: true, | |
validator: new I18NValidator({ | |
en: { | |
required: "The '{field}' field is required!", | |
string: "The '{field}' field must be a string!", | |
}, | |
hu: { | |
required: "The '{field}' mezőnek szövegnek kell lennie!", | |
string: "A '{field}' mezőnek szövegnek kell lennie!", | |
}, | |
}), | |
}); | |
// Load API Gateway | |
const qnt = broker.loadServices(`${process.cwd()}/src/`, '*.service.js'); | |
// const qnt = broker.loadServices(`./`, '*.service.js'); | |
broker.logger.info(`------------------ ${qnt} SERVICES LOADED----------`); | |
// Start server | |
broker.start() | |
// No meta lang and use default eng | |
.then(() => broker.call("authserver.login", { | |
name: 100 | |
}).then(res => broker.logger.info(res))) | |
.catch(err => broker.logger.error(err.message, err.data)) | |
// "hu" lang | |
.then(() => broker.call("authserver.login", { | |
name: 100 | |
}, { | |
meta: { | |
lang: 'hu' | |
} | |
}).then(res => broker.logger.info(res))) | |
.catch(err => broker.logger.error(err.message, err.data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Validator = require('fastest-validator'); | |
const DefaultMessages = require('fastest-validator/lib/messages'); | |
const { ValidationError } = require('moleculer').Errors; | |
class I18NValidator { | |
constructor(messages) { | |
this.validators = {}; | |
Object.keys(messages).forEach((lang) => { | |
this.validators[lang] = new Validator(); | |
this.validators[lang].messages = Object.assign({}, DefaultMessages, messages[lang]); | |
}); | |
} | |
init(broker) { | |
this.broker = broker; | |
} | |
compile(schema) { | |
this.checks = {}; | |
Object.keys(this.validators).forEach((lang) => { | |
this.checks[lang] = this.validators[lang].compile(schema); | |
}); | |
return this.checks; | |
} | |
validate(params, schema) { | |
const res = this.validator.validate(params, schema); | |
if (res !== true) { | |
throw new ValidationError('Parameters validation error!', null, res); | |
} | |
return true; | |
} | |
/** | |
* Register validator as a middleware | |
* | |
* @memberof ParamValidator | |
*/ | |
middleware() { | |
return function I18NValidator(handler, action) { | |
// Wrap a param validator | |
if (action.params && typeof action.params === 'object') { | |
const checks = this.compile(action.params); | |
return function validateContextParams(ctx) { | |
const check = checks[ctx.meta.lang] || checks.en; | |
const res = check(ctx.params); | |
if (res === true) { | |
return handler(ctx); | |
} | |
return Promise.reject(new ValidationError('Parameters validation error!', null, res)); | |
}; | |
} | |
return handler; | |
}.bind(this); | |
} | |
} | |
module.exports = I18NValidator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const authServer = { | |
name: 'authserver', | |
actions: { | |
login: { | |
params: { | |
email: { type: 'email' }, | |
password: { type: 'string', min: 8 }, | |
}, | |
handler: () => {}, | |
}, | |
info: () => ({ message: 'ok' }), | |
}, | |
}; | |
module.exports = authServer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment