This is a User Registration template that is customizable. You can set which properties you need and specify validations for those properties such as length, regex and if a property is or is not mandatory.
Sometimes you need to collect some information about the users of your Application, which can very depending on the application itself.
While some Apps only need a username and password, other Apps might need the Geo Location of the user, phone number, gender, among others. And to ensure a sanitized database, it would be convenient to have those properties validated, ensuring a Date string is in the right format, a Zip code matches a certain format for a Country, an email is actually an email and a URL contains a valid URL format.
To make this task easier, we at Back4app created this template that you can use for easy "turn on/turn off" of properties and support multiple validations.
This template relies on Cloud Code, which is the serverless functions in Back4app coded using NodeJS. We will use basically the same code, a function called validateData which will receive the Class name and the request for validation:
sync function validateData(className, request){
// Retrieve the parameters for the Class from the MandatoryFields class so we can validate each one
const query = new Parse.Query("MandatoryFields");
query.equalTo("class", className);
let results = await query.find();
if (results){
// Loop through the results array
for (let i = 0; i < results.length; i ++){
// pick each field and its properties
let field = results[i].get("field")
let isMandatory = results[i].get("isMandatory")
let minLength = results[i].get("minLength")
let maxLength = results[i].get("maxLength")
let regexValidation = results[i].get("regexValidation")
// If you need to debug values, uncomment the line below
//console.log("PROPERTY: " + field + " ISMANDATORY: " + isMandatory + " MINLENGTH: " + minLength + " MAXLENGTH: " + maxLength + " REGEX: " + regexValidation)
// if a field is mandatory but was not passed in the request
let value = request.object.get(field)
if (value){
if (isMandatory && ! value) {
throw "The field " + field + " is mandatory.";
}
// if a field does not meet the required length
if (value.length < minLength || value.length > maxLength) {
throw "The field " + field + " must have a length between " + minLength + " and " + maxLength + ".";
}
// if a field does not match the Regex validation
if (regexValidation && regexValidation.length > 0){
let regex = new RegExp(regexValidation);
if (! regex.test(value)) {
throw "The field " + field + " is did not match the Regex validation.";
}
}
}
}
}
}
That function will then be called on two BeforeSave triggers for the classes delivered with the class:
For the User class:
Parse.Cloud.beforeSave("User", async (request) => {
await validateData("User", request)
});
For the Gender class:
Parse.Cloud.beforeSave("Gender", async (request) => {
await validateData("Gender", request)
});
If you need assistance with Cloud Code, please check our Docs and the Parse Guide for reference.
This App is composed of three classes:
User The User class is the regular User class from Parse, tuned to contain these custom properties along with the regular ones:
Gender The Gender class contains information about the gender of the user so the user can pick the one which identifies to. It contains the following properties:
MandatoryFields The MandatoryFields class holds the values of Classes/Properties pairs that will be validated using Cloud Code to ensure only the correct data will be stored. It contains the following properties: