PCData
maintained by PandaClouds.com
PCData
provides a clean way to validate data in Parse Sever Cloud Code.
Installation
-
If you want to use this library, you first need to install the Node.js.
-
When you install node.js, will also be installed npm.
-
Please run the following command.
npm install --save @panda-clouds/parse-data
Usage
Node.js
const PCData = require('@panda-clouds/parse-data');
// example usage
Parse.Cloud.beforeSave('Book', request =>{
const data = new PCData(request);
// will throw Error if request.object.get('title') doesn't exist
data.prop('title').mustExist();
// Chain requirements together
data.prop('author').mustExist().mustNotChange();
});
You can replace PCData with any variable.
Methods
Unit Tests are an additional resource for learning functionality.
- mustExist()
requires the property to be set, else throws error
Example:
Parse.Cloud.beforeSave('Book', request =>{
const data = new PCData(request);
data.prop('title').mustExist(); // will throw Error if request.object.get('title') doesn't exist
});
- mustNotChange()
requires the property to be the same as the first time ACUTUALLY SETTING IT, else throws error
Note: undefined => value will NOT throw an error Consider the following
- the object is saved WITHOUT setting the property.
- the object is saved again WITH setting the property.
- this will pass
Example:
Parse.Cloud.beforeSave('Book', request =>{
const data = new PCData(request);
data.prop('title').mustNotChange(); // will throw Error if request.object.get('title') is not equal to request.original.get('title')
});
- mustBeBefore(propName)
Requires that the property be before the property specified by propName.
Note: only supported in Parse v3.
Example:
Parse.Cloud.beforeSave('TimePeriod', request => {
const data = new PCData(request);
data.prop('start').mustBeBefore('end'); // will throw Error if start or end are not Date objects or if start is not before end.
});
Contributions
Pull requests are welcome! here is a checklist to speed things up:
- modify
PCData.js
- add unit tests in
PCData.spec.js
- run
npm test
- document method in
README.md
- add your name to 'Contributors' in
README.md
Contributors
(Add your name)
- [*] Marc Smith
- [*] Scott Runyon