You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Cloud Validator
* Update cloud-code.md
* Apply nits from code review
* Update _includes/cloudcode/cloud-code.md
Co-authored-by: Tom Fox <[email protected]>
* Update _includes/cloudcode/cloud-code.md
Co-authored-by: Tom Fox <[email protected]>
* Update _includes/cloudcode/cloud-code.md
Co-authored-by: Tom Fox <[email protected]>
* Update _includes/cloudcode/cloud-code.md
Co-authored-by: Tom Fox <[email protected]>
Co-authored-by: Tom Fox <[email protected]>
@@ -125,6 +125,136 @@ If there is an error, the response in the client looks like:
125
125
}
126
126
```
127
127
128
+
## Implementing cloud function validation
129
+
130
+
*Available only on parse-server cloud code starting 4.-.-*
131
+
132
+
It's important to make sure the parameters required for a Cloud function are provided, and are in the necessary format. Starting with Parse Server 4.-.-, you can now specify a validator function or object which will be called prior to your cloud function.
133
+
134
+
Let's take a look at the `averageStars` example. If you wanted to make sure that `request.params.movie` is provided, and `averageStars` can only be called by logged in users, you could add a validator object to the function.
If the rules specified in the validator object aren't met, the Cloud Function won't run. This means that you can confidently build your function, knowing that `request.params.movie` is defined, as well as `request.user`.
153
+
154
+
### More Advanced Validation
155
+
156
+
157
+
Often, not only is it important that `request.params.movie` is defined, but also that it's the correct data type. You can do this by providing an `Object` to the `fields` parameter in the Validator.
-`request.params.movie` is less than 20 characters
193
+
-`request.user` is defined
194
+
-`request.user.get('accType')` is defined
195
+
-`request.user.get('accType')` is equal to 'reviewer'
196
+
197
+
However, the requested user could set 'accType' to reviewer, and then recall the function. Here, you could provide validation on a `Parse.User``beforeSave` trigger. `beforeSave` validators have a few additional options available, to help you make sure your data is secure.
198
+
199
+
```javascript
200
+
Parse.Cloud.beforeSave(Parse.User, () => {
201
+
// any additional beforeSave logic here
202
+
}, {
203
+
fields: {
204
+
accType: {
205
+
default:'viewer',
206
+
constant:true
207
+
},
208
+
},
209
+
});
210
+
```
211
+
This means that the field `accType` on `Parse.User` will be 'viewer' on signup, and will be unchangable, unless `masterKey` is provided.
212
+
213
+
The full range of built-in Validation Options are:
214
+
215
+
-`requireMaster`: whether the function requires a `masterKey` to run.
216
+
-`requireUser`: whether the function requires a `request.user` to run.
217
+
-`validateMasterKey`: whether the validator should run on `masterKey` (defaults to false).
218
+
-`fields`: an `Array` or `Object` of fields that are required on the request.
219
+
-`requireUserKeys`: an `Array` of fields to be validated on `request.user`.
220
+
221
+
The full range of built-in Validation Options on `.fields` are:
222
+
223
+
-`type`: the type of the `request.params[field]` or `request.object.get(field)`.
224
+
-`default`: what the field should default to if it's `null`.
225
+
-`required`: whether the field is required.
226
+
-`options`: a singular option, array of options, or custom function of allowed values for the field.
227
+
-`constant`: whether the field is immutable.
228
+
-`error`: a custom error message if validation fails.
229
+
230
+
You can also pass a function to the Validator. This can help you apply reoccuring logic to your Cloud Code.
231
+
232
+
```javascript
233
+
constvalidationRules=request=> {
234
+
if (request.master) {
235
+
return;
236
+
}
237
+
if (!request.user||request.user.id!=='masterUser') {
238
+
throw'Unauthorized';
239
+
}
240
+
}
241
+
242
+
Parse.Cloud.define('adminFunction', request=> {
243
+
// do admin code here, confident that request.user.id is masterUser, or masterKey is provided
// do admin code here, confident that request.user.id is masterUser, or masterKey is provided
248
+
},validationRules)
249
+
250
+
```
251
+
252
+
### Some considerations to be aware of
253
+
- The validation function will run prior to your Cloud Code Functions. You can use async and promises here, but try to keep the validation as simple and fast as possible so your cloud requests resolve quickly.
254
+
- As previously mentioned, cloud validator objects will not validate if a masterKey is provided, unless `validateMasterKey:true` is set. However, if you set your validator to a function, the function will **always** run.
255
+
256
+
This range of options should help you write more secure Cloud Code. If you need help in any way, feel free to reach out on our [developer supported community forum](https://community.parseplatform.org/).
257
+
128
258
# Cloud Jobs
129
259
130
260
Sometimes you want to execute long running functions, and you don't want to wait for the response. Cloud Jobs are meant for just that.
@@ -171,20 +301,24 @@ Viewing jobs is supported on parse-dashboard starting version 1.0.19, but you ca
171
301
172
302
## beforeSave
173
303
174
-
### Implementing validation
304
+
### Implementing data validation
175
305
176
306
Another reason to run code in the cloud is to enforce a particular data format. For example, you might have both an Android and an iOS app, and you want to validate data for each of those. Rather than writing code once for each client environment, you can write it just once with Cloud Code.
177
307
178
308
Let's take a look at our movie review example. When you're choosing how many stars to give something, you can typically only give 1, 2, 3, 4, or 5 stars. You can't give -6 stars or 1337 stars in a review. If we want to reject reviews that are out of bounds, we can do this with the `beforeSave` method:
179
309
180
310
```javascript
181
311
Parse.Cloud.beforeSave("Review", (request) => {
182
-
if (request.object.get("stars") <1) {
183
-
throw"you cannot give less than one star";
184
-
}
185
-
186
-
if (request.object.get("stars") >5) {
187
-
throw"you cannot give more than five stars";
312
+
// do any additional beforeSave logic here
313
+
},{
314
+
fields: {
315
+
stars : {
316
+
required:true,
317
+
options:stars=> {
318
+
return stars >=1&& stars =<5;
319
+
},
320
+
error:'Your review must be between one and five stars'
321
+
}
188
322
}
189
323
});
190
324
@@ -216,7 +350,9 @@ If you want to use `beforeSave` for a predefined class in the Parse JavaScript S
@@ -289,17 +425,13 @@ const afterSave = function afterSave(request) {
289
425
You can run custom Cloud Code before an object is deleted. You can do this with the `beforeDelete` method. For instance, this can be used to implement a restricted delete policy that is more sophisticated than what can be expressed through [ACLs]({{ site.apis.js }}/classes/Parse.ACL.html). For example, suppose you have a photo album app, where many photos are associated with each album, and you want to prevent the user from deleting an album if it still has a photo in it. You can do that by writing a function like this:
0 commit comments