Update User Profile (the right way)


#1

I am trying to determine if there is is a better way to validate and update a user profile using Simple Schema and Collection2 packages (no autoform).

On the user creation method I call:
Schemas.Users.clean(user) and
check(user, Schemas.User) on the server side to validate the user object before insertion. Works great, and all fields are properly validated.

Problem is on the update. I only need to validate the fields in the user’s profile. So calling clean() and check() on the object passed, fails as it only includes fields from the user.profile. This leads to another issue, when I call:

Meteor.users.update(Meteor.userId(), {$set: {profile: user.profile}})

to update the users profile with the new data, none of the fields in the user.profile object are validated. Blank fields are even inserted even when I set optional to false like so:

    'profile.lastName': {
      type: String,
      optional: false,
      label: 'The last name of the user.'
    },

So as a workaround to this, I first grab the user from the DB doing a

const updateUser = Meteor.users.find()

and replace the updateUser.profile object with the one passed from the server like so:

updateUser.profile = user.profile

Finally I call my clean and check against the updateUser object and finally finally do the update like so:

Meteor.users.update(updateUser._id, {$set: {profile: updateUser.profile}})

There has to be a better way? Any help would be greatly appreciated!

Thanks.


#2

No-one has any suggestion?


#3

Have a look at the validated method package that should solve some of your issues