Closed BrunyC closed 2 years ago
I think you must define your custom validator inside a moleculer.config, not inside of a service definition.
see: Custom-validator
My bad, this really help. Thank you. Now I got other error.
Error log: TypeError: this.validator.validate is not a function at JoiValidator.validate (/home/bruno/HubServicesMolecular/hubapi/validators/joi.validator.js:15:30) at /home/bruno/HubServicesMolecular/hubapi/validators/joi.validator.js:11:27 at validateContextParams (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/validators/base.js:79:19) at ServiceBroker.timeoutMiddleware (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/middlewares/timeout.js:31:14) at ServiceBroker.fallbackMiddleware (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/middlewares/fallback.js:31:11) at ServiceBroker.errorHandlerMiddleware (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/middlewares/error-handler.js:14:10) at ServiceBroker.tracingLocalActionMiddleware (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/middlewares/tracing.js:97:12) at Object.metricsMiddleware [as handler] (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/middlewares/metrics.js:37:11) at ServiceBroker.call (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/service-broker.js:1145:31) at Context.call (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/context.js:286:23) at Service.callAction (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer-web/src/index.js:621:26) at Service.aliasHandler (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer-web/src/index.js:581:17) at /home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer-web/src/index.js:446:33 at processTicksAndRejections (node:internal/process/task_queues:96:5) Data: undefined
I Just defined my custom validator in molecular.config, now my applications starts but when I make some call I got this error.
Please create a repro repo.
Im new in moleculer and Im trying make a validator with Joi to validate fiels in objects inside objects. But Im got this error and I dont know why. Help me please. Error: Method must be a function
Here is my custom Joi Class:
And here is my service:
Error log: Error: Method must be a function at new module.exports (/home/bruno/HubServicesMolecular/hubapi/node_modules/@hapi/hoek/lib/error.js:23:19) at module.exports (/home/bruno/HubServicesMolecular/hubapi/node_modules/@hapi/hoek/lib/assert.js:20:11) at Validator.method (/home/bruno/HubServicesMolecular/hubapi/node_modules/joi/lib/types/any.js:39:17) at Validator.eval (eval at compileRule (/home/bruno/HubServicesMolecular/hubapi/node_modules/fastest-validator/lib/validator.js:284:15), :42:43) at Validator.eval (eval at compile (/home/bruno/HubServicesMolecular/hubapi/node_modules/fastest-validator/lib/validator.js:227:19), :9:24) at resFn (/home/bruno/HubServicesMolecular/hubapi/node_modules/fastest-validator/lib/validator.js:240:19) at validateContextParams (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/validators/base.js:79:19) at ServiceBroker.timeoutMiddleware (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/middlewares/timeout.js:31:14) at ServiceBroker.fallbackMiddleware (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/middlewares/fallback.js:31:11) at ServiceBroker.errorHandlerMiddleware (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/middlewares/error-handler.js:14:10) at ServiceBroker.tracingLocalActionMiddleware (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/middlewares/tracing.js:97:12) at Object.metricsMiddleware [as handler] (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/middlewares/metrics.js:37:11) at ServiceBroker.call (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/service-broker.js:1145:31) at Context.call (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer/src/context.js:286:23) at Service.callAction (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer-web/src/index.js:621:26) at Service.aliasHandler (/home/bruno/HubServicesMolecular/hubapi/node_modules/moleculer-web/src/index.js:581:17) Data: undefined
What Im doing wrong?