Added ability to add a validation function to a Cloud Code function
This commit is contained in:
@@ -10,6 +10,14 @@ var router = new PromiseRouter();
|
|||||||
function handleCloudFunction(req) {
|
function handleCloudFunction(req) {
|
||||||
// TODO: set user from req.auth
|
// TODO: set user from req.auth
|
||||||
if (Parse.Cloud.Functions[req.params.functionName]) {
|
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) {
|
return new Promise(function (resolve, reject) {
|
||||||
var response = createResponseObject(resolve, reject);
|
var response = createResponseObject(resolve, reject);
|
||||||
var request = {
|
var request = {
|
||||||
|
|||||||
5
index.js
5
index.js
@@ -113,14 +113,17 @@ function ParseServer(args) {
|
|||||||
|
|
||||||
function addParseCloud() {
|
function addParseCloud() {
|
||||||
Parse.Cloud.Functions = {};
|
Parse.Cloud.Functions = {};
|
||||||
|
Parse.Cloud.Validators = {};
|
||||||
Parse.Cloud.Triggers = {
|
Parse.Cloud.Triggers = {
|
||||||
beforeSave: {},
|
beforeSave: {},
|
||||||
beforeDelete: {},
|
beforeDelete: {},
|
||||||
afterSave: {},
|
afterSave: {},
|
||||||
afterDelete: {}
|
afterDelete: {}
|
||||||
};
|
};
|
||||||
Parse.Cloud.define = function(functionName, handler) {
|
|
||||||
|
Parse.Cloud.define = function(functionName, handler, validationHandler) {
|
||||||
Parse.Cloud.Functions[functionName] = handler;
|
Parse.Cloud.Functions[functionName] = handler;
|
||||||
|
Parse.Cloud.Validators[functionName] = validationHandler;
|
||||||
};
|
};
|
||||||
Parse.Cloud.beforeSave = function(parseClass, handler) {
|
Parse.Cloud.beforeSave = function(parseClass, handler) {
|
||||||
var className = getClassName(parseClass);
|
var className = getClassName(parseClass);
|
||||||
|
|||||||
Reference in New Issue
Block a user