moleculerjs/moleculer

Improvement to work with Joi validator

Open

#419 opened on 2018年11月18日

GitHub で見る
 (3 comments) (0 reactions) (0 assignees)JavaScript (5,919 stars) (588 forks)batch import
Module: CoreStatus: On Holdhelp wanted

説明

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed
  • I'm reporting the issue to the correct repository

Expected Behavior

When I want to use Joi as validator inside a single node containing the API GW and internal nodes, I can't query internal nodes correctly.

Current Behavior

It fails validation

GET http://localhost:3000/api/v2/~node/actions?withServices=true&onlyAvailable=false

HTTP/1.1 422 Unprocessable Entity
X-Powered-By: Express
Content-type: application/json; charset=utf-8
Date: Sun, 18 Nov 2018 18:38:33 GMT
Connection: keep-alive
Transfer-Encoding: chunked

{
  "name": "ValidationError",
  "message": "child \"onlyAvailable\" fails because [\"onlyAvailable\" must be an object]",
  "code": 422,
  "type": "VALIDATION_ERROR",
  "data": [
    {
      "message": "\"onlyAvailable\" must be an object",
      "path": [
        "onlyAvailable"
      ],
      "type": "object.base",
      "context": {
        "key": "onlyAvailable",
        "label": "onlyAvailable"
      }
    }
  ]
}

Response code: 422 (Unprocessable Entity); Time: 49ms; Content length: 423 bytes

Failure Information

This bug happend because the validator for $node.list look like this and isn't a Joi schema (of course):

https://github.com/moleculerjs/moleculer/blob/2071fcf3ff8d54416b351312921dc184e22db58f/src/internals.js#L15-L25

The only solution I see is to create another ServiceBroker with the fastest-validator but I don't want to handle multiple nodes for now (I'm just starting with moleculer, it's awesome 🎉).

Is there a way to override the validator on a per service base?

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Create a ServiceBroker with a Joi Validator (from the doc example)
  2. Launch an API GW in it
  3. Request to GET http://localhost:3000/api/v2/~node/actions?withServices=true&onlyAvailable=false
  4. You should get an error of validation

Reproduce code snippet

class JoiValidator extends BaseValidator {
  constructor() {
    super();
    // eslint-disable-next-line global-require
    this.validator = require('joi');
  }

  compile(schema) {
    return params => this.validate(params, schema);
  }

  validate(params, schema) {
    const { error } = this.validator.validate(params, schema);
    if (error) throw new ValidationError(error.message, null, error.details);

    return true;
  }
}

const broker = new ServiceBroker({
    logger: console,
    transporter: "NATS",
    validation: true,
    validator: new JoiValidator(),
});

broker.createService({
    name: "test",
    mixins: [ApiGateway],
    settings: {
	    middleware: true,
	
	    routes: [{	
	      // Will also fail if the validator is strict (icebob/fastest-validator#11) ?
	      mergeParams: false,
	
	      // List all routes
	      aliases: {
	        'REST stock-events': 'stock-events',
	      },
	    }],
	}
});

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • Moleculer version: v0.13.4
  • NodeJS version: LTS
  • Operating System: Windows 10

Failure Logs

See request upper.

コントリビューターガイド