Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loopback remote method: parameter validation

Tags:

loopbackjs

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?

like image 206
Aleks Avatar asked Nov 25 '16 11:11

Aleks


People also ask

What is remote method in LoopBack?

A remote method is a method of a model, exposed over a custom REST endpoint. Use a remote method to perform operations not provided by LoopBack's standard model REST API. Note: The easiest way to define a remote method is by using the command-line remote method generator.

Is MVC a LoopBack?

LoopBack implements the MVC pattern. The models are defined in model files, controllers provide the REST API interface, and views are JSON objects returned by the controller. This not only allows modular development of the project, but also prevents the codebase from getting messy and unmanageable as the project grows.

What is LoopBack model?

A LoopBack model is a JavaScript object with both Node and REST APIs that represents data in backend systems such as databases. Models are connected to backend systems via data sources. You use the model APIs to interact with the data source to which it is attached.


1 Answers

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.

like image 137
Miroslav Bajtoš Avatar answered Jan 04 '23 12:01

Miroslav Bajtoš