Aleks
Aleks

Reputation: 5854

loopback remote method: parameter validation

Is there a form to make loopback automatically validate input parameters in a remote method?

Let's assume we have the following definition of a remote method:

  WebuserModel.remoteMethod('overLogin',  {
      description: "Performs a Webuser's login to the system",
      accepts: [
        {
          arg: 'credentials', type: {
            "username": { type: "string", required:true },
            "password": { type: "string", required: true }
          },
          http: {source: 'body'},
          required: true
        },
      ],
      returns: {arg: 'accesToken', type: "object", root: true},
      http: {path: '/login', verb: 'post'}
    }

I would here expect from loopback to perform validation of the input parameter on each request and to raise an error if the passed object does not comply with the defined schema (mandatory object with two mandatory properties).

Apparently it does not happen. Any clue?

Upvotes: 6

Views: 1735

Answers (1)

Miroslav Bajtoš
Miroslav Bajtoš

Reputation: 10785

Disclaimer: I am a core developer of LoopBack and the author of argument validation in [email protected].

LoopBack does not support validation of nested object properties provided by the clients when invoking remote methods. Right now, we check only that the value is an object, see lib/types/object.js in strong-remoting.

In the upcoming LoopBack 4 version, we are planning to support full OpenAPI and/or JSON Schema validation for input arguments, see https://github.com/strongloop/loopback-next/issues/118

Based on the comments in that GitHub issue, it should be relatively easy to add JSONSchema-based validations to LoopBack 3.x too.

Upvotes: 2

Related Questions