From e73dcf4287140557e966079122f3edcef6d75d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kaiser?= Date: Sun, 31 Jan 2016 20:08:07 +0000 Subject: [PATCH 1/3] Added ability to add a validation function to a Cloud Code function --- functions.js | 8 ++++++++ index.js | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/functions.js b/functions.js index cf4aeb28bf..e287b98edf 100644 --- a/functions.js +++ b/functions.js @@ -10,6 +10,14 @@ var router = new PromiseRouter(); function handleCloudFunction(req) { // TODO: set user from req.auth if (Parse.Cloud.Functions[req.params.functionName]) { + // Run the validator for this function first + if (Parse.Cloud.Validators[req.params.functionName]) { + var result = Parse.Cloud.Validators[req.params.functionName](req.body); + if (!result) { + throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Validation failed.'); + } + } + return new Promise(function (resolve, reject) { var response = createResponseObject(resolve, reject); var request = { diff --git a/index.js b/index.js index 79a321986f..cbce320a86 100644 --- a/index.js +++ b/index.js @@ -113,14 +113,17 @@ function ParseServer(args) { function addParseCloud() { Parse.Cloud.Functions = {}; + Parse.Cloud.Validators = {}; Parse.Cloud.Triggers = { beforeSave: {}, beforeDelete: {}, afterSave: {}, afterDelete: {} }; - Parse.Cloud.define = function(functionName, handler) { + + Parse.Cloud.define = function(functionName, handler, validationHandler) { Parse.Cloud.Functions[functionName] = handler; + Parse.Cloud.Validators[functionName] = validationHandler; }; Parse.Cloud.beforeSave = function(parseClass, handler) { var className = getClassName(parseClass); From 4e3b558a01df9f6cfe791a416c556958d9690101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kaiser?= Date: Sun, 31 Jan 2016 20:08:07 +0000 Subject: [PATCH 2/3] Added ability to add a validation function to a Cloud Code function --- functions.js | 8 ++++++++ index.js | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/functions.js b/functions.js index cf4aeb28bf..e287b98edf 100644 --- a/functions.js +++ b/functions.js @@ -10,6 +10,14 @@ var router = new PromiseRouter(); function handleCloudFunction(req) { // TODO: set user from req.auth if (Parse.Cloud.Functions[req.params.functionName]) { + // Run the validator for this function first + if (Parse.Cloud.Validators[req.params.functionName]) { + var result = Parse.Cloud.Validators[req.params.functionName](req.body); + if (!result) { + throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Validation failed.'); + } + } + return new Promise(function (resolve, reject) { var response = createResponseObject(resolve, reject); var request = { diff --git a/index.js b/index.js index 79a321986f..cbce320a86 100644 --- a/index.js +++ b/index.js @@ -113,14 +113,17 @@ function ParseServer(args) { function addParseCloud() { Parse.Cloud.Functions = {}; + Parse.Cloud.Validators = {}; Parse.Cloud.Triggers = { beforeSave: {}, beforeDelete: {}, afterSave: {}, afterDelete: {} }; - Parse.Cloud.define = function(functionName, handler) { + + Parse.Cloud.define = function(functionName, handler, validationHandler) { Parse.Cloud.Functions[functionName] = handler; + Parse.Cloud.Validators[functionName] = validationHandler; }; Parse.Cloud.beforeSave = function(parseClass, handler) { var className = getClassName(parseClass); From 394b8751284b2492ee0109fecaf6c1d0aa0e8dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kaiser?= Date: Wed, 3 Feb 2016 21:51:40 +0000 Subject: [PATCH 3/3] Added function validation --- functions.js | 1 - index.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/functions.js b/functions.js index 8f1df1f952..48eae9544f 100644 --- a/functions.js +++ b/functions.js @@ -9,7 +9,6 @@ var router = new PromiseRouter(); function handleCloudFunction(req) { if (Parse.Cloud.Functions[req.params.functionName]) { - // Run the validator for this function first if (Parse.Cloud.Validators[req.params.functionName]) { var result = Parse.Cloud.Validators[req.params.functionName](req.body || {}); if (!result) { diff --git a/index.js b/index.js index cfe38fe4ab..ebab3751dd 100644 --- a/index.js +++ b/index.js @@ -131,7 +131,7 @@ function addParseCloud() { afterSave: {}, afterDelete: {} }; - + Parse.Cloud.define = function(functionName, handler, validationHandler) { Parse.Cloud.Functions[functionName] = handler; Parse.Cloud.Validators[functionName] = validationHandler;