Skip to content

Commit 696809f

Browse files
committed
Merge pull request #226 from codegefluester/cloud_code_validation
Cloud code validation
2 parents b6a67e2 + 394b875 commit 696809f

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

cloud/main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@ Parse.Cloud.define('foo', function(req, res) {
9696
Parse.Cloud.define('bar', function(req, res) {
9797
res.error('baz');
9898
});
99+
100+
Parse.Cloud.define('requiredParameterCheck', function(req, res) {
101+
res.success();
102+
}, function(params) {
103+
return params.name;
104+
});

functions.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ var router = new PromiseRouter();
99

1010
function handleCloudFunction(req) {
1111
if (Parse.Cloud.Functions[req.params.functionName]) {
12+
if (Parse.Cloud.Validators[req.params.functionName]) {
13+
var result = Parse.Cloud.Validators[req.params.functionName](req.body || {});
14+
if (!result) {
15+
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Validation failed.');
16+
}
17+
}
18+
1219
return new Promise(function (resolve, reject) {
1320
var response = createResponseObject(resolve, reject);
1421
var request = {

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,17 @@ function ParseServer(args) {
127127

128128
function addParseCloud() {
129129
Parse.Cloud.Functions = {};
130+
Parse.Cloud.Validators = {};
130131
Parse.Cloud.Triggers = {
131132
beforeSave: {},
132133
beforeDelete: {},
133134
afterSave: {},
134135
afterDelete: {}
135136
};
136-
Parse.Cloud.define = function(functionName, handler) {
137+
138+
Parse.Cloud.define = function(functionName, handler, validationHandler) {
137139
Parse.Cloud.Functions[functionName] = handler;
140+
Parse.Cloud.Validators[functionName] = validationHandler;
138141
};
139142
Parse.Cloud.beforeSave = function(parseClass, handler) {
140143
var className = getClassName(parseClass);

spec/ParseAPI.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,42 @@ describe('miscellaneous', function() {
572572
});
573573
});
574574

575+
it('test cloud function parameter validation success', (done) => {
576+
// Register a function with validation
577+
Parse.Cloud.define('functionWithParameterValidation', (req, res) => {
578+
res.success('works');
579+
}, (params) => {
580+
return params.success === 100;
581+
});
582+
583+
Parse.Cloud.run('functionWithParameterValidation', {"success":100}).then((s) => {
584+
delete Parse.Cloud.Functions['functionWithParameterValidation'];
585+
done();
586+
}, (e) => {
587+
fail('Validation should not have failed.');
588+
done();
589+
});
590+
});
591+
592+
it('test cloud function parameter validation', (done) => {
593+
// Register a function with validation
594+
Parse.Cloud.define('functionWithParameterValidationFailure', (req, res) => {
595+
res.success('noway');
596+
}, (params) => {
597+
return params.success === 100;
598+
});
599+
600+
Parse.Cloud.run('functionWithParameterValidationFailure', {"success":500}).then((s) => {
601+
fail('Validation should not have succeeded');
602+
delete Parse.Cloud.Functions['functionWithParameterValidationFailure'];
603+
done();
604+
}, (e) => {
605+
expect(e.code).toEqual(141);
606+
expect(e.message).toEqual('Validation failed.');
607+
done();
608+
});
609+
});
610+
575611
it('fails on invalid client key', done => {
576612
var headers = {
577613
'Content-Type': 'application/octet-stream',

0 commit comments

Comments
 (0)