loopbackjs - How to use jsonschema for Loopback remoteMethod? -
in app want define json schemas custom api.
for example from: http://docs.strongloop.com/display/public/lb/remote+methods#remotemethods-example
module.exports = function(person){ person.greet = function(msg, cb) { cb(null, 'greetings... ' + msg); } person.remotemethod( 'greet', { accepts: <generate definitions jsonschema>, returns: <generate definitions jsonschema> } ); };
how that?
this right way?
my solution - validation decorator + remote method params object type
var validate = require('jsonschema').validate; byschema = function (schema) { return function (func) { return function () { var data = arguments[0], callback = arguments[1]; var result = validate(data, schema); if (result.errors.length > 0) { // errors in request body callback(null, { success: false, error: 'schema validation error', }); return; } return func.apply(this, arguments); }; }; }; defaultremotearguments = { accepts: { arg: 'data', type: 'object', http: function(ctx) { return ctx.req.body; } }, returns: { arg: 'data', type: 'object', root: true } };
example:
auth.login = byschema(require('<path shcemajson json request>')) (function(data, cb) { // process request }); auth.remotemethod('login', defaultremotearguments);
in solution contrib loopback explorer not useful, because request/response objects, not fields...
the correct way set type
in returns
attribute model name. in case write:
person.remotemethod( 'greet', { ... returns: {type:'person', ...} } );
Comments
Post a Comment