loopback remote method: parameter validation
Asked Answered
F

1

6

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?

Fawn answered 25/11, 2016 at 11:4 Comment(1)
Good question, I'm not sure it does on nested objects, requested some clarification hereNunciature
P
2

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.

Prod answered 9/8, 2017 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.