Require one of several columns on Mongoose.js

Is there a way to require only one (or several, but not all) multiple columns in a single collection in Mongoose.js? In my case, I use Passport and want my user to register through one of the providers that I provide, or make him / her. However, I do not want the user to register through any one provider, but depending on what he wants.

Here is an example diagram from the scotch.io tutorial in the Passport ( NOTE . This is an example. I will not use it in my application, but I can use something like this):

 // app/models/user.js // load the things we need var mongoose = require('mongoose'); var bcrypt = require('bcrypt-nodejs'); // define the schema for our user model var userSchema = mongoose.Schema({ local : { email : String, password : String, }, facebook : { id : String, token : String, email : String, name : String }, twitter : { id : String, token : String, displayName : String, username : String }, google : { id : String, token : String, email : String, name : String } }); // methods ====================== // generating a hash userSchema.methods.generateHash = function(password) { return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); }; // checking if password is valid userSchema.methods.validPassword = function(password) { return bcrypt.compareSync(password, this.local.password); }; // create the model for users and expose it to our app module.exports = mongoose.model('User', userSchema); 

How to make it necessary for at least one of the local , facebook , twitter or google objects to be specified (not null , not undefined , etc.) before saving the document without requiring one (and others are not required) or requiring all of them? As for the application, this will force the user to register for the first time with a username and password; Twitter or Facebook OAuth account or Google+ OpenID account. However, the user will not be tied to any one provider, therefore, he / she will not need to register with a username and password, but he also should not register through an account on a social network if this is not his thing.

+5
source share

Source: https://habr.com/ru/post/1206281/


All Articles