Skip to content

Commit

Permalink
Merge pull request #226 from codegefluester/cloud_code_validation
Browse files Browse the repository at this point in the history
Cloud code validation
  • Loading branch information
gfosco committed Feb 5, 2016
2 parents b6a67e2 + 394b875 commit 696809f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cloud/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,9 @@ Parse.Cloud.define('foo', function(req, res) {
Parse.Cloud.define('bar', function(req, res) {
res.error('baz');
});

Parse.Cloud.define('requiredParameterCheck', function(req, res) {
res.success();
}, function(params) {
return params.name;
});
7 changes: 7 additions & 0 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ var router = new PromiseRouter();

function handleCloudFunction(req) {
if (Parse.Cloud.Functions[req.params.functionName]) {
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 = {
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,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);
Expand Down
36 changes: 36 additions & 0 deletions spec/ParseAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,42 @@ describe('miscellaneous', function() {
});
});

it('test cloud function parameter validation success', (done) => {
// Register a function with validation
Parse.Cloud.define('functionWithParameterValidation', (req, res) => {
res.success('works');
}, (params) => {
return params.success === 100;
});

Parse.Cloud.run('functionWithParameterValidation', {"success":100}).then((s) => {
delete Parse.Cloud.Functions['functionWithParameterValidation'];
done();
}, (e) => {
fail('Validation should not have failed.');
done();
});
});

it('test cloud function parameter validation', (done) => {
// Register a function with validation
Parse.Cloud.define('functionWithParameterValidationFailure', (req, res) => {
res.success('noway');
}, (params) => {
return params.success === 100;
});

Parse.Cloud.run('functionWithParameterValidationFailure', {"success":500}).then((s) => {
fail('Validation should not have succeeded');
delete Parse.Cloud.Functions['functionWithParameterValidationFailure'];
done();
}, (e) => {
expect(e.code).toEqual(141);
expect(e.message).toEqual('Validation failed.');
done();
});
});

it('fails on invalid client key', done => {
var headers = {
'Content-Type': 'application/octet-stream',
Expand Down

0 comments on commit 696809f

Please sign in to comment.