Consider the case:
validatedSchema = new schema({
id : "1454216545154",
name: { type: Number, validate: [
function(v){
return (this.id !== Server.LoggedIn.ID);
}, 'Don't try to change the name of another user!!!!'
]}
})
I haven't yet set up my full server for testing but am in planning stage.
Can we access sibling elements from the validation function, in this case 'id' and 'external' variables? And if so, how?
Thank you
No, you can't. But what you're looking to do here is really access control which doesn't belong in your ORM layer anyway.
But if you really wanted to do this, you could add pre-save middleware to check that the current user can only save changes to their own record:
validatedSchema.pre('save', function (next) {
if (this.id !== Server.LoggedIn.ID) {
next(new Error("Can't change another user's data!"));
} else {
next();
}
});