Parse Server Dashboard Column validation like User class username

407 Views Asked by At

I just want to know if there's a way to make the columns for a class when creating from Parse Dashboard mandatory. Just like _User class has Username, password and email required.

1

There are 1 best solutions below

0
On

I suggest you use Cloud Code's beforeSave triggers for this. These beforeSave triggers are similar to validators in Mongoose: they are automatically executed before any document of that specific type is saved.

On top of checking that some fields are required, you can use it to set default values, etc.

If the trigger returns an error, the document will not be saved. If it returns a success, the flow continues and the document is saved.

Here's a quick sample to create a beforeSave on the User class:

Parse.Cloud.beforeSave(Parse.User, function (request, response) {
    if (request.object.get('favouriteAnimals') === undefined) {
        request.object.set('favouriteAnimals', []);
    }
    if (!request.object.get('firstName') || !request.object.get('lastName')) {
        response.error('Full name is required.');
    }
    else {
        response.success();
    }
});