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

Adding validation functions to Cloud Functions #224

Closed
wants to merge 6 commits into from

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(...) etc.

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

@codegefluester
Copy link
Contributor Author

Re-doing some stuff

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.

1 participant