Advertisement
AllenYuan

userschema data validation

May 6th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var mongoose = require('mongoose');
  2. var uniqueValidator = require('mongoose-unique-validator');
  3.  
  4. // schema representing user data
  5. var UserSchema = new mongoose.Schema(
  6.   {
  7.     username: {
  8.       type: String,
  9.       lowercase: true,
  10.       unique: true,
  11.       required: [true, "can't be blank"],
  12.       match: [/^[a-zA-Z0-9]+$/, 'is invalid'],
  13.       index: true,
  14.     },
  15.     email: {
  16.       type: String,
  17.       lowercase: true,
  18.       unique: true,
  19.       required: [true, "can't be blank"],
  20.       match: [/\S+@\S+\.\S+/, 'is invalid'],
  21.       index: true,
  22.     },
  23.     bio: String,
  24.     image: String,
  25.     hash: String,
  26.     salt: String,
  27.   },
  28.   {timestamps: true}
  29. );
  30.  
  31. UserSchema.plugin(uniqueValidator, {message: 'is already taken.'});
  32.  
  33. // register the schema with mongoose as 'User'
  34. mongoose.model('User', UserSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement