Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud code validation #226

Merged
merged 7 commits into from
Feb 5, 2016

Conversation

codegefluester
Copy link
Contributor

If your Cloud Functions have required parameters, you can do this today:

Parse.Cloud.define("foobar", function(req, res) {
  if (req.params.myRequiredParam != 'foobar') {
      res.error();
  }
 // ... do something else ...
});

OR

function isValidRequest(req) {
   return req.params.myRequiredParam;
}

Parse.Cloud.define("foobar", function(req, res) {
  if (!isValidRequest(req)) {
     res.error();
  }

  // ... do something else ...
});

Depending on the approach you use you might end up duplicating code or writing more if-not-valid-return-error kind of code all over the place.

This PR introduces the option to provide a Validator function for a Cloud Function which runs before the actual Cloud Function runs and validates the input parameters of the request. If the validation fails, it will return an error or executes the Cloud Function if the validation succeeds.

Example

Parse.Cloud.define("foobar", function(req, res) {
  res.success();
}, function(params) {
  return params.myRequiredParam;
});

OR for multiple functions that share a validator

var requireMyAwesomeParameter = function(params) {
  return params.myRequiredParam; 
}

Parse.Cloud.define("foobar1", function(req, res) {
  res.success();
}, requireMyAwesomeParameter);

Parse.Cloud.define("foobar2", function(req, res) {
  res.success();
}, requireMyAwesomeParameter);

This PR is just a foundation for other work that can be done around here, for example multiple validators per function, global validators that are only registered once by calling something like Parse.Cloud.validate(...) and returning detailed validation errors etc.

I added two test in ParseAPI.spec.js:L547

@gfosco
Copy link
Contributor

gfosco commented Feb 5, 2016

This is very cool, @codegefluester! 👍

@gfosco
Copy link
Contributor

gfosco commented Feb 5, 2016

If you could write up a post/docs about this, we should host it somewhere..

gfosco added a commit that referenced this pull request Feb 5, 2016
@gfosco gfosco merged commit 696809f into parse-community:master Feb 5, 2016
@codegefluester
Copy link
Contributor Author

Sure thing @gfosco, what would be the best place to put this?

@drew-gross
Copy link
Contributor

@natanrolnik
Copy link
Contributor

@codegefluester from what I understand, this doesn't enable multiple required parameters right? Do you think that returning an array of params is a great idea? I see that currently it only checks for one value, what would make an array not an option.

@codegefluester
Copy link
Contributor Author

@natanrolnik You can actually verify as many parameters as you want. We pass in the complete object of parameters of the request and all you need to do is returning true or false. I just used one parameter in the example to simplify it a bit.

I also noticed that it probably would be better to pass in the complete request as the current implementation would not contain request.user. If you want you can add it, otherwise I'll out up another pull request for this.

@codegefluester
Copy link
Contributor Author

Implemented in #468

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants