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/2] 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/2] 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);