-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Validation Handler Update #6968
Conversation
Danger run resulted in 1 fail and 1 markdown; to find out more, see the checks page. Generated by 🚫 dangerJS |
Codecov Report
@@ Coverage Diff @@
## master #6968 +/- ##
==========================================
+ Coverage 93.83% 93.86% +0.02%
==========================================
Files 169 169
Lines 12271 12362 +91
==========================================
+ Hits 11514 11603 +89
- Misses 757 759 +2
Continue to review full report at Codecov.
|
Failing test isn't related to this PR. Also: how to handle docs. Should using the inbuilt validation blocks be recommended? I'm thinking the JS in this section of the docs titled "Implementing Validation" should change to something like: "We recommend using the third parameter of a Parse.Cloud function to validate the request, prior to the function being called. Validators should make sure all data referenced in the upcoming trigger is correctly formatted." const adminValidator = (request) => {
if (!request.user || !request.user.get('Admin')) {
throw 'Unauthorised';
}
}
Parse.Cloud.define('AdminFunction',(request) => {
// do admin function here. only admins will be able to call function.
}, adminValidator);
// ensure that Admin is set to false on signup, and that it cannot be edited.
Parse.Cloud.beforeSave(Parse.User, (request) => {
// do any additional trigger work here
},{
fields : {
Admin : {
default : false,
constant: true
}
}
}; And for the example function: Parse.Cloud.beforeSave("Review", (request) => {
},{
fields: {
stars : {
required:true,
options: stars => {
return stars >= 1 && stars =< 5;
},
error: 'Your review must be between one and five stars'
}
}
}); |
Can you increase code coverage? |
Thanks for this useful PR!
Not sure if this has been addressed yet, but returning nothing or |
Is coverage okay, or do we need more tests @dplewis? Also, my inbuilt validator object is heavily inspired by Vue, and cloud function validation that I find myself often using. I expect this to evolve as more use cases are found, but should anyone suggest any other options for it, please do. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some quick changes. The last step is to document the validationHandler.
@param {(Object|Function)} validationHandler
@dblythy Can you give me access to your fork? |
@dblythy Our JSDoc template needs to be updated to format correctly. |
Thanks again @dplewis, you’re a legend. |
Hello. Nice work but looks like de index.d.ts was not updated (before save does not accept the third arg). And sync func dosen't seems to work in 'options' attributes, even with a throw, am I doing wrong ? |
@dblythy would it be correct to say that an async options function doesn't work? I'm trying to implement a unique value validation on a column and think a check on validation level would be nicer solution then to throw an error in beforeSave. Since I need to do a db lookup it needs to be async. Since I'm checking on a pointer, I don't think I could do this on DB level. |
It is not currently possible @Cinezaster. I have opened an issue and will work on a fix in a wk or two. |
A bit to unpack here, let me know if you'd like it separated into multiple PRs.
Update Parse.Cloud Validation Handlers
This updates validation handlers to allow async, and removes the requirement for a return value on the validation handler. This is a breaking change to existing validator functionality, as returning 'false' won't fail the validation. Existing
return false
, will have to be replaced withthrow 'error'
.Extend Parse.Cloud Validation Handlers to other Triggers
For completeness, I have added the ability to add a validation function to beforeSave, afterSave, beforeDelete, afterDelete, beforeFind, afterFind, beforeFileSave, afterFileSave, beforeDeleteFile, afterDeleteFile, beforeConnect, beforeSubscribe and afterLiveQueryEvent triggers. I have added test cases for all uses.
Allow validation parameter to be an object
Probably jumped on this one a bit early. This allows for some default inbuilt validation, making Parse.Cloud functions easier, more secure, and more accessible to less experienced developers, for less effort.
Options:
requireUser
(Boolean): will throw if no userrequireMaster
(Boolean): will throw if no masterKeyfields
(Array of Strings or Object): used for validating params for Parse.Cloud.define, or validating fields on request.object. Examples:If fields is an array of strings, type will be ignored, and required will be 'true', e.g:
fields: ['data','data1']
requireUserKeys
(Array of Strings or Object): key required onrequest.user
to make a request. Useful for validating users have completed onboarding before calling further functions.validateMasterKey
: whether requests using {useMasterKey:true} should run the validation. In my opinion, it makes sense for requests with the masterKey to skip the validation by default.Options for validation object expected to evolve over time.
I haven't completely finished the docs yet for Parse.Cloud.js because I wanted to see your thoughts. If you're happy with the PR, I'll add all the validation options to Parse.Cloud.js.
Allow file triggers to throw Parse.Errors
At the moment, if you throw a parse error from a file trigger (
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'It should fail'
), the error will be converted to a file trigger error, removing the error.code that you've set. This PR passes the error from the trigger through.Closes #6965.
Let me know what you think