user.model.ts 808 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { model, Schema, Types } from "mongoose";

export interface IUser {
  email: string;
  name?: string;
  password: string;
  role?: Types.ObjectId;
}

const validateEmail = (email: string) => {
  const re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  return re.test(email);
};

15
16
17
18
19
20
21
22
23
24
25
const schema = new Schema<IUser>(
  {
    email: {
      type: String,
      rquired: true,
      unique: true,
      validate: [validateEmail, "이메일을 입력해주세요"],
    },
    name: { type: String },
    password: { type: String, required: true, select: false },
    role: { type: Schema.Types.ObjectId, ref: "Role" },
26
  },
27
28
29
30
31
32
33
34
35
  {
    toJSON: {
      versionKey: false,
      transform(doc, ret, options) {
        delete ret.password;
      },
    },
  }
);
36
37

export default model<IUser>("User", schema);