Skip to content

Instantly share code, notes, and snippets.

@desaijay315
Created April 5, 2019 03:05
Show Gist options
  • Save desaijay315/e12c3c8ff88febd172165bab9d6ed008 to your computer and use it in GitHub Desktop.
Save desaijay315/e12c3c8ff88febd172165bab9d6ed008 to your computer and use it in GitHub Desktop.
const mongoose = require('mongoose')
const validator = require('validator')
const UserSchema = new mongoose.Schema({
name:{
type: String,
required: true,
trim: true
},
age:{
type: Number,
default: 0,
validate(value){
if(value < 0){
throw new Error('Age must be a positive number')
}
}
},
email:{
type: String,
required: true,
unique:true,
trim: true,
validate(value){
if(!validator.isEmail(value)){
throw new Error('Email is invalid!')
}
}
},
password:{
type:String,
required:true,
trim:true,
minlength: 7,
validate(value){
if(validator.isEmpty(value)){
throw new Error('Please enter your password!')
}else if(validator.equals(value.toLowerCase(),"password")){
throw new Error('Password is invalid!')
}else if(validator.contains(value.toLowerCase(), "password")){
throw new Error('Password should not contain password!')
}
}
},
tokens:[{
token:{
type:String,
required: true
}
}],
createdAt:{
type: Date,
default: Date.now
}
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment