Created
April 5, 2019 03:05
-
-
Save desaijay315/e12c3c8ff88febd172165bab9d6ed008 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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