moleculerjs / moleculer

:rocket: Progressive microservices framework for Node.js
https://moleculer.services/
MIT License
6.16k stars 587 forks source link

Error: Method must be a function #1043

Closed BrunyC closed 2 years ago

BrunyC commented 2 years ago

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:

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;

And here is my service:

const { Service } = require("moleculer");
const Joi = require("joi");
const JoiValidator = require("../validators/joi.validator");

module.exports = function(broker) {
    return new Service(broker, {
        name: "mainService",
        logger: true,
        validation: true,
        validator: new JoiValidator(),
        actions: {
            contacts: {
                params: {
                    created: {type: "string"},
                    data: Joi.object().keys({
                                               name: {type: "string"})
                },
                handler(ctx) {
                    return "Return=> " + ctx.params;
                }
            }
        }
    });
};

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?

filiafobico commented 2 years ago

I think you must define your custom validator inside a moleculer.config, not inside of a service definition.
see: Custom-validator

BrunyC commented 2 years ago

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.

icebob commented 2 years ago

Please create a repro repo.